Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed |ensureIndex| method in Collection, now it is done through JS i…

…nstead of Java. Added support methods for it and fixed minor bugs.
  • Loading branch information...
commit bc7704418b0f2da5f74212467d62baa22c043a72 1 parent 5740074
@sergi authored
Showing with 104 additions and 19 deletions.
  1. +95 −19 lib/mongodb.js
  2. +9 −0 lib/mongosupport.js
View
114 lib/mongodb.js
@@ -38,20 +38,20 @@ MongoDB.prototype = {
return this._db.authenticate(username, password);
},
command: function(cmd) {
- return this._db.command(cmd);
+ return this._db.command(new BasicDBObject(cmd));
},
createCollection: function(name, obj) {
return new Collection(
- this._db.createCollection(name, new BasicDBObject(obj)));
+ this._db.createCollection(name, new BasicDBObject(obj)), this);
},
dropDatabase: function() {
this._db.dropDatabase();
},
getCollection: function(name) {
- return new Collection(this._db.getCollection(name));
+ return new Collection(this._db.getCollection(name), this);
},
getCollectionFromFull: function(fullNameSpace) {
- return this._db.getCollectionFromFull(fullNameSpace);
+ return this._db.getCollectionFromFull(fullNameSpace, this);
},
getCollectionNames: function() {
return this._db.getCollectionNames().toArray().slice();
@@ -74,24 +74,83 @@ if (typeof exports != "undefined")
exports.Mongo = Mongo;
-function Collection(collection) {
+function Collection(collection, db) {
+ if (!db)
+ throw "A database argument is required.";
this.collection = collection;
+ this._db = db;
}
Collection.prototype = {
+ _db: null,
+ _dbCommand: function(cmd){
+ return this._db.command(cmd);
+ },
+ _genIndexName: function(keys){
+ var name = "";
+ for (var k in keys) {
+ if (name.length > 0)
+ name += "_";
+ name += k + "_";
+
+ var v = keys[k];
+ if (typeof v == "number")
+ name += v;
+ }
+ return name;
+ },
+ _indexCache: null,
+ _indexSpec: function(keys, options) {
+ var ret = { ns : this.getFullName() , key : keys , name : this._genIndexName( keys ) };
+ if (options) {
+ var type = typeof(options);
+ switch(type) {
+ case("string"):
+ ret.name = options;
+ break;
+ case("boolean"):
+ ret.unique = true;
+ break;
+ case("object"):
+ if ( options.length ) {
+ var nb = 0;
+ for each(var option in options) {
+ if (typeof ( option ) == "string")
+ ret.name = option;
+ else if (typeof(option) == "boolean" && option === true) {
+ if (nb == 0)
+ ret.unique = true;
+ if (nb == 1)
+ ret.dropDups = true;
+ nb++;
+ }
+ }
+ }
+ else {
+ Util.extend(ret, options);
+ }
+ break;
+ default:
+ throw "Can't handle: " + typeof(options) + "as an options argument.";
+ }
+ }
+ return ret;
+ },
+ getFullName: function() {
+ return this._db.getName()+"."+this.getName();
+ },
collection: null,
count: function() {
},
drop: function() {
+ this.resetIndexCache();
this.collection.dropIndexes();
this.collection.drop();
},
-
find: function(obj) {
var dbo = Util.createBDObject(obj);
return new Cursor(this.collection.find(dbo));
},
-
findOne: function(obj) {
var dbo = obj;
if (obj && typeof(obj) != "object")
@@ -107,46 +166,63 @@ Collection.prototype = {
} else
return null;
},
-
save: function(obj) {
var dbo = Util.createBDObject(obj);
var sm = this.collection.save(dbo) || {};
return Util.ensureStringId(sm);
},
-
getName: function() {
return this.collection.getName();
},
-
getCount: function() {
return this.collection.getCount();
},
+ /**
+ * Using the JavaScript way instead of the Java method because the latter
+ * automatically converts a numeric value into float and writes the name
+ * of the index using the float instead of an integer. This obviously makes
+ * comparisons fail in JavaScript
+ */
+ ensureIndex: function(keys, options){
+ var name = this._indexSpec(keys, options).name;
+ this._indexCache = this._indexCache || {};
+ if (this._indexCache[name])
+ return;
- ensureIndex: function(obj) {
- this.collection.ensureIndex(Util.createBDObject(obj));
+ this.createIndex(keys, options);
+ if (this._db.getLastError() == "")
+ this._indexCache[name] = true;
},
-
ensureIDIndex: function() {
this.collection.ensureIDIndex();
},
-
dropIndexes: function() {
this.collection.dropIndexes();
},
-
- dropIndex: function(obj) {
- this.collection.dropIndex(Util.createBDObject(obj));
+ createIndex: function(keys , options){
+ this._db.getCollection("system.indexes")
+ .insert(this._indexSpec(keys, options) ,true);
},
+ dropIndex: function(index) {
+ if (typeof(index) == "object")
+ index = this._genIndexName(index);
+ var res = this._dbCommand({deleteIndexes: this.getName(), index: index});
+ this.resetIndexCache();
+ return res;
+ },
getIndexInfo: function() {
return this.collection.getIndexInfo() || {};
},
-
insert: function(docs) {
var insertable = docs.isArray && docs.isArray() ?
- docs.map(createBDO) : Util.createBDObject(docs);
+ docs.map(Util.createBDObject) : Util.createBDObject(docs);
return this.collection.insert(insertable).length;
+ },
+ resetIndexCache: function() {
+ this._indexCache = {};
+ this.collection.resetIndexCache();
}
}
View
9 lib/mongosupport.js
@@ -7,6 +7,15 @@ var MongoSupport = {
createBDObject: function(obj) {
return obj ? new Packages.com.mongodb.BasicDBObject(obj)
: new Packages.com.mongodb.BasicDBObject();
+ },
+ extend: function( dst , src , deep ) {
+ for (var k in src){
+ var v = src[k];
+ if (deep && typeof(v) == "object")
+ v = MongoSupport.extend(typeof(v.length) == "number" ? [] : {} , v , true);
+ dst[k] = v;
+ }
+ return dst;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.