diff --git a/src/DocumentIndex.js b/src/DocumentIndex.js index ae1d55d..c114d2d 100644 --- a/src/DocumentIndex.js +++ b/src/DocumentIndex.js @@ -12,11 +12,12 @@ class DocumentIndex { } updateIndex (oplog, onProgressCallback) { - const reducer = (handled, item, idx) => { - if (item.payload.op === 'PUTALL' && item.payload.docs[Symbol.iterator]) { + const values = oplog.values + for (let i = 0; i <= values.length -1; i++) { + const item = values[i] + if (item.payload.op === 'PUTALL' && item.payload.docs && item.payload.docs[Symbol.iterator]) { for (const doc of item.payload.docs) { - if (doc && handled[doc.key] !== true) { - handled[doc.key] = true + if (doc) { this._index[doc.key] = { payload: { op: 'PUT', @@ -26,22 +27,19 @@ class DocumentIndex { } } } - } else if (handled[item.payload.key] !== true) { - handled[item.payload.key] = true - if (item.payload.op === 'PUT') { + } if (item.payload.op === 'PUT') { this._index[item.payload.key] = item } else if (item.payload.op === 'DEL') { delete this._index[item.payload.key] + } else if (item.payload.op === 'SET') { + if (this._index[item.payload.key]) { + Object.assign(this._index[item.payload.key].payload.value,item.payload.value) + } } + if (onProgressCallback) { + onProgressCallback(item, values.length - i) } - if (onProgressCallback) onProgressCallback(item, idx) - return handled } - - oplog.values - .slice() - .reverse() - .reduce(reducer, {}) } } diff --git a/src/DocumentStore.js b/src/DocumentStore.js index 4af323a..de6144e 100644 --- a/src/DocumentStore.js +++ b/src/DocumentStore.js @@ -97,6 +97,16 @@ class DocumentStore extends Store { value: null }, options) } + + set (key, edits = {}, options = {}) { + if (!this._index.get(key)) { throw new Error(`No entry with key '${key}' in the database`) } + + return this._addOperation({ + op: 'SET', + key: key, + value: edits + }, options) + } } module.exports = DocumentStore