Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

API design: getMany(), key & value iterators, iterator.nextv() #380

@vweevers

Description

@vweevers

Jotting down thoughts about various feature requests, ideas and perf improvements - and how APIs for those could fit together.

db.getMany(keys[, options][, callback])

assert(db.supports.getMany)

for (const value of await db.getMany(['a', 'b']) {
  // value may be undefined
}

keyIterator = db.keys([options])

Inherits from AbstractIterator but yields keys instead of entries. Similar to db.createKeyStream() but without the overhead of streams (also in terms of browser bundle size; we might want to move streams to userland because they operate on iterators anyway, and you could potentially choose between readable-stream, core stream, streamx, pull stream).

assert(db.supports.keyIterators)
for await (const key of db.keys({ limit: 2, reverse: true }))

valueIterator = db.values([options])

assert(db.supports.valueIterators)
for await (const value of db.values({ gte: 'foo' }))

iterator.mode

A property by which consumers (like streams) can determine what results to expect. One of entries, keys, values.

iterator.nextv(size[, options][, callback])

Useful for optimized streams, but also just getting X entries. The highWaterMark option (of leveldown) still applies - if either size or hwm is reached then we stop.

const entries = await iterator.nextv(60) // [['key', 'value'], ...]
const keys = await keyIterator.nextv(60) // ['key', ...]
const values = await valueIterator.nextv(60) // ['value', ...]

Options could include end: true to automatically end the iterator.

const first = await iterator.nextv(1, { end: true })[0]

iterator.all([options][, callback])

Same as level-concat-iterator, but highly optimizable. In level-js it can also offer snapshot guarantees when we drop that from (regular use of) iterators. No options, that argument is just reserved.

// Same return type as db.getMany() but operating on a range
const array = await db.values({ gte: 'foo', limit: 1e3 }).all()

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions