diff --git a/src/main/com/mongodb/BasicDBObject.java b/src/main/com/mongodb/BasicDBObject.java index 44376e2ed1b..6a76aad59f4 100644 --- a/src/main/com/mongodb/BasicDBObject.java +++ b/src/main/com/mongodb/BasicDBObject.java @@ -92,4 +92,8 @@ public BasicDBObject append( String key , Object val ){ private boolean _isPartialObject = false; + + public BasicDBObject getObj(String key) { + return (BasicDBObject) super.getObj( key ); + } } diff --git a/src/main/com/mongodb/DB.java b/src/main/com/mongodb/DB.java index 09c1d658eab..0a679841be4 100644 --- a/src/main/com/mongodb/DB.java +++ b/src/main/com/mongodb/DB.java @@ -76,7 +76,6 @@ public final DBCollection getCollection( String name ){ } return c; } - /** * Creates a collection with a given name and options. * If the collection does not exist, a new collection is created. diff --git a/src/main/com/mongodb/DBApiLayer.java b/src/main/com/mongodb/DBApiLayer.java index 872525ef1a2..74218fcdbe0 100644 --- a/src/main/com/mongodb/DBApiLayer.java +++ b/src/main/com/mongodb/DBApiLayer.java @@ -233,7 +233,7 @@ protected WriteResult insert(DBObject[] arr, boolean shouldApply , com.mongodb.W return last; } - public WriteResult remove( DBObject o , com.mongodb.WriteConcern concern ) + public WriteResult remove( DBObject o , WriteConcern concern ) throws MongoException { if ( willTrace() ) trace( "remove: " + _fullNameSpace + " " + JSON.serialize( o ) ); @@ -312,7 +312,7 @@ public void createIndex( final DBObject keys, final DBObject options ) full.put( k , options.get( k ) ); full.put( "key" , keys ); - DBApiLayer.this.doGetCollection( "system.indexes" ).insert( new DBObject[]{ full } , false , WriteConcern.SAFE ); + DBApiLayer.this.doGetCollection( "system.indexes" ).insert( new DBObject[]{ full } , false , com.mongodb.WriteConcern.SAFE ); } final String _fullNameSpace; diff --git a/src/main/com/mongodb/DBCollection.java b/src/main/com/mongodb/DBCollection.java index ea8ea65b6f9..0ac76797b59 100644 --- a/src/main/com/mongodb/DBCollection.java +++ b/src/main/com/mongodb/DBCollection.java @@ -19,12 +19,10 @@ package com.mongodb; // Mongo -import org.bson.types.*; -import com.mongodb.util.*; - -// Java import java.util.*; +import org.bson.types.*; + /** This class provides a skeleton implementation of a database collection. *
A typical invocation sequence is thus *
* @dochub collections */ -@SuppressWarnings("unchecked") +@SuppressWarnings({"unchecked", "rawtypes"}) public abstract class DBCollection { /** @@ -681,7 +679,7 @@ public void dropIndexes( String name ) * Drops (deletes) this collection. Use with care. * @throws MongoException */ - public void drop() + public void drop() throws MongoException { resetIndexCache(); CommandResult res =_db.command( BasicDBObjectBuilder.start().add( "drop" , getName() ).get() ); @@ -706,9 +704,27 @@ public long count() * @return * @throws MongoException */ - public long count(DBObject query) + public long count(DBObject query) throws MongoException { - return getCount(query, null); + BasicDBObject cmd = new BasicDBObject(); + cmd.put( "count", getName() ); + cmd.put( "query", query ); + + CommandResult res = _db.command( cmd, getOptions() ); + + if ( ! res.ok() ){ + String errmsg = res.getErrorMessage(); + + if ( errmsg.equals("ns does not exist") || + errmsg.equals("ns missing" ) ){ + // for now, return 0 - lets pretend it does exist + return 0; + } + + throw new MongoException( "error counting : " + res ); + } + + return res.getLong("n"); } @@ -717,6 +733,7 @@ public long count(DBObject query) * @return number of documents that match query * @throws MongoException */ + @Deprecated public long getCount() throws MongoException { return getCount(new BasicDBObject(), null); @@ -728,6 +745,7 @@ public long getCount() * @return * @throws MongoException */ + @Deprecated public long getCount(DBObject query) throws MongoException { return getCount(query, null); @@ -740,6 +758,7 @@ public long getCount(DBObject query) * @return * @throws MongoException */ + @Deprecated public long getCount(DBObject query, DBObject fields) throws MongoException { return getCount( query , fields , 0 , 0 ); @@ -756,6 +775,7 @@ public long getCount(DBObject query, DBObject fields) * @return number of documents that match query and fields * @throws MongoException */ + @Deprecated public long getCount(DBObject query, DBObject fields, long limit, long skip ) throws MongoException { @@ -1288,7 +1308,7 @@ public int getOptions(){ final DB _db; - final protected String _name; + final protected String _name; final protected String _fullName; protected List@@ -34,7 +32,7 @@ *
BSONObject
.
+ * @param key the field to look for
+ * @return the field value cast to a BSONObject
.
+ */
+ public BSONObject getObj( String key ){
+ Object foo = get( key );
+ return (BSONObject) foo ;
+ }
+
+
/** Returns the value of a field as an int
.
* @param key the field to look for
* @param def the default to return
diff --git a/src/test/com/mongodb/DBCursorTest.java b/src/test/com/mongodb/DBCursorTest.java
index df2673364a9..1d9ba25d136 100644
--- a/src/test/com/mongodb/DBCursorTest.java
+++ b/src/test/com/mongodb/DBCursorTest.java
@@ -19,7 +19,7 @@
import java.io.*;
import java.util.*;
-import org.testng.annotations.Test;
+import org.testng.annotations.*;
import com.mongodb.util.*;
@@ -66,18 +66,53 @@ public void testSnapshot() {
assertEquals( 50 , c.find().snapshot().limit(50).toArray().size() );
}
- @Test(groups = {"basic"})
- public void testOptions() {
- DBCollection c = _db.getCollection("test");
- DBCursor dbCursor = c.find();
-
- assertEquals(0, dbCursor.getOptions());
- dbCursor.setOptions(Bytes.QUERYOPTION_TAILABLE);
- assertEquals(Bytes.QUERYOPTION_TAILABLE, dbCursor.getOptions());
- dbCursor.addOption(Bytes.QUERYOPTION_SLAVEOK);
- assertEquals(Bytes.QUERYOPTION_TAILABLE | Bytes.QUERYOPTION_SLAVEOK, dbCursor.getOptions());
- dbCursor.resetOptions();
- assertEquals(0, dbCursor.getOptions());
+ @Test(enabled = false)
+ public void testTailable() {
+ DBCollection c = _db.getCollection("tail1");
+ c.drop();
+ _db.createCollection( "tail1", new BasicDBObject("capped", true).append( "size", 10000 ) );
+ for ( int i=0; i<10; i++ )
+ c.save( new BasicDBObject( "x" , i ) );
+
+
+ DBCursor cur = c.find().sort( new BasicDBObject( "$natural" , 1 ) )
+ .addOption( Bytes.QUERYOPTION_TAILABLE );
+
+ while ( cur.hasNext() ) {
+ cur.next();
+ //do nothing...
+ }
+
+ assert( !cur.hasNext() );
+ c.save( new BasicDBObject( "x" , 12 ) );
+ assert( cur.hasNext() );
+ assertNotNull( cur.next() );
+ assert( !cur.hasNext() );
+
+ }
+
+ @Test(enabled = false)
+ public void testTailableAwait() {
+ DBCollection c = _db.getCollection("tail1");
+ c.drop();
+ _db.createCollection( "tail1", new BasicDBObject("capped", true).append( "size", 10000 ) );
+ for ( int i=0; i<10; i++ )
+ c.save( new BasicDBObject( "x" , i ) );
+
+
+ DBCursor cur = c.find().sort( new BasicDBObject( "$natural" , 1 ) )
+ .addOption( Bytes.QUERYOPTION_TAILABLE | Bytes.QUERYOPTION_AWAITDATA );
+
+ while ( cur.hasNext() ) {
+ cur.next();
+ //do nothing...
+ }
+
+ assert( !cur.hasNext() );
+ c.save( new BasicDBObject( "x" , 12 ) );
+ assert( cur.hasNext() );
+ assertNotNull( cur.next() );
+ assert( !cur.hasNext() );
}
@Test
diff --git a/src/test/com/mongodb/JavaClientTest.java b/src/test/com/mongodb/JavaClientTest.java
index 022c54b137e..330dbb8cec6 100644
--- a/src/test/com/mongodb/JavaClientTest.java
+++ b/src/test/com/mongodb/JavaClientTest.java
@@ -302,6 +302,16 @@ public void testTimestamp()
assert( t.getInc() > 0 );
}
+ @Test
+ public void testDrop(){
+ DBCollection c = _db.getCollection( "drop1" );
+ c.drop();
+ c.insert( new BasicDBObject( "_id" , 1 ) );
+ assertEquals( 1 , c.find().count() );
+ c.drop();
+ assertEquals( 0 , c.find().count() );
+ }
+
@Test
public void testStrictWriteSetInCollection(){
DBCollection c = _db.getCollection( "write1" );