From ac8052dde6fa5c9263101ee97e63af7d4949f3a2 Mon Sep 17 00:00:00 2001 From: CSDUMMI <31551856+CSDUMMI@users.noreply.github.com> Date: Sun, 3 Oct 2021 16:27:06 +0200 Subject: [PATCH 1/2] Added `applyEntry` function to Index. --- src/KeyValueIndex.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/KeyValueIndex.js b/src/KeyValueIndex.js index a7520bd..1b2981a 100644 --- a/src/KeyValueIndex.js +++ b/src/KeyValueIndex.js @@ -8,6 +8,14 @@ class KeyValueIndex { get(key) { return this._index[key] } + + applyEntry(entry) { + if(item.payload.op === "PUT") { + this._index[entry.payload.key] = item.payload.value + } else if(item.payload.op === "DEL") { + delete this._index[item.payload.key] + } + } updateIndex(oplog) { oplog.values @@ -16,11 +24,7 @@ class KeyValueIndex { .reduce((handled, item) => { if(!handled.includes(item.payload.key)) { handled.push(item.payload.key) - if(item.payload.op === 'PUT') { - this._index[item.payload.key] = item.payload.value - } else if(item.payload.op === 'DEL') { - delete this._index[item.payload.key] - } + this.applyEntry(item) } return handled }, []) From 8b76301c194edeb1ae99c3f2e8c0c80d284f1362 Mon Sep 17 00:00:00 2001 From: CSDUMMI <31551856+CSDUMMI@users.noreply.github.com> Date: Thu, 7 Oct 2021 17:57:02 +0200 Subject: [PATCH 2/2] Using more helper functions --- src/KeyValueIndex.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/KeyValueIndex.js b/src/KeyValueIndex.js index 1b2981a..777d760 100644 --- a/src/KeyValueIndex.js +++ b/src/KeyValueIndex.js @@ -9,11 +9,22 @@ class KeyValueIndex { return this._index[key] } + put(entry) { + this._index[entry.payload.key] = item.payload.value + } + + del(entry) { + delete this._index[item.payload.key] + } + applyEntry(entry) { - if(item.payload.op === "PUT") { - this._index[entry.payload.key] = item.payload.value - } else if(item.payload.op === "DEL") { - delete this._index[item.payload.key] + switch(entry.payload.op) { + case "PUT": + this.put(entry) + break + case "DEL": + this.del(entry) + break } }