From f71d7006d35090c34bd77602ede372fb91295745 Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sat, 27 Aug 2022 23:08:38 -0400 Subject: [PATCH 01/15] Update DocumentIndex.js EDIT opcode to overwrite specific values --- src/DocumentIndex.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/DocumentIndex.js b/src/DocumentIndex.js index ae1d55d..1ca2b3b 100644 --- a/src/DocumentIndex.js +++ b/src/DocumentIndex.js @@ -32,12 +32,17 @@ class DocumentIndex { this._index[item.payload.key] = item } else if (item.payload.op === 'DEL') { delete this._index[item.payload.key] + } else if (item.payload.op === 'EDIT') { + if(this._index[item.payload.key]){ + const vals = this._index[item.payload.key].payload.value; + for (const elem in item.payload.value) if (elem in vals) vals[elem] = item.payload.value[elem]; + this._index[item.payload.key].value = vals + } } } if (onProgressCallback) onProgressCallback(item, idx) return handled } - oplog.values .slice() .reverse() From 1224ef2040c58d2876ed32a83a4d2b094eb1f41d Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sat, 27 Aug 2022 23:09:39 -0400 Subject: [PATCH 02/15] Update DocumentStore.js edit opcode --- src/DocumentStore.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/DocumentStore.js b/src/DocumentStore.js index 4af323a..0b9470e 100644 --- a/src/DocumentStore.js +++ b/src/DocumentStore.js @@ -97,6 +97,16 @@ class DocumentStore extends Store { value: null }, options) } + + edit (key, options = {}, edits) { + if (!this._index.get(key)) { throw new Error(`No entry with key '${key}' in the database`) } + + return this._addOperation({ + op: 'EDIT', + key: key, + value: edits + }, options) + } } module.exports = DocumentStore From 3bcb00dd367f361a2d7cf54ea3add7b9f7cb8753 Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sat, 27 Aug 2022 23:10:41 -0400 Subject: [PATCH 03/15] Update DocumentIndex.js --- src/DocumentIndex.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DocumentIndex.js b/src/DocumentIndex.js index 1ca2b3b..b7a7abd 100644 --- a/src/DocumentIndex.js +++ b/src/DocumentIndex.js @@ -43,6 +43,7 @@ class DocumentIndex { if (onProgressCallback) onProgressCallback(item, idx) return handled } + oplog.values .slice() .reverse() From 214942c38ea6367214405236904bfe40a542c7d5 Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sat, 27 Aug 2022 23:13:41 -0400 Subject: [PATCH 04/15] Update DocumentIndex.js --- src/DocumentIndex.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DocumentIndex.js b/src/DocumentIndex.js index b7a7abd..cda46c3 100644 --- a/src/DocumentIndex.js +++ b/src/DocumentIndex.js @@ -33,7 +33,7 @@ class DocumentIndex { } else if (item.payload.op === 'DEL') { delete this._index[item.payload.key] } else if (item.payload.op === 'EDIT') { - if(this._index[item.payload.key]){ + if (this._index[item.payload.key]) { const vals = this._index[item.payload.key].payload.value; for (const elem in item.payload.value) if (elem in vals) vals[elem] = item.payload.value[elem]; this._index[item.payload.key].value = vals From 93dc81497cc9841939f48f41017a40f7efdd162e Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sat, 27 Aug 2022 23:23:15 -0400 Subject: [PATCH 05/15] Update DocumentStore.js --- src/DocumentStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DocumentStore.js b/src/DocumentStore.js index 0b9470e..a58d45c 100644 --- a/src/DocumentStore.js +++ b/src/DocumentStore.js @@ -98,7 +98,7 @@ class DocumentStore extends Store { }, options) } - edit (key, options = {}, edits) { + edit (key, options = {}, edits = {}) { if (!this._index.get(key)) { throw new Error(`No entry with key '${key}' in the database`) } return this._addOperation({ From a1c868624be64f09408001ada93eed4247ccfd4c Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sat, 27 Aug 2022 23:47:15 -0400 Subject: [PATCH 06/15] Update DocumentIndex.js --- src/DocumentIndex.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DocumentIndex.js b/src/DocumentIndex.js index cda46c3..583d011 100644 --- a/src/DocumentIndex.js +++ b/src/DocumentIndex.js @@ -35,7 +35,7 @@ class DocumentIndex { } else if (item.payload.op === 'EDIT') { if (this._index[item.payload.key]) { const vals = this._index[item.payload.key].payload.value; - for (const elem in item.payload.value) if (elem in vals) vals[elem] = item.payload.value[elem]; + for (const elem in item.payload.value) if (elem in vals) vals[elem] = item.payload.value[elem] this._index[item.payload.key].value = vals } } From 16047cc1d5eaba71cf328385380e36c801be4911 Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sat, 27 Aug 2022 23:55:03 -0400 Subject: [PATCH 07/15] Update DocumentIndex.js remove unneeded line --- src/DocumentIndex.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/DocumentIndex.js b/src/DocumentIndex.js index 583d011..14bf9f3 100644 --- a/src/DocumentIndex.js +++ b/src/DocumentIndex.js @@ -36,7 +36,6 @@ class DocumentIndex { if (this._index[item.payload.key]) { const vals = this._index[item.payload.key].payload.value; for (const elem in item.payload.value) if (elem in vals) vals[elem] = item.payload.value[elem] - this._index[item.payload.key].value = vals } } } From 63cd8807e1eaea5b0604ed39226f7a16f8513a8b Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sun, 28 Aug 2022 02:04:13 -0400 Subject: [PATCH 08/15] Update DocumentStore.js change arg order --- src/DocumentStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DocumentStore.js b/src/DocumentStore.js index a58d45c..3f0169c 100644 --- a/src/DocumentStore.js +++ b/src/DocumentStore.js @@ -98,7 +98,7 @@ class DocumentStore extends Store { }, options) } - edit (key, options = {}, edits = {}) { + edit (key, edits = {}, options = {}) { if (!this._index.get(key)) { throw new Error(`No entry with key '${key}' in the database`) } return this._addOperation({ From 353cfff7aa6a7ce304ceeaab11540053529508e1 Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sun, 28 Aug 2022 10:13:00 -0400 Subject: [PATCH 09/15] Update DocumentIndex.js Allow new fields/not checking if the field exists. It should be faster and more flexible this way. --- src/DocumentIndex.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DocumentIndex.js b/src/DocumentIndex.js index 14bf9f3..2174033 100644 --- a/src/DocumentIndex.js +++ b/src/DocumentIndex.js @@ -35,7 +35,7 @@ class DocumentIndex { } else if (item.payload.op === 'EDIT') { if (this._index[item.payload.key]) { const vals = this._index[item.payload.key].payload.value; - for (const elem in item.payload.value) if (elem in vals) vals[elem] = item.payload.value[elem] + for (const elem in item.payload.value) vals[elem] = item.payload.value[elem] } } } From 139189bfad4ca33c2ff910db8f4f6fb9fc1bc138 Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sun, 28 Aug 2022 10:29:02 -0400 Subject: [PATCH 10/15] Update DocumentIndex.js optimized --- src/DocumentIndex.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/DocumentIndex.js b/src/DocumentIndex.js index 2174033..4897364 100644 --- a/src/DocumentIndex.js +++ b/src/DocumentIndex.js @@ -34,8 +34,7 @@ class DocumentIndex { delete this._index[item.payload.key] } else if (item.payload.op === 'EDIT') { if (this._index[item.payload.key]) { - const vals = this._index[item.payload.key].payload.value; - for (const elem in item.payload.value) vals[elem] = item.payload.value[elem] + Object.assign(this._index[item.payload.key].payload.value,item.payload.value) } } } From ebb7f2bb969b7d98b822142a32e633fbdca7c7e6 Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sun, 28 Aug 2022 14:20:21 -0400 Subject: [PATCH 11/15] Update DocumentIndex.js proper ordering of edits --- src/DocumentIndex.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/DocumentIndex.js b/src/DocumentIndex.js index 4897364..5ae523a 100644 --- a/src/DocumentIndex.js +++ b/src/DocumentIndex.js @@ -12,10 +12,13 @@ class DocumentIndex { } updateIndex (oplog, onProgressCallback) { - const reducer = (handled, item, idx) => { - if (item.payload.op === 'PUTALL' && item.payload.docs[Symbol.iterator]) { + const values = oplog.values + const handled = {} + 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) { + if (doc && !handled[doc.key]) { handled[doc.key] = true this._index[doc.key] = { payload: { @@ -26,8 +29,7 @@ class DocumentIndex { } } } - } else if (handled[item.payload.key] !== true) { - handled[item.payload.key] = true + } else if (!handled[item.payload.key]) { if (item.payload.op === 'PUT') { this._index[item.payload.key] = item } else if (item.payload.op === 'DEL') { @@ -38,14 +40,10 @@ class DocumentIndex { } } } - if (onProgressCallback) onProgressCallback(item, idx) - return handled + if (onProgressCallback) { + onProgressCallback(item, values.length - i) + } } - - oplog.values - .slice() - .reverse() - .reduce(reducer, {}) } } From 79c4307aab16d6a76accf2e51bb56c40e9f7533d Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sun, 28 Aug 2022 14:32:21 -0400 Subject: [PATCH 12/15] Update DocumentIndex.js --- src/DocumentIndex.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/DocumentIndex.js b/src/DocumentIndex.js index 5ae523a..97cf0a4 100644 --- a/src/DocumentIndex.js +++ b/src/DocumentIndex.js @@ -13,13 +13,11 @@ class DocumentIndex { updateIndex (oplog, onProgressCallback) { const values = oplog.values - const handled = {} 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]) { - handled[doc.key] = true + if (doc) { this._index[doc.key] = { payload: { op: 'PUT', @@ -29,8 +27,7 @@ class DocumentIndex { } } } - } else if (!handled[item.payload.key]) { - 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] @@ -39,7 +36,6 @@ class DocumentIndex { Object.assign(this._index[item.payload.key].payload.value,item.payload.value) } } - } if (onProgressCallback) { onProgressCallback(item, values.length - i) } From d4518ef41c1a492f77d4f05a44ecbdd682a5f906 Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sun, 28 Aug 2022 15:40:22 -0400 Subject: [PATCH 13/15] Update DocumentIndex.js Change opcode name to SET --- src/DocumentIndex.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DocumentIndex.js b/src/DocumentIndex.js index 97cf0a4..c114d2d 100644 --- a/src/DocumentIndex.js +++ b/src/DocumentIndex.js @@ -31,7 +31,7 @@ class DocumentIndex { this._index[item.payload.key] = item } else if (item.payload.op === 'DEL') { delete this._index[item.payload.key] - } else if (item.payload.op === 'EDIT') { + } else if (item.payload.op === 'SET') { if (this._index[item.payload.key]) { Object.assign(this._index[item.payload.key].payload.value,item.payload.value) } From f484f9077c9ba38f166c1b4c59d39877f229ff96 Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sun, 28 Aug 2022 15:40:59 -0400 Subject: [PATCH 14/15] Update DocumentStore.js --- src/DocumentStore.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DocumentStore.js b/src/DocumentStore.js index 3f0169c..3245795 100644 --- a/src/DocumentStore.js +++ b/src/DocumentStore.js @@ -98,11 +98,11 @@ class DocumentStore extends Store { }, options) } - edit (key, edits = {}, 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: 'EDIT', + op: 'SET', key: key, value: edits }, options) From 7422082083c1ac67c803213b0612def3cabc18a3 Mon Sep 17 00:00:00 2001 From: threshold <111914967+threshold862543@users.noreply.github.com> Date: Sun, 28 Aug 2022 15:42:46 -0400 Subject: [PATCH 15/15] Update DocumentStore.js --- src/DocumentStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DocumentStore.js b/src/DocumentStore.js index 3245795..de6144e 100644 --- a/src/DocumentStore.js +++ b/src/DocumentStore.js @@ -98,7 +98,7 @@ class DocumentStore extends Store { }, options) } - SET (key, edits = {}, options = {}) { + set (key, edits = {}, options = {}) { if (!this._index.get(key)) { throw new Error(`No entry with key '${key}' in the database`) } return this._addOperation({