From e22dc9be51d7de05cf9f02cd700f92c171de208f Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 15 Apr 2021 11:04:25 +0100 Subject: [PATCH 1/5] feat: split .query into .query and .queryKeys Applies changes from ipfs/interface-datastore/pull/87 Depends on: - [ ] https://github.com/ipfs/interface-datastore/pull/87 - [ ] https://github.com/ipfs/js-datastore-core/pull/59 --- package.json | 7 +++-- src/index.js | 87 ++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 65 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index efc45b2..701535c 100644 --- a/package.json +++ b/package.json @@ -42,8 +42,11 @@ }, "homepage": "https://github.com/ipfs/js-datastore-level#readme", "dependencies": { - "datastore-core": "^3.0.0", - "interface-datastore": "^3.0.3", + "datastore-core": "ipfs/js-datastore-core#feat/split-query-into-query-and-query-keys", + "interface-datastore": "ipfs/interface-datastore#feat/split-query-into-query-and-query-keys", + "it-filter": "^1.0.2", + "it-map": "^1.0.5", + "it-take": "^1.0.1", "level": "^6.0.1" }, "devDependencies": { diff --git a/src/index.js b/src/index.js index 8638c45..19c17b2 100644 --- a/src/index.js +++ b/src/index.js @@ -3,15 +3,19 @@ const { Key, Errors, Adapter, utils: { - filter, map, take, sortAll + sortAll } } = require('interface-datastore') +const filter = require('it-filter') +const map = require('it-map') +const take = require('it-take') /** * @typedef {import('interface-datastore').Datastore} Datastore * @typedef {import('interface-datastore').Pair} Pair * @typedef {import('interface-datastore').Batch} Batch * @typedef {import('interface-datastore').Query} Query + * @typedef {import('interface-datastore').KeyQuery} KeyQuery * @typedef {import('interface-datastore').Options} QueryOptions */ @@ -159,41 +163,42 @@ class LevelDatastore extends Adapter { /** * @param {Query} q - * @returns {AsyncIterable} */ query (q) { - let values = true - if (q.keysOnly != null) { - values = !q.keysOnly + let it = this._query({ + values: true, + prefix: q.prefix + }) + + if (Array.isArray(q.filters)) { + it = q.filters.reduce((it, f) => filter(it, f), it) } - const opts = { - keys: true, - values: values, - keyAsBuffer: true + if (Array.isArray(q.orders)) { + it = q.orders.reduce((it, f) => sortAll(it, f), it) } - // Let the db do the prefix matching - if (q.prefix != null) { - const prefix = q.prefix.toString() - // Match keys greater than or equal to `prefix` and - // @ts-ignore - opts.gte = prefix - // less than `prefix` + \xFF (hex escape sequence) - // @ts-ignore - opts.lt = prefix + '\xFF' + const { offset, limit } = q + if (offset) { + let i = 0 + it = filter(it, () => i++ >= offset) } - let it = levelIteratorToIterator( - this.db.iterator(opts) - ) + if (limit) { + it = take(it, limit) + } - it = map(it, ({ key, value }) => { - if (values) { - return { key, value } - } - return /** @type {Pair} */({ key }) - }) + return it + } + + /** + * @param {KeyQuery} q + */ + queryKeys (q) { + let it = map(this._query({ + values: false, + prefix: q.prefix + }), ({ key }) => key) if (Array.isArray(q.filters)) { it = q.filters.reduce((it, f) => filter(it, f), it) @@ -202,6 +207,7 @@ class LevelDatastore extends Adapter { if (Array.isArray(q.orders)) { it = q.orders.reduce((it, f) => sortAll(it, f), it) } + const { offset, limit } = q if (offset) { let i = 0 @@ -214,6 +220,33 @@ class LevelDatastore extends Adapter { return it } + + /** + * @param {object} opts + * @param {boolean} opts.values + * @param {string} [opts.prefix] + * @returns {AsyncIterable} + */ + _query (opts) { + const iteratorOpts = { + keys: true, + keyAsBuffer: true, + values: opts.values + } + + // Let the db do the prefix matching + if (opts.prefix != null) { + const prefix = opts.prefix.toString() + // Match keys greater than or equal to `prefix` and + // @ts-ignore + iteratorOpts.gte = prefix + // less than `prefix` + \xFF (hex escape sequence) + // @ts-ignore + iteratorOpts.lt = prefix + '\xFF' + } + + return levelIteratorToIterator(this.db.iterator(iteratorOpts)) + } } /** From 632daeed02605c55fd766fc5e693fb17dd41679a Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 15 Apr 2021 11:07:15 +0100 Subject: [PATCH 2/5] chore: linting --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 19c17b2..840c5f7 100644 --- a/src/index.js +++ b/src/index.js @@ -194,7 +194,7 @@ class LevelDatastore extends Adapter { /** * @param {KeyQuery} q */ - queryKeys (q) { + queryKeys (q) { let it = map(this._query({ values: false, prefix: q.prefix From 2a5dc398be066bbc84b6f952f82ba34272bbd2d1 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 15 Apr 2021 11:54:19 +0100 Subject: [PATCH 3/5] chore: update ts config --- tsconfig.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index e605b61..fe3cbd6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,10 +1,10 @@ { - "extends": "./node_modules/aegir/src/config/tsconfig.aegir.json", + "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { "outDir": "dist" }, "include": [ - "test", // remove this line if you don't want to type-check tests + "test", "src" ] } From 022cb92d8bc39ffa300a01b66d2a34bdd2a8e317 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 15 Apr 2021 12:18:06 +0100 Subject: [PATCH 4/5] chore: remove gh urls --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 701535c..7893908 100644 --- a/package.json +++ b/package.json @@ -42,8 +42,8 @@ }, "homepage": "https://github.com/ipfs/js-datastore-level#readme", "dependencies": { - "datastore-core": "ipfs/js-datastore-core#feat/split-query-into-query-and-query-keys", - "interface-datastore": "ipfs/interface-datastore#feat/split-query-into-query-and-query-keys", + "datastore-core": "^4.0.0", + "interface-datastore": "^4.0.0", "it-filter": "^1.0.2", "it-map": "^1.0.5", "it-take": "^1.0.1", From e47d4a6e2a84456f9904cd18042a473fea0876c1 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 15 Apr 2021 12:23:28 +0100 Subject: [PATCH 5/5] chore: increase bundle size --- .aegir.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.aegir.js b/.aegir.js index 75cd7af..378889f 100644 --- a/.aegir.js +++ b/.aegir.js @@ -10,7 +10,7 @@ const esbuild = { /** @type {import('aegir').PartialOptions} */ module.exports = { build: { - bundlesizeMax: '65KB' + bundlesizeMax: '67KB' }, test: { browser: {