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 | 2 2 2 2 2 2 2 2 1 4177 1440 1348 671 1348 92 4085 52 4033 4745 4033 4033 4033 4033 4033 4033 4033 47 35 12 1 1118 1118 1118 1118 2 2077 2077 2077 2 2077 2 2077 2077 12 2065 104 104 8 8 96 2 5 5 286 2 1056 1056 312 291 291 291 286 5 291 291 21 18 14 4 4 4 3 747 4 743 1 286 278 8 8 8 1 12 12 12 12 12 4 8 8 8 8 4 4 1 24 24 24 12 12 4 8 2 | 'use strict'; var Promise = require('./../deps/promise'); var explain404 = require('./../deps/ajax/explain404'); var pouchCollate = require('pouchdb-collate'); var collate = pouchCollate.collate; var CHECKPOINT_VERSION = 1; var REPLICATOR = "pouchdb"; // This is an arbitrary number to limit the // amount of replication history we save in the checkpoint. // If we save too much, the checkpoing docs will become very big, // if we save fewer, we'll run a greater risk of having to // read all the changes from 0 when checkpoint PUTs fail // CouchDB 2.0 has a more involved history pruning, // but let's go for the simple version for now. var CHECKPOINT_HISTORY_SIZE = 5; var LOWEST_SEQ = 0; function updateCheckpoint(db, id, checkpoint, session, returnValue) { return db.get(id).catch(function (err) { if (err.status === 404) { if (db.type() === 'http') { explain404('PouchDB is just checking if a remote checkpoint exists.'); } return { session_id: session, _id: id, history: [], replicator: REPLICATOR, version: CHECKPOINT_VERSION }; } throw err; }).then(function (doc) { if (returnValue.cancelled) { return; } // Filter out current entry for this replication doc.history = (doc.history || []).filter(function (item) { return item.session_id !== session; }); // Add the latest checkpoint to history doc.history.unshift({ last_seq: checkpoint, session_id: session }); // Just take the last pieces in history, to // avoid really big checkpoint docs. // see comment on history size above doc.history = doc.history.slice(0, CHECKPOINT_HISTORY_SIZE); doc.version = CHECKPOINT_VERSION; doc.replicator = REPLICATOR; doc.session_id = session; doc.last_seq = checkpoint; return db.put(doc).catch(function (err) { if (err.status === 409) { // retry; someone is trying to write a checkpoint simultaneously return updateCheckpoint(db, id, checkpoint, session, returnValue); } throw err; }); }); } function Checkpointer(src, target, id, returnValue) { this.src = src; this.target = target; this.id = id; this.returnValue = returnValue; } Checkpointer.prototype.writeCheckpoint = function (checkpoint, session) { var self = this; return this.updateTarget(checkpoint, session).then(function () { return self.updateSource(checkpoint, session); }); }; Checkpointer.prototype.updateTarget = function (checkpoint, session) { return updateCheckpoint(this.target, this.id, checkpoint, session, this.returnValue); }; Checkpointer.prototype.updateSource = function (checkpoint, session) { var self = this; if (this.readOnlySource) { return Promise.resolve(true); } return updateCheckpoint(this.src, this.id, checkpoint, session, this.returnValue) .catch(function (err) { var isForbidden = typeof err.status === 'number' && Math.floor(err.status / 100) === 4; if (isForbidden) { self.readOnlySource = true; return true; } throw err; }); }; var comparisons = { "undefined": function(targetDoc, sourceDoc) { // This is the previous comparison function Eif (collate(targetDoc.last_seq, sourceDoc.last_seq) === 0) { return sourceDoc.last_seq; } return 0; }, "1": function(targetDoc, sourceDoc) { // This is the comparison function ported from CouchDB return compareReplicationLogs(sourceDoc, targetDoc).last_seq; } }; Checkpointer.prototype.getCheckpoint = function () { var self = this; return self.target.get(self.id).then(function (targetDoc) { return self.src.get(self.id).then(function (sourceDoc) { // Since we can't migrate an old version doc to a new one // (no session id), we just go with the lowest seq in this case Iif (targetDoc.version !== sourceDoc.version) { return LOWEST_SEQ; } var version; if (targetDoc.version) { version = targetDoc.version.toString(); } else { version = "undefined"; } Eif (version in comparisons) { return comparisons[version](targetDoc, sourceDoc); } return LOWEST_SEQ; }, function (err) { if (err.status === 404 && targetDoc.last_seq) { return self.src.put({ _id: self.id, last_seq: LOWEST_SEQ }).then(function () { return LOWEST_SEQ; }, function (err) { Eif (err.status === 401) { self.readOnlySource = true; return targetDoc.last_seq; } return LOWEST_SEQ; }); } throw err; }); }).catch(function (err) { if (err.status !== 404) { throw err; } return LOWEST_SEQ; }); }; // This checkpoint comparison is ported from CouchDBs source // they come from here: // https://github.com/apache/couchdb-couch-replicator/blob/master/src/couch_replicator.erl#L863-L906 function compareReplicationLogs (srcDoc, tgtDoc) { if (srcDoc.session_id === tgtDoc.session_id) { return { last_seq: srcDoc.last_seq, history: srcDoc.history || [] }; } var sourceHistory = srcDoc.history || []; var targetHistory = tgtDoc.history || []; return compareReplicationHistory(sourceHistory, targetHistory); } function compareReplicationHistory (sourceHistory, targetHistory) { // the erlang loop via function arguments is not so easy to repeat in JS // therefore, doing this as recursion var S = sourceHistory[0]; var sourceRest = sourceHistory.slice(1); var T = targetHistory[0]; var targetRest = targetHistory.slice(1); if (!S || targetHistory.length === 0) { return { last_seq: LOWEST_SEQ, history: [] }; } var sourceId = S.session_id; Iif (hasSessionId(sourceId, targetHistory)) { return { last_seq: S.last_seq, history: sourceHistory }; } var targetId = T.session_id; if (hasSessionId(targetId, sourceRest)) { return { last_seq: T.last_seq, history: targetRest }; } return compareReplicationHistory(sourceRest, targetRest); } function hasSessionId (sessionId, history) { var props = history[0]; var rest = history.slice(1); if (!sessionId || history.length === 0) { return false; } if (sessionId === props.session_id) { return true; } return hasSessionId(sessionId, rest); } module.exports = Checkpointer; |