Add amount and reverse iterator options to storage iterators

This commit is contained in:
haad 2024-02-16 16:58:06 +01:00
parent 16b3358355
commit 5a73b9cdfb
2 changed files with 6 additions and 4 deletions

View File

@ -76,10 +76,11 @@ const ComposedStorage = async (storage1, storage2) => {
* @memberof module:Storage.Storage-Composed
* @instance
*/
const iterator = async function * () {
const iterator = async function * ({ amount, reverse } = {}) {
const keys = []
const iteratorOptions = { amount: amount || -1, reverse: reverse || false }
for (const storage of [storage1, storage2]) {
for await (const [key, value] of storage.iterator()) {
for await (const [key, value] of storage.iterator(iteratorOptions)) {
if (!keys[key]) {
keys[key] = true
yield [key, value]

View File

@ -78,8 +78,9 @@ const LevelStorage = async ({ path, valueEncoding } = {}) => {
* @memberof module:Storage.Storage-Level
* @instance
*/
const iterator = async function * () {
for await (const [key, value] of db.iterator()) {
const iterator = async function * ({ amount, reverse } = {}) {
const iteratorOptions = { limit: amount || -1, reverse: reverse || false }
for await (const [key, value] of db.iterator(iteratorOptions)) {
yield [key, value]
}
}