Skip to content

Commit 2ee21e9

Browse files
committed
[JAVA-289]: convenience methods to obtain max bson size object
1 parent a743354 commit 2ee21e9

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/main/com/mongodb/Mongo.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,26 @@ void _applyMongoOptions() {
514514
setWriteConcern( _options.getWriteConcern() );
515515
}
516516

517+
/**
518+
* Gets the maximum size for a BSON object supported by the current master server.
519+
* Note that this method may make a request to the server to obtain the size.
520+
* @throws MongoException
521+
* @return the size supported, or 0 if it cannot be determined.
522+
*/
523+
public int getMaxBsonObjectSize() {
524+
int size = 0;
525+
if (_connector.getReplicaSetStatus() != null)
526+
size = _connector.getReplicaSetStatus().getMaxBsonObjectSize();
527+
if (size == 0) {
528+
// need to fetch size
529+
DB db = getDB("admin");
530+
CommandResult res = db.command("isMaster");
531+
if (res.containsField("maxBsonObjectSize"))
532+
size = ((Integer)res.get( "maxBsonObjectSize" )).intValue();
533+
}
534+
return size;
535+
}
536+
517537
final ServerAddress _addr;
518538
final List<ServerAddress> _addrs;
519539
final MongoOptions _options;

src/main/com/mongodb/ReplicaSetStatus.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ synchronized void update(Set<Node> seenNodes){
169169
seenNodes.add(node);
170170
}
171171
}
172+
173+
if (_isMaster && res.containsField("maxBsonObjectSize"))
174+
_maxBsonObjectSize = ((Integer)res.get( "maxBsonObjectSize" )).intValue();
175+
172176
}
173177
catch ( MongoException e ){
174178
Throwable root = e;
@@ -388,11 +392,15 @@ void close(){
388392
_closed = true;
389393
}
390394

395+
public int getMaxBsonObjectSize() {
396+
return _maxBsonObjectSize;
397+
}
391398

392399
final List<Node> _all;
393400
Updater _updater;
394401
Mongo _mongo;
395402
String _setName = null; // null until init
403+
int _maxBsonObjectSize = 0;
396404
Logger _logger = _rootLogger; // will get changed to use set name once its found
397405

398406
String _lastPrimarySignal;

0 commit comments

Comments
 (0)