-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsDbColl.js
68 lines (53 loc) · 1.83 KB
/
jsDbColl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Database collection implementation
var DbCollection = function(database, name, options) {
this._database = database;
this._options = options;
var created;
this._docStore = {};
this._docStore._hndl = jsdb_createDocStore(database, name, options.size, options.onDisk, &created);
if (!options.onDisk)
Db.inMem.push(name);
if (created)
if (!this._docStore._id_ && options.autoIndexId)
this._docStore._id_ = jsdb_createIndex(this._docStore._hndl, {_id:1}, "_id_", "art", 0, true, false);
Db.catalog[name] = this._docStore;
};
Db.prototype.getCollection = function(name) {
this[name] = new DbCollection(this._db, name, {size : Db._defaultSize, onDisk : Db._onDisk, autoIndexId : Db._autoIndexId});
};
Db.prototype.createCollection = function(name, options) {
return this[name] = new DbCollection(this._db, name, options);
};
DbCollection.prototype.save = function (document, concern) {
var docId, count, dbtxn = this._dbtxn, result;
if (!dbtxn)
dbtxn = jsdb_beginTxn(this._database);
if (jsdb_insertDocs(this._docStore._hndl, document, &docId, &count, dbtxn)) {
result = { nInserted : count};
if (!this._dbtxn)
jsdb_commitTxn(this._database, dbtxn);
} else {
result = {
nInserted : 0,
writeConcernError : {
code : 64,
errmsg : "timeout"
}
};
if (!this._dbtxn)
jsdb_rollbackTxn(this._database, dbtxn);
};
};
DbCollection.prototype.createIndex = function(key, options) {
var hndl, prev;
prev = Object.keys(this._docStore).length - 1;
if (hndl = jsdb_createIndex(this._docStore._hndl, key, options.name, options.type, options.size, options.unique, options.sparse, options.partialFilterExpression))
this._docStore[options.name] = hndl;
else
print ("createIndex error: ", options.name);
return {
numIndexesBefore : prev,
numIndexesAfter: Object.keys(this._docStore).length - 1,
ok : true
};
};