Skip to content

Commit cde4237

Browse files
committed
Changed store to prototype
1 parent 73a35e7 commit cde4237

File tree

1 file changed

+54
-46
lines changed

1 file changed

+54
-46
lines changed

lib/topology_base.js

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,78 @@ var MongoError = require('mongodb-core').MongoError
33

44
// The store of ops
55
var Store = function(topology, storeOptions) {
6+
var self = this;
67
var storedOps = [];
78
storeOptions = storeOptions || {force:false, bufferMaxEntries: -1}
89

10+
// Internal state
11+
this.s = {
12+
storedOps: storedOps
13+
, storeOptions: storeOptions
14+
, topology: topology
15+
}
16+
917
Object.defineProperty(this, 'length', {
10-
enumerable:true, get: function() { return storedOps.length; }
18+
enumerable:true, get: function() { return self.s.storedOps.length; }
1119
});
20+
}
1221

13-
this.add = function(opType, ns, ops, options, callback) {
14-
if(storeOptions.force) return callback(new MongoError("db closed by application"));
15-
if(storeOptions.bufferMaxEntries == 0) return callback(new MongoError(f("no connection available for operation and number of stored operation > %s", storeOptions.bufferMaxEntries)));
16-
if(storeOptions.bufferMaxEntries > 0 && storedOps.length > storeOptions.bufferMaxEntries) {
17-
while(storedOps.length > 0) {
18-
var op = storedOps.shift();
19-
op.c(new MongoError(f("no connection available for operation and number of stored operation > %s", storeOptions.bufferMaxEntries)));
20-
}
21-
22-
return;
22+
Store.prototype.add = function(opType, ns, ops, options, callback) {
23+
if(this.s.storeOptions.force) return callback(new MongoError("db closed by application"));
24+
if(this.s.storeOptions.bufferMaxEntries == 0) return callback(new MongoError(f("no connection available for operation and number of stored operation > %s", this.s.storeOptions.bufferMaxEntries)));
25+
if(this.s.storeOptions.bufferMaxEntries > 0 && this.s.storedOps.length > this.s.storeOptions.bufferMaxEntries) {
26+
while(this.s.storedOps.length > 0) {
27+
var op = this.s.storedOps.shift();
28+
op.c(new MongoError(f("no connection available for operation and number of stored operation > %s", this.s.storeOptions.bufferMaxEntries)));
2329
}
2430

25-
storedOps.push({t: opType, n: ns, o: ops, op: options, c: callback})
31+
return;
2632
}
2733

28-
this.addObjectAndMethod = function(opType, object, method, params, callback) {
29-
if(storeOptions.force) return callback(new MongoError("db closed by application"));
30-
if(storeOptions.bufferMaxEntries == 0) return callback(new MongoError(f("no connection available for operation and number of stored operation > %s", storeOptions.bufferMaxEntries)));
31-
if(storeOptions.bufferMaxEntries > 0 && storedOps.length > storeOptions.bufferMaxEntries) {
32-
while(storedOps.length > 0) {
33-
var op = storedOps.shift();
34-
op.c(new MongoError(f("no connection available for operation and number of stored operation > %s", storeOptions.bufferMaxEntries)));
35-
}
34+
this.s.storedOps.push({t: opType, n: ns, o: ops, op: options, c: callback})
35+
}
3636

37-
return;
37+
Store.prototype.addObjectAndMethod = function(opType, object, method, params, callback) {
38+
if(this.s.storeOptions.force) return callback(new MongoError("db closed by application"));
39+
if(this.s.storeOptions.bufferMaxEntries == 0) return callback(new MongoError(f("no connection available for operation and number of stored operation > %s", this.s.storeOptions.bufferMaxEntries)));
40+
if(this.s.storeOptions.bufferMaxEntries > 0 && this.s.storedOps.length > this.s.storeOptions.bufferMaxEntries) {
41+
while(this.s.storedOps.length > 0) {
42+
var op = this.s.storedOps.shift();
43+
op.c(new MongoError(f("no connection available for operation and number of stored operation > %s", this.s.storeOptions.bufferMaxEntries)));
3844
}
3945

40-
storedOps.push({t: opType, m: method, o: object, p: params, c: callback})
46+
return;
4147
}
4248

43-
this.flush = function() {
44-
while(storedOps.length > 0) {
45-
storedOps.shift().c(new MongoError(f("no connection available for operation")));
46-
}
47-
}
49+
this.s.storedOps.push({t: opType, m: method, o: object, p: params, c: callback})
50+
}
4851

49-
this.execute = function() {
50-
// Get current ops
51-
var ops = storedOps;
52-
// Reset the ops
53-
storedOps = [];
54-
55-
// Execute all the stored ops
56-
while(ops.length > 0) {
57-
var op = ops.shift();
58-
59-
if(op.t == 'cursor') {
60-
op.o[op.m].apply(op.o, op.p);
61-
} else {
62-
topology[op.t](op.n, op.o, op.op, op.c);
63-
}
64-
}
52+
Store.prototype.flush = function() {
53+
while(this.s.storedOps.length > 0) {
54+
this.s.storedOps.shift().c(new MongoError(f("no connection available for operation")));
6555
}
56+
}
6657

67-
this.all = function() {
68-
return storedOps;
69-
}
58+
Store.prototype.execute = function() {
59+
// Get current ops
60+
var ops = this.s.storedOps;
61+
// Reset the ops
62+
this.s.storedOps = [];
63+
64+
// Execute all the stored ops
65+
while(ops.length > 0) {
66+
var op = ops.shift();
67+
68+
if(op.t == 'cursor') {
69+
op.o[op.m].apply(op.o, op.p);
70+
} else {
71+
this.s.topology[op.t](op.n, op.o, op.op, op.c);
72+
}
73+
}
74+
}
75+
76+
Store.prototype.all = function() {
77+
return this.s.storedOps;
7078
}
7179

7280
// Server capabilities

0 commit comments

Comments
 (0)