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

Commit 579b22c

Browse files
author
Alec Gibson
committed
Simplify shouldBreak calls for fetching snapshots by timestamp
This change removes or renames `shouldBreak` calls. In `Backend`, for clarity we instead pre-filter ops, and just pass around the ops we want to be applied to a snapshot. In the `MemoryMilestoneDB`, these functions are extracted and renamed to more descriptive break condition names.
1 parent 4a7a178 commit 579b22c

File tree

5 files changed

+63
-51
lines changed

5 files changed

+63
-51
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,5 +485,5 @@ The `41xx` and `51xx` codes are reserved for use by ShareDB DB adapters, and the
485485
* 5018 - Required QueryEmitter listener not assigned
486486
* 5019 - getMilestoneSnapshot MilestoneDB method unimplemented
487487
* 5020 - saveMilestoneSnapshot MilestoneDB method unimplemented
488-
* 5021 - getMilestoneSnapshotBeforeTime MilestoneDB method unimplemented
489-
* 5022 - getMilestoneSnapshotAfterTime MilestoneDB method unimplemented
488+
* 5021 - getMilestoneSnapshotAtOrBeforeTime MilestoneDB method unimplemented
489+
* 5022 - getMilestoneSnapshotAtOrAfterTime MilestoneDB method unimplemented

lib/backend.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -701,22 +701,14 @@ Backend.prototype._fetchSnapshotByTimestamp = function (collection, id, timestam
701701
var options = {metadata: true};
702702
db.getOps(collection, id, from, to, options, function (error, ops) {
703703
if (error) return callback(error);
704-
backend._buildSnapshotFromOps(id, milestoneSnapshot, ops, callback, function shouldBreak(nextOp) {
705-
var opTimestamp = nextOp && nextOp.m && nextOp.m.ts;
706-
return timestamp !== null && opTimestamp > timestamp;
707-
});
704+
filterOpsInPlaceBeforeTimestamp(ops, timestamp);
705+
backend._buildSnapshotFromOps(id, milestoneSnapshot, ops, callback);
708706
});
709707
});
710708
});
711709
};
712710

713-
Backend.prototype._buildSnapshotFromOps = function (id, startingSnapshot, ops, callback, shouldBreak) {
714-
if (typeof shouldBreak !== 'function') {
715-
shouldBreak = function () {
716-
return false;
717-
};
718-
}
719-
711+
Backend.prototype._buildSnapshotFromOps = function (id, startingSnapshot, ops, callback) {
720712
var type = null;
721713
var data;
722714
var fetchedVersion = 0;
@@ -731,10 +723,6 @@ Backend.prototype._buildSnapshotFromOps = function (id, startingSnapshot, ops, c
731723
for (var index = 0; index < ops.length; index++) {
732724
var op = ops[index];
733725

734-
if (shouldBreak(op)) {
735-
break;
736-
}
737-
738726
fetchedVersion = op.v + 1;
739727

740728
if (op.create) {
@@ -762,3 +750,18 @@ function pluckIds(snapshots) {
762750
}
763751
return ids;
764752
}
753+
754+
function filterOpsInPlaceBeforeTimestamp(ops, timestamp) {
755+
if (timestamp === null) {
756+
return;
757+
}
758+
759+
for (var i = 0; i < ops.length; i++) {
760+
var op = ops[i];
761+
var opTimestamp = op.m && op.m.ts;
762+
if (opTimestamp > timestamp) {
763+
ops.length = i;
764+
return;
765+
}
766+
}
767+
}

lib/client/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ Connection.prototype.fetchSnapshot = function(collection, id, version, callback)
642642
};
643643

644644
/**
645-
* Fetch a read-only snapshot at a given version
645+
* Fetch a read-only snapshot at a given timestamp
646646
*
647647
* @param collection - the collection name of the snapshot
648648
* @param id - the ID of the snapshot

lib/milestone-db/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ MilestoneDB.prototype.saveMilestoneSnapshot = function (collection, snapshot, ca
4141
};
4242

4343
MilestoneDB.prototype.getMilestoneSnapshotAtOrBeforeTime = function (collection, id, timestamp, callback) {
44-
var error = new ShareDBError(5021, 'getMilestoneSnapshotBeforeTime MilestoneDB method unimplemented');
44+
var error = new ShareDBError(5021, 'getMilestoneSnapshotAtOrBeforeTime MilestoneDB method unimplemented');
4545
this._callBackOrEmitError(error, callback);
4646
};
4747

4848
MilestoneDB.prototype.getMilestoneSnapshotAtOrAfterTime = function (collection, id, timestamp, callback) {
49-
var error = new ShareDBError(5022, 'getMilestoneSnapshotAfterTime MilestoneDB method unimplemented');
49+
var error = new ShareDBError(5022, 'getMilestoneSnapshotAtOrAfterTime MilestoneDB method unimplemented');
5050
this._callBackOrEmitError(error, callback);
5151
};
5252

lib/milestone-db/memory.js

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,8 @@ MemoryMilestoneDB.prototype = Object.create(MilestoneDB.prototype);
2525
MemoryMilestoneDB.prototype.getMilestoneSnapshot = function (collection, id, version, callback) {
2626
if (!this._isValidVersion(version)) return process.nextTick(callback, new ShareDBError(4001, 'Invalid version'));
2727

28-
var shouldBreak = function (currentSnapshot, nextSnapshot) {
29-
if (version === null) {
30-
return false;
31-
}
32-
33-
return nextSnapshot.v > version;
34-
};
35-
36-
this._findMilestoneSnapshot(collection, id, shouldBreak, callback);
28+
var predicate = versionLessThanOrEqualTo(version);
29+
this._findMilestoneSnapshot(collection, id, predicate, callback);
3730
};
3831

3932
MemoryMilestoneDB.prototype.saveMilestoneSnapshot = function (collection, snapshot, callback) {
@@ -57,31 +50,15 @@ MemoryMilestoneDB.prototype.saveMilestoneSnapshot = function (collection, snapsh
5750
MemoryMilestoneDB.prototype.getMilestoneSnapshotAtOrBeforeTime = function (collection, id, timestamp, callback) {
5851
if (!this._isValidTimestamp(timestamp)) return process.nextTick(callback, new ShareDBError(4001, 'Invalid timestamp'));
5952

60-
var shouldBreak = function (currentSnapshot, nextSnapshot) {
61-
if (timestamp === null) {
62-
return !!currentSnapshot;
63-
}
64-
65-
var mtime = nextSnapshot && nextSnapshot.m && nextSnapshot.m.mtime;
66-
return mtime > timestamp;
67-
};
68-
69-
this._findMilestoneSnapshot(collection, id, shouldBreak, callback);
53+
var filter = timestampLessThanOrEqualTo(timestamp);
54+
this._findMilestoneSnapshot(collection, id, filter, callback);
7055
};
7156

7257
MemoryMilestoneDB.prototype.getMilestoneSnapshotAtOrAfterTime = function (collection, id, timestamp, callback) {
7358
if (!this._isValidTimestamp(timestamp)) return process.nextTick(callback, new ShareDBError(4001, 'Invalid timestamp'));
7459

75-
var shouldBreak = function (currentSnapshot) {
76-
if (timestamp === null) {
77-
return false;
78-
}
79-
80-
var mtime = currentSnapshot && currentSnapshot.m && currentSnapshot.m.mtime;
81-
return mtime >= timestamp;
82-
}
83-
84-
this._findMilestoneSnapshot(collection, id, shouldBreak, function (error, snapshot) {
60+
var filter = timestampGreaterThanOrEqualTo(timestamp);
61+
this._findMilestoneSnapshot(collection, id, filter, function (error, snapshot) {
8562
if (error) return process.nextTick(callback, error);
8663

8764
var mtime = snapshot && snapshot.m && snapshot.m.mtime;
@@ -93,7 +70,7 @@ MemoryMilestoneDB.prototype.getMilestoneSnapshotAtOrAfterTime = function (collec
9370
});
9471
};
9572

96-
MemoryMilestoneDB.prototype._findMilestoneSnapshot = function (collection, id, shouldBreak, callback) {
73+
MemoryMilestoneDB.prototype._findMilestoneSnapshot = function (collection, id, breakCondition, callback) {
9774
if (!collection) return process.nextTick(callback, new ShareDBError(4001, 'Missing collection'));
9875
if (!id) return process.nextTick(callback, new ShareDBError(4001, 'Missing ID'));
9976

@@ -102,7 +79,7 @@ MemoryMilestoneDB.prototype._findMilestoneSnapshot = function (collection, id, s
10279
var milestoneSnapshot;
10380
for (var i = 0; i < milestoneSnapshots.length; i++) {
10481
var nextMilestoneSnapshot = milestoneSnapshots[i];
105-
if (shouldBreak(milestoneSnapshot, nextMilestoneSnapshot)) {
82+
if (breakCondition(milestoneSnapshot, nextMilestoneSnapshot)) {
10683
break;
10784
} else {
10885
milestoneSnapshot = nextMilestoneSnapshot;
@@ -116,3 +93,35 @@ MemoryMilestoneDB.prototype._getMilestoneSnapshotsSync = function (collection, i
11693
var collectionSnapshots = this._milestoneSnapshots[collection] || (this._milestoneSnapshots[collection] = {});
11794
return collectionSnapshots[id] || (collectionSnapshots[id] = []);
11895
};
96+
97+
function versionLessThanOrEqualTo(version) {
98+
return function (currentSnapshot, nextSnapshot) {
99+
if (version === null) {
100+
return false;
101+
}
102+
103+
return nextSnapshot.v > version;
104+
};
105+
}
106+
107+
function timestampGreaterThanOrEqualTo(timestamp) {
108+
return function (currentSnapshot) {
109+
if (timestamp === null) {
110+
return false;
111+
}
112+
113+
var mtime = currentSnapshot && currentSnapshot.m && currentSnapshot.m.mtime;
114+
return mtime >= timestamp;
115+
};
116+
}
117+
118+
function timestampLessThanOrEqualTo(timestamp) {
119+
return function (currentSnapshot, nextSnapshot) {
120+
if (timestamp === null) {
121+
return !!currentSnapshot;
122+
}
123+
124+
var mtime = nextSnapshot && nextSnapshot.m && nextSnapshot.m.mtime;
125+
return mtime > timestamp;
126+
};
127+
}

0 commit comments

Comments
 (0)