Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sharding on individual collections. #61

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/com/mongodb/DB.java
Expand Up @@ -82,6 +82,38 @@ public DBCollection getCollection( String name ){
return c;
}

/**
* Gets a collection with a given name.
* If the collection does not exist, a new collection is created, and the specified command is run against it.
* @param name the name of the collection to return
* @param cmd the command to run against a newly created collection
* @return the collection
*/
protected abstract DBCollection doGetCollectionWithDBCommandOnCreation( String name, DBObject cmd );

/**
* Gets a collection with a given name.
* If the collection does not exist, a new collection is created, and the specified command is run against it.
* @param name the name of the collection to return
* @param cmd the command to run against a newly created collection
* @return the collection
*/
public final DBCollection getCollectionWithAdminDBCommandOnCreation( String name, DBObject cmd ){
DBCollection c = doGetCollectionWithDBCommandOnCreation(name,cmd);
return c;
}

public final DBCollection getShardedCollection( String name ){
BasicDBObject key = new BasicDBObject("_id", new Integer(1));
return getCollectionShardedWithKey(name, key);
}

public DBCollection getCollectionShardedWithKey(String name, BasicDBObject key) {
DBObject cmd = new BasicDBObject( "shardcollection", _name + "." + name );
cmd.put("key", key);
return getCollectionWithAdminDBCommandOnCreation(name, cmd);
}

/**
* Creates a collection with a given name and options.
* If the collection does not exist, a new collection is created.
Expand Down
11 changes: 10 additions & 1 deletion src/main/com/mongodb/DBApiLayer.java
Expand Up @@ -113,13 +113,22 @@ public void requestEnsureConnection(){
}

protected MyCollection doGetCollection( String name ){
return doGetCollectionWithDBCommandOnCreation(name, null);
}

protected MyCollection doGetCollectionWithDBCommandOnCreation(String name, DBObject cmd) {
MyCollection c = _collections.get( name );
if ( c != null )
return c;

c = new MyCollection( name );
MyCollection old = _collections.putIfAbsent(name, c);
return old != null ? old : c;
if (old != null) return old;
else {
if (cmd != null) getMongo().getDB("admin").command(cmd);
return c;
}

}

String _removeRoot( String ns ){
Expand Down
24 changes: 23 additions & 1 deletion src/main/com/mongodb/Mongo.java
Expand Up @@ -286,12 +286,20 @@ public Mongo( MongoURI uri )
}

/**
* gets a database object
* gets a database object, creating it if it doesn't exist
* @param dbname the database name
* @return
*/
public DB getDB( String dbname ){
return getDBWithAdminCommandOnCreation(dbname, null);
}

/**
* gets a database object, creating it and running the specified command if the db doesn't exist
* @param dbname the database name
* @return
*/
public DB getDBWithAdminCommandOnCreation(String dbname, DBObject cmd){
DB db = _dbs.get( dbname );
if ( db != null )
return db;
Expand All @@ -300,9 +308,23 @@ public DB getDB( String dbname ){
DB temp = _dbs.putIfAbsent( dbname , db );
if ( temp != null )
return temp;
if ( cmd != null ) {
getDB("admin").command(cmd);

}
return db;
}

/**
* gets a sharded database object, creating it if it doesn't exist
* @param dbname the database name
* @return
*/
public DB getShardedDB( String dbname){
DBObject cmd = new BasicDBObject( "enablesharding", dbname );
return getDBWithAdminCommandOnCreation(dbname, cmd);
}

/**
* gets a collection of DBs used by the driver since this Mongo instance was created.
* This may include DBs that exist in the client but not yet on the server.
Expand Down