Skip to content

Commit 3bd0364

Browse files
committed
JAVA-390: Make it possible to override the bson decoder, not just the callback
JAVA-391: add a new BSON decoder and bson object type that uses a lazy approach like C++ code
1 parent 719b6b7 commit 3bd0364

21 files changed

+1507
-505
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,14 @@ public void resetOptions(){
12691269
public int getOptions(){
12701270
return _options.get();
12711271
}
1272+
1273+
public void setDBDecoderFactory(DBDecoderFactory dbDecoderFactory) {
1274+
this.dbDecoderFactory = dbDecoderFactory;
1275+
}
1276+
1277+
public DBDecoderFactory getDBDecoderFactory() {
1278+
return dbDecoderFactory;
1279+
}
12721280

12731281
final DB _db;
12741282

@@ -1284,4 +1292,6 @@ public int getOptions(){
12841292
private ReflectionDBObject.JavaWrapper _wrapper = null;
12851293

12861294
final private Set<String> _createdIndexes = new HashSet<String>();
1295+
1296+
private DBDecoderFactory dbDecoderFactory;
12871297
}

src/main/com/mongodb/DBDecoder.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.io.IOException;
19+
import java.io.InputStream;
20+
import org.bson.BSONDecoder;
21+
22+
/**
23+
*
24+
*/
25+
public interface DBDecoder extends BSONDecoder {
26+
public DBCallback getDBCallback(DBCollection collection);
27+
28+
public DBObject decode( byte[] b, DBCollection collection );
29+
30+
public DBObject decode( InputStream in, DBCollection collection ) throws IOException;
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
public interface DBDecoderFactory {
22+
23+
public DBDecoder create( );
24+
25+
}

src/main/com/mongodb/DBPort.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public DBPort( ServerAddress addr ){
5858
_hashCode = _addr.hashCode();
5959

6060
_logger = Logger.getLogger( _rootLogger.getName() + "." + addr.toString() );
61+
_decoder = _options.dbDecoderFactory.create();
6162
}
6263

6364
Response call( OutMessage msg , DBCollection coll )
@@ -107,7 +108,13 @@ private synchronized Response go( OutMessage msg , DBCollection coll , boolean f
107108
return null;
108109

109110
_processingResponse = true;
110-
return new Response( _sa , coll , _in , _decoder);
111+
DBDecoder decoder = _decoder;
112+
if (coll.getDBDecoderFactory() != null) {
113+
// custom decoder for this collection, use it
114+
// here we have to create a new decoder per call, pool would be nicer
115+
decoder = coll.getDBDecoderFactory().create();
116+
}
117+
return new Response( _sa , coll , _in , decoder);
111118
}
112119
catch ( IOException ioe ){
113120
close();
@@ -305,7 +312,7 @@ public DBPortPool getPool() {
305312
final DBPortPool _pool;
306313
final MongoOptions _options;
307314
final Logger _logger;
308-
final BSONDecoder _decoder = new BSONDecoder();
315+
final DBDecoder _decoder;
309316

310317
private Socket _socket;
311318
private InputStream _in;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.io.IOException;
19+
import java.io.InputStream;
20+
import org.bson.BSONCallback;
21+
import org.bson.BSONObject;
22+
import org.bson.BasicBSONDecoder;
23+
24+
/**
25+
*
26+
* @author antoine
27+
*/
28+
public class DefaultDBDecoder extends BasicBSONDecoder implements DBDecoder {
29+
30+
static class DefaultFactory implements DBDecoderFactory {
31+
@Override
32+
public DBDecoder create( ){
33+
return new DefaultDBDecoder( );
34+
}
35+
}
36+
37+
public static DBDecoderFactory FACTORY = new DefaultFactory();
38+
39+
public DefaultDBDecoder( ){
40+
}
41+
42+
public DBCallback getDBCallback(DBCollection collection) {
43+
// brand new callback every time
44+
return new DefaultDBCallback(collection);
45+
}
46+
47+
public DBObject decode(byte[] b, DBCollection collection) {
48+
DBCallback cbk = getDBCallback(collection);
49+
cbk.reset();
50+
decode(b, cbk);
51+
return (DBObject) cbk.get();
52+
}
53+
54+
public DBObject decode(InputStream in, DBCollection collection) throws IOException {
55+
DBCallback cbk = getDBCallback(collection);
56+
cbk.reset();
57+
decode(in, cbk);
58+
return (DBObject) cbk.get();
59+
}
60+
61+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 org.bson.LazyBSONCallback;
19+
20+
/**
21+
*
22+
*/
23+
public class LazyDBCallback extends LazyBSONCallback implements DBCallback {
24+
25+
@Override
26+
public Object createObject(byte[] data, int offset) {
27+
return new LazyDBObject(data, offset, this);
28+
}
29+
30+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.io.IOException;
19+
import java.io.InputStream;
20+
import org.bson.LazyBSONDecoder;
21+
22+
/**
23+
*
24+
*/
25+
public class LazyDBDecoder extends LazyBSONDecoder implements DBDecoder {
26+
static class LazyDBDecoderFactory implements DBDecoderFactory {
27+
@Override
28+
public DBDecoder create( ){
29+
return new LazyDBDecoder();
30+
}
31+
}
32+
33+
public static DBDecoderFactory FACTORY = new LazyDBDecoderFactory();
34+
35+
public LazyDBDecoder( ){
36+
}
37+
38+
public DBCallback getDBCallback(DBCollection collection) {
39+
// callback doesnt do anything special, unique per decoder
40+
return _callback;
41+
}
42+
43+
public DBObject decode(byte[] b, DBCollection collection) {
44+
DBCallback cbk = getDBCallback(collection);
45+
cbk.reset();
46+
decode(b, cbk);
47+
return (DBObject) cbk.get();
48+
}
49+
50+
public DBObject decode(InputStream in, DBCollection collection) throws IOException {
51+
DBCallback cbk = getDBCallback(collection);
52+
cbk.reset();
53+
decode(in, cbk);
54+
return (DBObject) cbk.get();
55+
}
56+
57+
DBCallback _callback = new LazyDBCallback();
58+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 org.bson.LazyBSONCallback;
19+
import org.bson.LazyBSONObject;
20+
21+
public class LazyDBObject extends LazyBSONObject implements DBObject {
22+
23+
public void markAsPartialObject() {
24+
}
25+
26+
public boolean isPartialObject() {
27+
return false;
28+
}
29+
30+
public LazyDBObject(byte[] data, LazyBSONCallback cbk) {
31+
this(data, 0, cbk);
32+
}
33+
34+
public LazyDBObject(byte[] data, int offset, LazyBSONCallback cbk) {
35+
super(data, offset, cbk);
36+
}
37+
}

src/main/com/mongodb/MongoOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void reset(){
4141
w = 0;
4242
wtimeout = 0;
4343
fsync = false;
44-
dbCallbackFactory = DefaultDBCallback.FACTORY;
44+
dbDecoderFactory = DefaultDBDecoder.FACTORY;
4545
}
4646

4747
/**
@@ -126,7 +126,7 @@ else if (safe)
126126
* Override the DBCallback factory. Default is for the standard Mongo Java
127127
* driver configuration.
128128
*/
129-
public DBCallbackFactory dbCallbackFactory;
129+
public DBDecoderFactory dbDecoderFactory;
130130

131131
/**
132132
* If <b>true</b> the driver sends a getLastError command after

src/main/com/mongodb/Response.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
class Response {
2828

29-
Response( ServerAddress addr , DBCollection collection , InputStream in, BSONDecoder decoder)
29+
Response( ServerAddress addr , DBCollection collection , InputStream in, DBDecoder decoder)
3030
throws IOException {
3131

3232
_host = addr;
@@ -54,16 +54,12 @@ class Response {
5454
else
5555
_objects = new ArrayList<DBObject>( _num );
5656

57-
DBCallback c = collection.getDB().getMongo().getMongoOptions().dbCallbackFactory.create( collection );
58-
5957
for ( int i=0; i < _num; i++ ){
6058
if ( user._toGo < 5 )
6159
throw new IOException( "should have more objects, but only " + user._toGo + " bytes left" );
62-
c.reset();
63-
decoder.decode( user , c );
64-
6560
// TODO: By moving to generics, you can remove these casts (and requirement to impl DBOBject).
66-
_objects.add( (DBObject)c.get() );
61+
62+
_objects.add( decoder.decode( user, collection ) );
6763
}
6864

6965
if ( user._toGo != 0 )

0 commit comments

Comments
 (0)