Skip to content

Commit 9701ea6

Browse files
fixed bug in decoder for binary data, off by a byte at subtype; implemented LazyBSONList and added a writeable lazy bson object (needed for testing with morphia).
1 parent 960e825 commit 9701ea6

11 files changed

+294
-40
lines changed

src/main/com/mongodb/LazyDBCallback.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
*/
1616
package com.mongodb;
1717

18-
import org.bson.*;
19-
import org.bson.types.*;
18+
import java.util.Iterator;
19+
import java.util.logging.Logger;
2020

21-
import java.util.*;
22-
import java.util.logging.*;
21+
import org.bson.LazyBSONCallback;
22+
import org.bson.types.ObjectId;
2323

2424
/**
2525
*
@@ -51,5 +51,5 @@ public Object createDBRef( String ns, ObjectId id ){
5151

5252
final DBCollection _collection;
5353
final DB _db;
54-
private static final Logger log = Logger.getLogger( "com.mongodb.LazyDBCallback" );
54+
private static final Logger log = Logger.getLogger( LazyDBCallback.class.getName() );
5555
}

src/main/com/mongodb/LazyDBObject.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717

1818
import org.bson.LazyBSONCallback;
1919
import org.bson.LazyBSONObject;
20-
import org.bson.io.BSONByteBuffer;
21-
22-
import java.io.*;
20+
import org.bson.io.BSONByteBuffer;
2321

2422
public class LazyDBObject extends LazyBSONObject implements DBObject {
2523

26-
public void markAsPartialObject() {
24+
public void markAsPartialObject() {
25+
_partial = true;
2726
}
2827

2928
public boolean isPartialObject() {
30-
return false;
29+
return _partial;
3130
}
3231

3332
public LazyDBObject(BSONByteBuffer buff, LazyBSONCallback cbk){
@@ -46,4 +45,6 @@ public LazyDBObject(byte[] data, LazyBSONCallback cbk){
4645
public LazyDBObject(byte[] data, int offset, LazyBSONCallback cbk){
4746
super(data, offset, cbk);
4847
}
48+
49+
private boolean _partial = false;
4950
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright (C) 2008 10gen Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb;
17+
18+
import java.util.Iterator;
19+
import java.util.logging.Logger;
20+
21+
/**
22+
*
23+
*/
24+
public class LazyWriteableDBCallback extends LazyDBCallback {
25+
26+
public LazyWriteableDBCallback( DBCollection coll ){
27+
super(coll);
28+
}
29+
30+
@Override
31+
public Object createObject( byte[] data, int offset ){
32+
LazyWriteableDBObject o = new LazyWriteableDBObject( data, offset, this );
33+
//log.info("Created inner BSONObject: " + o);
34+
// need to detect DBRef but must make sure we dont search through all fields
35+
// $ref must be 1st key
36+
Iterator it = o.keySet().iterator();
37+
if ( it.hasNext() && it.next().equals( "$ref" ) &&
38+
o.containsField( "$id" ) ){
39+
return new DBRef( _db, o );
40+
}
41+
return o;
42+
}
43+
44+
private static final Logger log = Logger.getLogger( LazyWriteableDBCallback.class.getName() );
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (C) 2008 10gen Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb;
17+
18+
19+
/**
20+
*
21+
*/
22+
public class LazyWriteableDBDecoder extends LazyDBDecoder {
23+
static class LazyDBDecoderFactory implements DBDecoderFactory {
24+
@Override
25+
public DBDecoder create( ){
26+
return new LazyWriteableDBDecoder();
27+
}
28+
}
29+
30+
public static DBDecoderFactory FACTORY = new LazyDBDecoderFactory();
31+
32+
public DBCallback getDBCallback(DBCollection collection) {
33+
return new LazyWriteableDBCallback(collection);
34+
}
35+
36+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* Copyright (C) 2008 10gen Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb;
17+
18+
import java.util.HashMap;
19+
import java.util.HashSet;
20+
import java.util.Map;
21+
import java.util.Set;
22+
23+
import org.bson.BSONObject;
24+
import org.bson.LazyBSONCallback;
25+
import org.bson.io.BSONByteBuffer;
26+
27+
public class LazyWriteableDBObject extends LazyDBObject {
28+
29+
public LazyWriteableDBObject(BSONByteBuffer buff, LazyBSONCallback cbk){
30+
super(buff, cbk);
31+
}
32+
33+
public LazyWriteableDBObject(BSONByteBuffer buff, int offset, LazyBSONCallback cbk){
34+
super(buff, offset, cbk);
35+
}
36+
37+
38+
public LazyWriteableDBObject(byte[] data, LazyBSONCallback cbk){
39+
this(data, 0, cbk);
40+
}
41+
42+
public LazyWriteableDBObject(byte[] data, int offset, LazyBSONCallback cbk){
43+
super(data, offset, cbk);
44+
}
45+
46+
/* (non-Javadoc)
47+
* @see org.bson.LazyBSONObject#put(java.lang.String, java.lang.Object)
48+
*/
49+
@Override
50+
public Object put(String key, Object v) {
51+
return writeable.put(key, v);
52+
}
53+
54+
/* (non-Javadoc)
55+
* @see org.bson.LazyBSONObject#putAll(org.bson.BSONObject)
56+
*/
57+
@Override
58+
public void putAll(BSONObject o) {
59+
for(String key : o.keySet()){
60+
put(key, o.get(key));
61+
}
62+
}
63+
64+
/* (non-Javadoc)
65+
* @see org.bson.LazyBSONObject#putAll(java.util.Map)
66+
*/
67+
@SuppressWarnings("rawtypes")
68+
@Override
69+
public void putAll(Map m) {
70+
writeable.putAll(m);
71+
}
72+
73+
/* (non-Javadoc)
74+
* @see org.bson.LazyBSONObject#get(java.lang.String)
75+
*/
76+
@Override
77+
public Object get(String key) {
78+
Object o = writeable.get(key);
79+
return (o!=null) ? o : super.get(key);
80+
}
81+
82+
/* (non-Javadoc)
83+
* @see org.bson.LazyBSONObject#removeField(java.lang.String)
84+
*/
85+
@Override
86+
public Object removeField(String key) {
87+
Object o = writeable.remove(key);
88+
return (o!=null) ? o : super.removeField(key);
89+
}
90+
91+
/* (non-Javadoc)
92+
* @see org.bson.LazyBSONObject#containsField(java.lang.String)
93+
*/
94+
@Override
95+
public boolean containsField(String s) {
96+
boolean has = writeable.containsKey(s);
97+
return (has) ? has : super.containsField(s);
98+
}
99+
100+
/* (non-Javadoc)
101+
* @see org.bson.LazyBSONObject#keySet()
102+
*/
103+
@Override
104+
public Set<String> keySet() {
105+
Set<String> combined = new HashSet<String>();
106+
combined.addAll(writeable.keySet());
107+
combined.addAll(super.keySet());
108+
return combined;
109+
}
110+
111+
/* (non-Javadoc)
112+
* @see org.bson.LazyBSONObject#isEmpty()
113+
*/
114+
@Override
115+
public boolean isEmpty() {
116+
return writeable.isEmpty() || super.isEmpty();
117+
}
118+
119+
final private HashMap<String, Object> writeable = new HashMap<String, Object>();
120+
}

src/main/org/bson/LazyBSONCallback.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
*/
1616
package org.bson;
1717

18-
import org.bson.types.*;
18+
import java.util.List;
19+
import java.util.logging.Logger;
1920

20-
import java.util.logging.*;
21+
import org.bson.types.ObjectId;
2122

2223
/**
2324
*
@@ -57,10 +58,14 @@ public void setRootObject( Object root ){
5758
}
5859

5960
public Object createObject( byte[] data, int offset ){
60-
// TODO - LazyBSONList ?
6161
return new LazyBSONObject( data, offset, this );
6262
}
6363

64+
@SuppressWarnings("rawtypes")
65+
public List createArray( byte[] data, int offset ){
66+
return new LazyDBList( data, offset, this );
67+
}
68+
6469
public Object createDBRef( String ns, ObjectId id ){
6570
return new BasicBSONObject( "$ns", ns ).append( "$id", id );
6671
}

src/main/org/bson/LazyBSONList.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public int size(){
7979

8080
public class LazyBSONListIterator implements Iterator {
8181
ArrayList<ElementRecord> elements;
82-
int pos;
82+
int pos=0;
8383

8484
public LazyBSONListIterator() {
8585
elements = getElementsToKey( null );
@@ -92,7 +92,7 @@ public boolean hasNext(){
9292

9393
@Override
9494
public Object next(){
95-
return elements.get(++pos);
95+
return getElementValue(elements.get(pos++));
9696
}
9797

9898
@Override

src/main/org/bson/LazyBSONObject.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.UUID;
2525
import java.util.logging.Logger;
2626
import java.util.regex.Pattern;
27+
2728
import org.bson.io.BSONByteBuffer;
2829
import org.bson.types.BSONTimestamp;
2930
import org.bson.types.Code;
@@ -393,7 +394,7 @@ protected Object getElementValue( ElementRecord record ){
393394
case BSON.OBJECT:
394395
return _callback.createObject( _input.array(), record.valueOffset );
395396
case BSON.ARRAY:
396-
return _callback.createObject( _input.array(), record.valueOffset );
397+
return _callback.createArray( _input.array(), record.valueOffset );
397398
case BSON.BINARY:
398399
return readBinary( record.valueOffset );
399400
case BSON.REGEX:
@@ -411,6 +412,7 @@ private Object readBinary( int valueOffset ){
411412
final int totalLen = _input.getInt( valueOffset );
412413
valueOffset += 4;
413414
final byte bType = _input.get( valueOffset );
415+
valueOffset += 1;
414416

415417
byte[] bin;
416418
switch ( bType ){

src/main/org/bson/LazyDBList.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
*
3+
*/
4+
package org.bson;
5+
6+
import org.bson.io.BSONByteBuffer;
7+
8+
import com.mongodb.DBObject;
9+
import com.mongodb.util.JSON;
10+
11+
/**
12+
* @author scotthernandez
13+
*
14+
*/
15+
public class LazyDBList extends LazyBSONList implements DBObject {
16+
private static final long serialVersionUID = -4415279469780082174L;
17+
18+
public LazyDBList(byte[] data, LazyBSONCallback callback) { super(data, callback); }
19+
public LazyDBList(byte[] data, int offset, LazyBSONCallback callback) { super(data, offset, callback); }
20+
public LazyDBList(BSONByteBuffer buffer, LazyBSONCallback callback) { super(buffer, callback); }
21+
public LazyDBList(BSONByteBuffer buffer, int offset, LazyBSONCallback callback) { super(buffer, offset, callback); }
22+
23+
/**
24+
* Returns a JSON serialization of this object
25+
* @return JSON serialization
26+
*/
27+
@Override
28+
public String toString(){
29+
return JSON.serialize( this );
30+
}
31+
32+
public boolean isPartialObject(){
33+
return _isPartialObject;
34+
}
35+
36+
public void markAsPartialObject(){
37+
_isPartialObject = true;
38+
}
39+
40+
private boolean _isPartialObject;
41+
}

src/test/com/mongodb/JavaClientTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ public void testMapReduceInlineSecondary() throws Exception {
512512
c.save( new BasicDBObject( "x" , new String[]{ "a" , "b" } ) );
513513
c.save( new BasicDBObject( "x" , new String[]{ "b" , "c" } ) );
514514
WriteResult wr = c.save( new BasicDBObject( "x" , new String[]{ "c" , "d" } ) );
515-
if(mongo.getReplicaSetStatus() != null )
515+
if(mongo.getReplicaSetStatus() != null && mongo.getReplicaSetStatus().getName() != null)
516516
wr.getLastError(WriteConcern.REPLICAS_SAFE);
517517

518518
MapReduceOutput out =
@@ -531,9 +531,9 @@ public void testMapReduceInlineSecondary() throws Exception {
531531
assertEquals( 1 , m.get( "d" ).intValue() );
532532
ReplicaSetStatus replStatus = mongo.getReplicaSetStatus();
533533
//if it is a replicaset, and there is no master, or master is not the secondary
534-
if(replStatus!= null && ((replStatus.getMaster() == null) || (replStatus.getMaster() != null && !replStatus.getMaster().equals(replStatus.getASecondary()))))
535-
assertTrue( !mongo.getReplicaSetStatus().isMaster( out.getCommandResult().getServerUsed() ), "Had a replicaset but didn't use secondary! replSetStatus : " + mongo.getReplicaSetStatus() + " \n Used: " +
536-
out.getCommandResult().getServerUsed() + " \n ");
534+
if( replStatus!= null && replStatus.getName() != null && ((replStatus.getMaster() == null) || (replStatus.getMaster() != null && !replStatus.getMaster().equals(replStatus.getASecondary()))))
535+
assertTrue( !mongo.getReplicaSetStatus().isMaster( out.getCommandResult().getServerUsed() ),
536+
"Had a replicaset but didn't use secondary! replSetStatus : " + mongo.getReplicaSetStatus() + " \n Used: " + out.getCommandResult().getServerUsed() + " \n ");
537537
}
538538

539539
@Test

0 commit comments

Comments
 (0)