From 44b9e5f9e8a91ba914905e5fcba256f16e657597 Mon Sep 17 00:00:00 2001 From: utanapishtim Date: Thu, 12 May 2022 13:22:06 -0700 Subject: [PATCH 1/2] changes put to return key and value passed --- index.js | 7 +++++-- test/all.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 3cf0cf38..3eef1044 100644 --- a/index.js +++ b/index.js @@ -761,6 +761,9 @@ class Batch { } _append (root, seq, key, value) { + const ret = { seq, key: null, value: null } + if (key) ret.key = (this.keyEncoding) ? this.keyEncoding.decode(key) : key + if (value) ret.value = (this.valueEncoding) ? this.valueEncoding.decode(value) : value const index = [] root.indexChanges(index, seq) index[0] = new Child(seq, 0, root) @@ -771,14 +774,14 @@ class Batch { this.root = root this.length++ this.blocks.set(seq, block) - return + return ret } return this._appendBatch(Node.encode({ key, value, index: deflate(index) - })) + })).then(() => ret) } async _appendBatch (raw) { diff --git a/test/all.js b/test/all.js index 62febede..f1fd51ba 100644 --- a/test/all.js +++ b/test/all.js @@ -407,3 +407,44 @@ tape('feed is unwrapped in getter', async t => { t.same(feed, db.feed) t.end() }) + +tape('put returns key & value', async t => { + const tests = [ + { + opts: {}, + key: Buffer.from('key'), + value: Buffer.from('value'), + t: (result, key, value) => { + t.equals(Buffer.compare(result.key, key), 0) + t.equals(Buffer.compare(result.value, value), 0) + } + }, + { + opts: { keyEncoding: 'utf8', valueEncoding: 'utf8' }, + key: 'key', + value: 'value', + t: (result, key, value) => { + t.equals(result.key, key) + t.equals(result.value, value) + } + }, + { + opts: { keyEncoding: 'utf8', valueEncoding: 'json' }, + key: 'key', + value: { value: 'value' }, + t: (result, key, value) => { + t.equals(result.key, key) + t.deepEquals(result.value, value) + } + } + ] + const Hypercore = require('hypercore') + for (const test of tests) { + const feed = new Hypercore(require('random-access-memory')) + const db = new Hyperbee(feed, test.opts) + await db.ready() + const result = await db.put(test.key, test.value) + test.t(result, test.key, test.value) + } + t.end() +}) From ce7656e9008ea668674b21abd0d1a0e000d3db79 Mon Sep 17 00:00:00 2001 From: utanapishtim Date: Mon, 16 May 2022 22:57:46 -0700 Subject: [PATCH 2/2] refactor for pr --- test/all.js | 59 ++++++++++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/test/all.js b/test/all.js index f1fd51ba..8c08e697 100644 --- a/test/all.js +++ b/test/all.js @@ -409,42 +409,27 @@ tape('feed is unwrapped in getter', async t => { }) tape('put returns key & value', async t => { - const tests = [ - { - opts: {}, - key: Buffer.from('key'), - value: Buffer.from('value'), - t: (result, key, value) => { - t.equals(Buffer.compare(result.key, key), 0) - t.equals(Buffer.compare(result.value, value), 0) - } - }, - { - opts: { keyEncoding: 'utf8', valueEncoding: 'utf8' }, - key: 'key', - value: 'value', - t: (result, key, value) => { - t.equals(result.key, key) - t.equals(result.value, value) - } - }, - { - opts: { keyEncoding: 'utf8', valueEncoding: 'json' }, - key: 'key', - value: { value: 'value' }, - t: (result, key, value) => { - t.equals(result.key, key) - t.deepEquals(result.value, value) - } - } - ] - const Hypercore = require('hypercore') - for (const test of tests) { - const feed = new Hypercore(require('random-access-memory')) - const db = new Hyperbee(feed, test.opts) - await db.ready() - const result = await db.put(test.key, test.value) - test.t(result, test.key, test.value) - } + + const db0 = create({ keyEncoding: 'utf8', valueEncoding: 'json' }) + const k0 = 'key' + const v0 = { value: 'value' } + const res0 = await db0.put(k0, v0) + t.equals(res0.key, k0) + t.deepEquals(res0.value, v0) + + const db1 = create({ keyEncoding: 'utf8', valueEncoding: 'utf8' }) + const k1 = 'key' + const v1 = 'value' + const res1 = await db1.put(k1, v1) + t.equals(res1.key, k1) + t.equals(res1.value, v1) + + const db2 = create({ keyEncoding: 'binary', valueEncoding: 'binary' }) + const k2 = Buffer.from('key') + const v2 = Buffer.from('value') + const res2 = await db2.put(k2, v2) + t.equals(Buffer.compare(res2.key, k2), 0) + t.equals(Buffer.compare(res2.value, v2), 0) + t.end() })