(db, id, checkpoint, session, returnValue)
| 9409 | var LOWEST_SEQ = 0; |
| 9410 | |
| 9411 | function updateCheckpoint(db, id, checkpoint, session, returnValue) { |
| 9412 | return db.get(id).catch(function (err) { |
| 9413 | if (err.status === 404) { |
| 9414 | if (db.adapter === 'http' || db.adapter === 'https') ; |
| 9415 | return { |
| 9416 | session_id: session, |
| 9417 | _id: id, |
| 9418 | history: [], |
| 9419 | replicator: REPLICATOR, |
| 9420 | version: CHECKPOINT_VERSION |
| 9421 | }; |
| 9422 | } |
| 9423 | throw err; |
| 9424 | }).then(function (doc) { |
| 9425 | if (returnValue.cancelled) { |
| 9426 | return; |
| 9427 | } |
| 9428 | |
| 9429 | // if the checkpoint has not changed, do not update |
| 9430 | if (doc.last_seq === checkpoint) { |
| 9431 | return; |
| 9432 | } |
| 9433 | |
| 9434 | // Filter out current entry for this replication |
| 9435 | doc.history = (doc.history || []).filter(function (item) { |
| 9436 | return item.session_id !== session; |
| 9437 | }); |
| 9438 | |
| 9439 | // Add the latest checkpoint to history |
| 9440 | doc.history.unshift({ |
| 9441 | last_seq: checkpoint, |
| 9442 | session_id: session |
| 9443 | }); |
| 9444 | |
| 9445 | // Just take the last pieces in history, to |
| 9446 | // avoid really big checkpoint docs. |
| 9447 | // see comment on history size above |
| 9448 | doc.history = doc.history.slice(0, CHECKPOINT_HISTORY_SIZE); |
| 9449 | |
| 9450 | doc.version = CHECKPOINT_VERSION; |
| 9451 | doc.replicator = REPLICATOR; |
| 9452 | |
| 9453 | doc.session_id = session; |
| 9454 | doc.last_seq = checkpoint; |
| 9455 | |
| 9456 | return db.put(doc).catch(function (err) { |
| 9457 | if (err.status === 409) { |
| 9458 | // retry; someone is trying to write a checkpoint simultaneously |
| 9459 | return updateCheckpoint(db, id, checkpoint, session, returnValue); |
| 9460 | } |
| 9461 | throw err; |
| 9462 | }); |
| 9463 | }); |
| 9464 | } |
| 9465 | |
| 9466 | class CheckpointerInternal { |
| 9467 | constructor(src, target, id, returnValue, opts = { |
no test coverage detected
searching dependent graphs…