Skip to content

Commit

Permalink
Fixed BasicDBList
Browse files Browse the repository at this point in the history
  • Loading branch information
kristina committed Jul 20, 2009
1 parent fd924ba commit 4814519
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/main/com/mongodb/BasicDBList.java
Expand Up @@ -43,10 +43,13 @@ public BasicDBList(){
* @throws IndexOutOfBoundsException if <code>key</code> cannot be parsed into an <code>int</code>
*/
public Object put( String key , Object v ){
int i = _getInt( key );
while ( i >= size() )
return put(_getInt( key ), v);
}

public Object put( int key, Object v ) {
while ( key >= size() )
add( null );
set( i , v );
set( key , v );
return v;
}

Expand Down
20 changes: 12 additions & 8 deletions src/main/com/mongodb/ByteEncoder.java
Expand Up @@ -203,14 +203,17 @@ private int putObject( String name , DBObject o ){
final int sizePos = _buf.position();
_buf.putInt( 0 ); // leaving space for this. set it at the end

if ( o.containsKey( "_id" ) )
_putObjectField( "_id" , o.get( "_id" ) );

List transientFields = null;
{
Object temp = o.get( "_transientFields" );
if ( temp instanceof List )
transientFields = (List)temp;

if ( myType == OBJECT ) {
if ( o.containsKey( "_id" ) )
_putObjectField( "_id" , o.get( "_id" ) );

{
Object temp = o.get( "_transientFields" );
if ( temp instanceof List )
transientFields = (List)temp;
}
}


Expand Down Expand Up @@ -334,13 +337,14 @@ private boolean _handleSpecialObjects( String name , DBObject o ){
return true;
}

if ( o.get( Bytes.NO_REF_HACK ) != null ){
if ( !(o instanceof List) && o.get( Bytes.NO_REF_HACK ) != null ){
o.removeField( Bytes.NO_REF_HACK );
return false;
}

if ( ! _dontRefContains( o ) &&
name != null &&
!(o instanceof List) &&
cameFromDB( o ) ){
putDBRef( name , o.get( "_ns" ).toString() , (ObjectId)(o.get( "_id" ) ) );
return true;
Expand Down
23 changes: 23 additions & 0 deletions src/test/com/mongodb/DBObjectTest.java
Expand Up @@ -68,6 +68,29 @@ public void testToMap() {
assertEquals(m.get("z"), "a");
}

@Test(groups = {"basic"})
public void testBasicDBList() {
BasicDBList l = new BasicDBList();
l.put(10, "x");
assertEquals(l.get("10"), "x");
assertEquals(l.get(3), null);
l.put("10", "y");
assertEquals(l.get("10"), "y");

Mongo db;
try {
db = new Mongo( "127.0.0.1" , "test" );
}
catch (UnknownHostException e2) {
return;
}
DBCollection c = db.getCollection("dblist");
c.drop();
c.insert(BasicDBObjectBuilder.start().add("array", l).get());
DBObject obj = c.findOne();
assertEquals(obj.get("array") instanceof List, true);
}

@Test(groups = {"basic"})
public void testPutAll() {
DBObject start = BasicDBObjectBuilder.start().add( "a" , 1 ).add( "b" , 2 ).get();
Expand Down

0 comments on commit 4814519

Please sign in to comment.