/
mongo_driver.js
507 lines (420 loc) · 14.5 KB
/
mongo_driver.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/**
* Provide a synchronous Collection API using fibers, backed by
* MongoDB. This is only for use on the server, and mostly identical
* to the client API.
*
* NOTE: the public API methods must be run within a fiber. If you call
* these outside of a fiber they will explode!
*/
var path = __meteor_bootstrap__.require('path');
var MongoDB = __meteor_bootstrap__.require('mongodb');
var Future = __meteor_bootstrap__.require(path.join('fibers', 'future'));
_Mongo = function (url) {
var self = this;
self.collection_queue = [];
MongoDB.connect(url, function(err, db) {
if (err)
throw err;
self.db = db;
// drain queue of pending callbacks
var c;
while ((c = self.collection_queue.pop())) {
Fiber(function () {
db.collection(c.name, c.callback);
}).run();
}
});
};
// protect against dangerous selectors. falsey and {_id: falsey}
// are both likely programmer error, and not what you want,
// particularly for destructive operations.
_Mongo._rewriteSelector = function (selector) {
// shorthand -- scalars match _id
if ((typeof selector === 'string') || (typeof selector === 'number'))
selector = {_id: selector};
if (!selector || (('_id' in selector) && !selector._id))
// can't match anything
return {_id: Meteor.uuid()};
else
return selector;
};
// callback: lambda (err, collection) called when
// collection is ready to go, or on error.
_Mongo.prototype._withCollection = function(collection_name, callback) {
var self = this;
if (self.db) {
self.db.collection(collection_name, callback);
} else {
self.collection_queue.push({name: collection_name, callback: callback});
}
};
// This should be called synchronously with a write, to create a
// transaction on the current write fence, if any. After we can read
// the write, and after observers have been notified (or at least,
// after the observer notifiers have added themselves to the write
// fence), you should call 'committed()' on the object returned.
_Mongo.prototype._maybeBeginWrite = function () {
var self = this;
var fence = Meteor._CurrentWriteFence.get();
if (fence)
return fence.beginWrite();
else
return {committed: function () {}};
};
//////////// Public API //////////
// The write methods block until the database has confirmed the write
// (it may not be replicated or stable on disk, but one server has
// confirmed it.) (In the future we might have an option to turn this
// off, ie, to enqueue the request on the wire and return
// immediately.) They return nothing on success, and raise an
// exception on failure.
//
// After making a write (with insert, update, remove), observers are
// notified asynchronously. If you want to receive a callback once all
// of the observer notifications have landed for your write, do the
// writes inside a write fence (set Meteor._CurrentWriteFence to a new
// _WriteFence, and then set a callback on the write fence.)
//
// Since our execution environment is single-threaded, this is
// well-defined -- a write "has been made" if it's returned, and an
// observer "has been notified" if its callback has returned.
_Mongo.prototype.insert = function (collection_name, document) {
var self = this;
if (collection_name === "___meteor_failure_test_collection" &&
document.fail) {
var e = new Error("Failure test");
e.expected = true;
throw e;
}
var write = self._maybeBeginWrite();
var future = new Future;
self._withCollection(collection_name, function (err, collection) {
if (err) {
future.ret(err);
return;
}
collection.insert(document, {safe: true}, function (err) {
future.ret(err);
});
});
var err = future.wait();
Meteor.refresh({collection: collection_name});
write.committed();
if (err)
throw err;
};
_Mongo.prototype.remove = function (collection_name, selector) {
var self = this;
if (collection_name === "___meteor_failure_test_collection" &&
selector.fail) {
var e = new Error("Failure test");
e.expected = true;
throw e;
}
var write = self._maybeBeginWrite();
// XXX does not allow options. matches the client.
selector = _Mongo._rewriteSelector(selector);
var future = new Future;
self._withCollection(collection_name, function (err, collection) {
if (err) {
future.ret(err);
return;
}
collection.remove(selector, {safe: true}, function (err) {
future.ret(err);
});
});
var err = future.wait();
Meteor.refresh({collection: collection_name});
write.committed();
if (err)
throw err;
};
_Mongo.prototype.update = function (collection_name, selector, mod, options) {
var self = this;
if (collection_name === "___meteor_failure_test_collection" &&
selector.fail) {
var e = new Error("Failure test");
e.expected = true;
throw e;
}
var write = self._maybeBeginWrite();
selector = _Mongo._rewriteSelector(selector);
if (!options) options = {};
var future = new Future;
self._withCollection(collection_name, function (err, collection) {
if (err) {
future.ret(err);
return;
}
var opts = {safe: true};
// explictly enumerate options that minimongo supports
if (options.upsert) opts.upsert = true;
if (options.multi) opts.multi = true;
collection.update(selector, mod, opts, function (err) {
future.ret(err);
});
});
var err = future.wait();
Meteor.refresh({collection: collection_name});
write.committed();
if (err)
throw err;
};
_Mongo.prototype.find = function (collection_name, selector, options) {
var self = this;
if (arguments.length === 1)
selector = {};
return _Mongo._makeCursor(self, collection_name, selector, options);
};
_Mongo.prototype.findOne = function (collection_name, selector, options) {
var self = this;
if (arguments.length === 1)
selector = {};
return self.find(collection_name, selector, options).fetch()[0];
};
// We'll actually design an index API later. For now, we just pass through to
// Mongo's, but make it synchronous.
_Mongo.prototype._ensureIndex = function (collectionName, index, options) {
var self = this;
options = _.extend({safe: true}, options);
// We expect this function to be called at startup, not from within a method,
// so we don't interact with the write fence.
var future = new Future;
self._withCollection(collectionName, function (err, collection) {
if (err) {
future.throw(err);
return;
}
// XXX do we have to bindEnv or Fiber.run this callback?
collection.ensureIndex(index, options, function (err, indexName) {
if (err) {
future.throw(err);
return;
}
future.ret();
});
});
future.wait();
};
// Cursors
// Returns a _Mongo.Cursor, or throws an exception on
// failure. Creating a cursor involves a database query, and we block
// until it returns.
_Mongo._makeCursor = function (mongo, collection_name, selector, options) {
var future = new Future;
options = options || {};
selector = _Mongo._rewriteSelector(selector);
mongo._withCollection(collection_name, function (err, collection) {
if (err) {
future.ret([false, err]);
return;
}
var cursor = collection.find(selector, options.fields, {
sort: options.sort, limit: options.limit, skip: options.skip});
future.ret([true, cursor]);
});
var result = future.wait();
if (!(result[0]))
throw result[1];
return new _Mongo.Cursor(mongo, collection_name, selector, options,
result[1]);
};
// Do not call directly. Use _Mongo._makeCursor instead.
_Mongo.Cursor = function (mongo, collection_name, selector, options, cursor) {
var self = this;
if (!cursor)
throw new Error("Cursor required");
// NB: 'options' and 'selector' have already been preprocessed by _makeCursor
self.mongo = mongo;
self.collection_name = collection_name;
self.selector = selector;
self.options = options;
self.cursor = cursor;
self._synchronousNextObject = Future.wrap(cursor.nextObject.bind(cursor));
self._synchronousCount = Future.wrap(cursor.count.bind(cursor));
self.visited_ids = {};
};
_Mongo.Cursor.prototype._nextObject = function () {
var self = this;
while (true) {
var doc = self._synchronousNextObject().wait();
if (!doc || !doc._id) return null;
if (self.visited_ids[doc._id]) continue;
self.visited_ids[doc._id] = true;
return doc;
}
};
// XXX Make more like ECMA forEach:
// https://github.com/meteor/meteor/pull/63#issuecomment-5320050
_Mongo.Cursor.prototype.forEach = function (callback) {
var self = this;
// We implement the loop ourself instead of using self.cursor.each, because
// "each" will call its callback outside of a fiber which makes it much more
// complex to make this function synchronous.
while (true) {
var doc = self._nextObject();
if (!doc) return;
callback(doc);
}
};
// XXX Make more like ECMA map:
// https://github.com/meteor/meteor/pull/63#issuecomment-5320050
// XXX Allow overlapping callback executions if callback yields.
_Mongo.Cursor.prototype.map = function (callback) {
var self = this;
var res = [];
self.forEach(function (doc) {
res.push(callback(doc));
});
return res;
};
_Mongo.Cursor.prototype.rewind = function () {
var self = this;
// known to be synchronous
self.cursor.rewind();
self.visited_ids = {};
};
_Mongo.Cursor.prototype.fetch = function () {
var self = this;
return self.map(_.identity);
};
_Mongo.Cursor.prototype.count = function () {
var self = this;
return self._synchronousCount().wait();
};
_Mongo.Cursor.prototype._getRawObjects = function (ordered) {
var self = this;
if (ordered) {
return self.fetch();
} else {
var results = {};
self.forEach(function (doc) {
results[doc._id] = doc;
});
return results;
}
};
// options to contain:
// * callbacks for observe():
// - added (object, before_index)
// - changed (new_object, at_index, old_object)
// - moved (object, old_index, new_index) - can only fire with changed()
// - removed (object, at_index)
// * callbacks for _observeUnordered():
// - added (object)
// - changed (new_object)
// - removed (object)
//
// attributes available on returned LiveResultsSet
// * stop(): end updates
_Mongo.Cursor.prototype.observe = function (options) {
return new _Mongo.LiveResultsSet(this, true, options);
};
_Mongo.Cursor.prototype._observeUnordered = function (options) {
return new _Mongo.LiveResultsSet(this, false, options);
};
_Mongo.LiveResultsSet = function (cursor, ordered, options) {
var self = this;
// copy my cursor, so that the observe can run independently from
// some other use of the cursor.
self.cursor = _Mongo._makeCursor(cursor.mongo,
cursor.collection_name,
cursor.selector,
cursor.options);
// expose collection name
self.collection_name = cursor.collection_name;
self.ordered = ordered;
// previous results snapshot. on each poll cycle, diffs against
// results drives the callbacks.
self.results = ordered ? [] : {};
// state for polling
self.dirty = false; // do we need polling?
self.pending_writes = []; // people to notify when polling completes
self.poll_running = false; // is polling in progress now?
self.polling_suspended = false; // is polling temporarily suspended?
// (each instance of the class needs to get a separate throttling
// context -- we don't want to coalesce invocations of markDirty on
// different instances!)
self._markDirty = _.throttle(self._unthrottled_markDirty, 50 /* ms */);
// listen for the invalidation messages that will trigger us to poll
// the database for changes
var keys = self.cursor.options.key || {collection: cursor.collection_name};
if (!(keys instanceof Array))
keys = [keys];
self.crossbar_listeners = _.map(keys, function (key) {
return Meteor._InvalidationCrossbar.listen(key,function (notification,
complete) {
// When someone does a transaction that might affect us,
// schedule a poll of the database. If that transaction happens
// inside of a write fence, block the fence until we've polled
// and notified observers.
var fence = Meteor._CurrentWriteFence.get();
if (fence)
self.pending_writes.push(fence.beginWrite());
self._markDirty();
complete();
});
});
// user callbacks
self.added = options.added;
self.changed = options.changed;
self.removed = options.removed;
if (ordered)
self.moved = options.moved;
// run the first _poll() cycle synchronously.
self.poll_running = true;
self._doPoll();
self.poll_running = false;
// every once and a while, poll even if we don't think we're dirty,
// for eventual consistency with database writes from outside the
// Meteor universe
self.refreshTimer = Meteor.setInterval(_.bind(self._markDirty, this),
10 * 1000 /* 10 seconds */);
};
_Mongo.LiveResultsSet.prototype._unthrottled_markDirty = function () {
var self = this;
self.dirty = true;
if (self.polling_suspended)
return; // don't poll when told not to
if (self.poll_running)
return; // only one instance can run at once. just tell it to re-cycle.
self.poll_running = true;
Fiber(function () {
self.dirty = false;
var writes_for_cycle = self.pending_writes;
self.pending_writes = [];
self._doPoll(); // could yield, and set self.dirty
_.each(writes_for_cycle, function (w) {w.committed();});
self.poll_running = false;
if (self.dirty || self.pending_writes.length)
// rerun ourselves, but through _.throttle
self._markDirty();
}).run();
};
// interface for tests to control when polling happens
_Mongo.LiveResultsSet.prototype._suspendPolling = function() {
this.polling_suspended = true;
};
_Mongo.LiveResultsSet.prototype._resumePolling = function() {
this.polling_suspended = false;
this._unthrottled_markDirty(); // poll NOW, don't wait
};
_Mongo.LiveResultsSet.prototype._doPoll = function () {
var self = this;
// Get the new query results
self.cursor.rewind();
var new_results = self.cursor._getRawObjects(self.ordered);
var old_results = self.results;
LocalCollection._diffQuery(
self.ordered, old_results, new_results, self, true);
self.results = new_results;
};
_Mongo.LiveResultsSet.prototype.stop = function () {
var self = this;
_.each(self.crossbar_listeners, function (l) { l.stop(); });
Meteor.clearInterval(self.refreshTimer);
};
_.extend(Meteor, {
_Mongo: _Mongo
});