Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit 41f2395

Browse files
author
Alec Gibson
committed
Add a snapshot type to the readSnapshots middleware
Rather than pass the requested version into the `readSnapshots` middleware as an obscure way of determining if a snapshot is current or historical, this change adds an explicit `snapshotType` parameter to the middleware request, which states whether the snapshot is current or historical.
1 parent c60d7a9 commit 41f2395

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

lib/backend.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ Backend.prototype.MIDDLEWARE_ACTIONS = {
7777
submit: 'submit'
7878
};
7979

80+
Backend.prototype.SNAPSHOT_TYPES = {
81+
// The current snapshot is being fetched (eg through backend.fetch)
82+
current: 'current',
83+
// An historical snapshot is being fetched (eg through backend.fetchSnapshot)
84+
historical: 'historical'
85+
};
86+
8087
Backend.prototype._shimDocAction = function() {
8188
if (warnDeprecatedDoc) {
8289
warnDeprecatedDoc = false;
@@ -264,15 +271,7 @@ Backend.prototype._sanitizeOpsBulk = function(agent, projection, collection, ops
264271
}, callback);
265272
};
266273

267-
Backend.prototype._sanitizeSnapshots = function(agent, projection, collection, snapshots, request, callback) {
268-
if (typeof request === 'function') {
269-
callback = request;
270-
request = {};
271-
}
272-
273-
request.collection = collection;
274-
request.snapshots = snapshots;
275-
274+
Backend.prototype._sanitizeSnapshots = function(agent, projection, collection, snapshots, snapshotType, callback) {
276275
if (projection) {
277276
try {
278277
projections.projectSnapshots(projection.fields, snapshots);
@@ -281,6 +280,12 @@ Backend.prototype._sanitizeSnapshots = function(agent, projection, collection, s
281280
}
282281
}
283282

283+
var request = {
284+
collection: collection,
285+
snapshots: snapshots,
286+
snapshotType: snapshotType
287+
};
288+
284289
this.trigger(this.MIDDLEWARE_ACTIONS.readSnapshots, agent, request, callback);
285290
};
286291

@@ -360,7 +365,7 @@ Backend.prototype.fetch = function(agent, index, id, callback) {
360365
if (err) return callback(err);
361366
var snapshotProjection = backend._getSnapshotProjection(backend.db, projection);
362367
var snapshots = [snapshot];
363-
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, function(err) {
368+
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, backend.SNAPSHOT_TYPES.current, function(err) {
364369
if (err) return callback(err);
365370
backend.emit('timing', 'fetch', Date.now() - start, request);
366371
callback(null, snapshot);
@@ -384,7 +389,7 @@ Backend.prototype.fetchBulk = function(agent, index, ids, callback) {
384389
if (err) return callback(err);
385390
var snapshotProjection = backend._getSnapshotProjection(backend.db, projection);
386391
var snapshots = backend._getSnapshotsFromMap(ids, snapshotMap);
387-
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, function(err) {
392+
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, backend.SNAPSHOT_TYPES.current, function(err) {
388393
if (err) return callback(err);
389394
backend.emit('timing', 'fetchBulk', Date.now() - start, request);
390395
callback(null, snapshotMap);
@@ -572,7 +577,7 @@ Backend.prototype._query = function(agent, request, callback) {
572577
var backend = this;
573578
request.db.query(request.collection, request.query, request.fields, request.options, function(err, snapshots, extra) {
574579
if (err) return callback(err);
575-
backend._sanitizeSnapshots(agent, request.snapshotProjection, request.collection, snapshots, function(err) {
580+
backend._sanitizeSnapshots(agent, request.snapshotProjection, request.collection, snapshots, backend.SNAPSHOT_TYPES.current, function(err) {
576581
callback(err, snapshots, extra);
577582
});
578583
});
@@ -610,8 +615,8 @@ Backend.prototype.fetchSnapshot = function(agent, index, id, version, callback)
610615
if (error) return callback(error);
611616
var snapshotProjection = backend._getSnapshotProjection(backend.db, projection);
612617
var snapshots = [snapshot];
613-
var middlewareRequest = { v: version };
614-
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, middlewareRequest, function (error) {
618+
var snapshotType = backend.SNAPSHOT_TYPES.historical;
619+
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, snapshotType, function (error) {
615620
if (error) return callback(error);
616621
backend.emit('timing', 'fetchSnapshot', Date.now() - start, request);
617622
callback(null, snapshot);

lib/query-emitter.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ QueryEmitter.prototype.queryPoll = function(callback) {
186186
emitter.db.getSnapshotBulk(emitter.collection, inserted, emitter.fields, null, function(err, snapshotMap) {
187187
if (err) return emitter._finishPoll(err, callback, pending);
188188
var snapshots = emitter.backend._getSnapshotsFromMap(inserted, snapshotMap);
189-
emitter.backend._sanitizeSnapshots(emitter.agent, emitter.snapshotProjection, emitter.collection, snapshots, function(err) {
189+
var snapshotType = emitter.backend.SNAPSHOT_TYPES.current;
190+
emitter.backend._sanitizeSnapshots(emitter.agent, emitter.snapshotProjection, emitter.collection, snapshots, snapshotType, function(err) {
190191
if (err) return emitter._finishPoll(err, callback, pending);
191192
emitter._emitTiming('queryEmitter.pollGetSnapshotBulk', start);
192193
var diff = mapDiff(idsDiff, snapshotMap);
@@ -234,7 +235,8 @@ QueryEmitter.prototype.queryPollDoc = function(id, callback) {
234235
emitter.db.getSnapshot(emitter.collection, id, emitter.fields, null, function(err, snapshot) {
235236
if (err) return callback(err);
236237
var snapshots = [snapshot];
237-
emitter.backend._sanitizeSnapshots(emitter.agent, emitter.snapshotProjection, emitter.collection, snapshots, function(err) {
238+
var snapshotType = emitter.backend.SNAPSHOT_TYPES.current;
239+
emitter.backend._sanitizeSnapshots(emitter.agent, emitter.snapshotProjection, emitter.collection, snapshots, snapshotType, function(err) {
238240
if (err) return callback(err);
239241
emitter.onDiff([new arraydiff.InsertDiff(index, snapshots)]);
240242
emitter._emitTiming('queryEmitter.pollDocGetSnapshot', start);

test/client/snapshot-request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ describe('SnapshotRequest', function () {
228228
backend.use(backend.MIDDLEWARE_ACTIONS.readSnapshots,
229229
function (request) {
230230
expect(request.snapshots[0]).to.eql(v3);
231-
expect(request.v).to.be(3);
231+
expect(request.snapshotType).to.be(backend.SNAPSHOT_TYPES.historical);
232232
done();
233233
}
234234
);

0 commit comments

Comments
 (0)