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

Commit

Permalink
feat: split .query into .query and .queryKeys (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored Apr 15, 2021
1 parent 5aa1202 commit 39ba735
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const esbuild = {
/** @type {import('aegir').PartialOptions} */
module.exports = {
build: {
bundlesizeMax: '65KB'
bundlesizeMax: '67KB'
},
test: {
browser: {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@
},
"homepage": "https://github.com/ipfs/js-datastore-level#readme",
"dependencies": {
"datastore-core": "^3.0.0",
"interface-datastore": "^3.0.6",
"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",
"level": "^6.0.1"
},
"devDependencies": {
Expand Down
87 changes: 60 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -159,41 +163,42 @@ class LevelDatastore extends Adapter {

/**
* @param {Query} q
* @returns {AsyncIterable<Pair>}
*/
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)
Expand All @@ -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
Expand All @@ -214,6 +220,33 @@ class LevelDatastore extends Adapter {

return it
}

/**
* @param {object} opts
* @param {boolean} opts.values
* @param {string} [opts.prefix]
* @returns {AsyncIterable<Pair>}
*/
_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))
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit 39ba735

Please sign in to comment.