Skip to content

Commit f7f5000

Browse files
committed
JAVA-505: Made encoder creation work just like decoder creation
1 parent 2dbbf9d commit f7f5000

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public abstract class DBCollection {
5454
* @dochub insert
5555
*/
5656
public WriteResult insert(DBObject[] arr , WriteConcern concern ) throws MongoException {
57-
return insert( arr, concern, getDBEncoderFactory().create() );
57+
return insert( arr, concern, getDBEncoder());
5858
}
5959

6060
/**
@@ -162,7 +162,7 @@ public WriteResult insert(List<DBObject> list, WriteConcern concern )
162162
* @dochub update
163163
*/
164164
public WriteResult update( DBObject q , DBObject o , boolean upsert , boolean multi , WriteConcern concern ) throws MongoException {
165-
return update( q, o, upsert, multi, concern, getDBEncoderFactory().create() );
165+
return update( q, o, upsert, multi, concern, getDBEncoder());
166166
}
167167

168168
/**
@@ -236,7 +236,7 @@ public WriteResult updateMulti( DBObject q , DBObject o ) throws MongoException
236236
* @dochub remove
237237
*/
238238
public WriteResult remove( DBObject o , WriteConcern concern ) throws MongoException {
239-
return remove( o, concern, getDBEncoderFactory().create() );
239+
return remove( o, concern, getDBEncoder());
240240
}
241241

242242
/**
@@ -433,7 +433,7 @@ public final void createIndex( final DBObject keys )
433433
* @throws MongoException
434434
*/
435435
public void createIndex( DBObject keys , DBObject options ) throws MongoException {
436-
createIndex( keys, options, getDBEncoderFactory().create() );
436+
createIndex( keys, options, getDBEncoder());
437437
}
438438

439439
/**
@@ -658,6 +658,12 @@ private DBDecoder getDecoder() {
658658
return _decoderFactory != null ? _decoderFactory.create() : null;
659659
}
660660

661+
// Only create a new encoder if there is an encoder factory explicitly set on the collection. Otherwise return null
662+
// to allow DB to create its own or use a cached one.
663+
private DBEncoder getDBEncoder() {
664+
return _encoderFactory != null ? _encoderFactory.create() : null;
665+
}
666+
661667

662668
/**
663669
* calls {@link DBCollection#apply(com.mongodb.DBObject, boolean)} with ensureID=true
@@ -1149,7 +1155,6 @@ protected DBCollection( DB base , String name ){
11491155
_name = name;
11501156
_fullName = _db.getName() + "." + name;
11511157
_options = new Bytes.OptionHolder( _db._options );
1152-
_encoderFactory = _db.getMongo().getMongoOptions().dbEncoderFactory;
11531158
}
11541159

11551160
protected DBObject _checkObject( DBObject o , boolean canBeNull , boolean query ){
@@ -1394,15 +1399,15 @@ public void slaveOk(){
13941399
* @param option
13951400
*/
13961401
public void addOption( int option ){
1397-
_options.add( option );
1402+
_options.add(option);
13981403
}
13991404

14001405
/**
14011406
* sets the default query options
14021407
* @param options
14031408
*/
14041409
public void setOptions( int options ){
1405-
_options.set( options );
1410+
_options.set(options);
14061411
}
14071412

14081413
/**
@@ -1442,10 +1447,7 @@ public DBDecoderFactory getDBDecoderFactory() {
14421447
* @param fact the factory to set.
14431448
*/
14441449
public void setDBEncoderFactory(DBEncoderFactory fact) {
1445-
if (fact == null)
1446-
_encoderFactory = _db.getMongo().getMongoOptions().dbEncoderFactory;
1447-
else
1448-
_encoderFactory = fact;
1450+
_encoderFactory = fact;
14491451
}
14501452

14511453
/**

0 commit comments

Comments
 (0)