@@ -329,7 +329,7 @@ public final DBObject findOne( Object obj )
329
329
* @dochub find
330
330
*/
331
331
public final DBObject findOne ( Object obj , DBObject fields ) {
332
- Iterator <DBObject > iterator = __find (new BasicDBObject ("_id" , obj ), fields , 0 , -1 , 0 , getOptions (), getReadPreference (), _decoderFactory . create () );
332
+ Iterator <DBObject > iterator = __find ( new BasicDBObject ("_id" , obj ), fields , 0 , -1 , 0 , getOptions (), getReadPreference (), getDecoder () );
333
333
return (iterator != null ? iterator .next () : null );
334
334
}
335
335
@@ -644,14 +644,20 @@ public final DBObject findOne( DBObject o, DBObject fields ) {
644
644
* @dochub find
645
645
*/
646
646
public final DBObject findOne ( DBObject o , DBObject fields , ReadPreference readPref ) {
647
- Iterator <DBObject > i = __find ( o , fields , 0 , -1 , 0 , getOptions (), readPref , _decoderFactory . create () );
647
+ Iterator <DBObject > i = __find ( o , fields , 0 , -1 , 0 , getOptions (), readPref , getDecoder () );
648
648
DBObject obj = (i == null ? null : i .next ());
649
649
if ( obj != null && ( fields != null && fields .keySet ().size () > 0 ) ){
650
650
obj .markAsPartialObject ();
651
651
}
652
652
return obj ;
653
653
}
654
654
655
+ // Only create a new decoder if there is a decoder factory explicitly set on the collection. Otherwise return null
656
+ // so that DBPort will use a cached decoder from the default factory.
657
+ private DBDecoder getDecoder () {
658
+ return _decoderFactory != null ? _decoderFactory .create () : null ;
659
+ }
660
+
655
661
656
662
/**
657
663
* calls {@link DBCollection#apply(com.mongodb.DBObject, boolean)} with ensureID=true
@@ -1143,7 +1149,6 @@ protected DBCollection( DB base , String name ){
1143
1149
_name = name ;
1144
1150
_fullName = _db .getName () + "." + name ;
1145
1151
_options = new Bytes .OptionHolder ( _db ._options );
1146
- _decoderFactory = _db .getMongo ().getMongoOptions ().dbDecoderFactory ;
1147
1152
_encoderFactory = _db .getMongo ().getMongoOptions ().dbEncoderFactory ;
1148
1153
}
1149
1154
@@ -1415,24 +1420,39 @@ public int getOptions(){
1415
1420
return _options .get ();
1416
1421
}
1417
1422
1423
+ /**
1424
+ * Set a customer decoder factory for this collection. Set to null to use the default from MongoOptions.
1425
+ * @param fact the factory to set.
1426
+ */
1418
1427
public void setDBDecoderFactory (DBDecoderFactory fact ) {
1419
- if (fact == null )
1420
- _decoderFactory = _db .getMongo ().getMongoOptions ().dbDecoderFactory ;
1421
- else
1422
- _decoderFactory = fact ;
1428
+ _decoderFactory = fact ;
1423
1429
}
1424
1430
1431
+ /**
1432
+ * Get the decoder factory for this collection. A null return value means that the default from MongoOptions
1433
+ * is being used.
1434
+ * @return the factory
1435
+ */
1425
1436
public DBDecoderFactory getDBDecoderFactory () {
1426
1437
return _decoderFactory ;
1427
1438
}
1428
1439
1440
+ /**
1441
+ * Set a customer encoder factory for this collection. Set to null to use the default from MongoOptions.
1442
+ * @param fact the factory to set.
1443
+ */
1429
1444
public void setDBEncoderFactory (DBEncoderFactory fact ) {
1430
1445
if (fact == null )
1431
1446
_encoderFactory = _db .getMongo ().getMongoOptions ().dbEncoderFactory ;
1432
1447
else
1433
1448
_encoderFactory = fact ;
1434
1449
}
1435
1450
1451
+ /**
1452
+ * Get the encoder factory for this collection. A null return value means that the default from MongoOptions
1453
+ * is being used.
1454
+ * @return the factory
1455
+ */
1436
1456
public DBEncoderFactory getDBEncoderFactory () {
1437
1457
return _encoderFactory ;
1438
1458
}
0 commit comments