Skip to content
This repository has been archived by the owner on Sep 30, 2023. It is now read-only.

SET opcode #50

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
26 changes: 12 additions & 14 deletions src/DocumentIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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, {})
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/DocumentStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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