Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add support for sharding on individual collections. #61

Closed
wants to merge 1 commit into from

3 participants

@drapp

Change by Miles O'Connor.

This commits a general method to run a command the first time a mongo instance interacts with a db, which is specifically used to enable sharding on individual collections. We'd love to know if there's a better way to do this.

@scotthernandez
Collaborator

I'm not so wild about this interface or the way this is done. It would probably be better to provide a set of helper classes related to sharding operations which do not clutter the DB/Collection interfaces.

@trishagee

We won't be adding these sorts of helper methods to the existing driver, since we haven't seen a lot of demand for this functionality. If this sort of thing is a requirement for a developer's application, we would suggest they write their own helper classes, the driver's API is quite crowded with methods at the moment as it is.

@trishagee trishagee closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 6, 2012
  1. @drapp
This page is out of date. Refresh to see the latest.
View
32 src/main/com/mongodb/DB.java
@@ -83,6 +83,38 @@ public DBCollection getCollection( String name ){
}
/**
+ * 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.
* Note that if the options parameter is null, the creation will be deferred to when the collection is written to.
View
11 src/main/com/mongodb/DBApiLayer.java
@@ -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 ){
View
24 src/main/com/mongodb/Mongo.java
@@ -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;
@@ -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.
Something went wrong with that request. Please try again.