(db, id, checkpoint, session, returnValue)
| 9376 | var LOWEST_SEQ = 0; |
| 9377 | |
| 9378 | function updateCheckpoint(db, id, checkpoint, session, returnValue) { |
| 9379 | return db.get(id).catch(function (err) { |
| 9380 | if (err.status === 404) { |
| 9381 | if (db.adapter === 'http' || db.adapter === 'https') { |
| 9382 | explainError( |
| 9383 | 404, 'PouchDB is just checking if a remote checkpoint exists.' |
| 9384 | ); |
| 9385 | } |
| 9386 | return { |
| 9387 | session_id: session, |
| 9388 | _id: id, |
| 9389 | history: [], |
| 9390 | replicator: REPLICATOR, |
| 9391 | version: CHECKPOINT_VERSION |
| 9392 | }; |
| 9393 | } |
| 9394 | throw err; |
| 9395 | }).then(function (doc) { |
| 9396 | if (returnValue.cancelled) { |
| 9397 | return; |
| 9398 | } |
| 9399 | |
| 9400 | // if the checkpoint has not changed, do not update |
| 9401 | if (doc.last_seq === checkpoint) { |
| 9402 | return; |
| 9403 | } |
| 9404 | |
| 9405 | // Filter out current entry for this replication |
| 9406 | doc.history = (doc.history || []).filter(function (item) { |
| 9407 | return item.session_id !== session; |
| 9408 | }); |
| 9409 | |
| 9410 | // Add the latest checkpoint to history |
| 9411 | doc.history.unshift({ |
| 9412 | last_seq: checkpoint, |
| 9413 | session_id: session |
| 9414 | }); |
| 9415 | |
| 9416 | // Just take the last pieces in history, to |
| 9417 | // avoid really big checkpoint docs. |
| 9418 | // see comment on history size above |
| 9419 | doc.history = doc.history.slice(0, CHECKPOINT_HISTORY_SIZE); |
| 9420 | |
| 9421 | doc.version = CHECKPOINT_VERSION; |
| 9422 | doc.replicator = REPLICATOR; |
| 9423 | |
| 9424 | doc.session_id = session; |
| 9425 | doc.last_seq = checkpoint; |
| 9426 | |
| 9427 | return db.put(doc).catch(function (err) { |
| 9428 | if (err.status === 409) { |
| 9429 | // retry; someone is trying to write a checkpoint simultaneously |
| 9430 | return updateCheckpoint(db, id, checkpoint, session, returnValue); |
| 9431 | } |
| 9432 | throw err; |
| 9433 | }); |
| 9434 | }); |
| 9435 | } |
no test coverage detected
searching dependent graphs…