Skip to content

Fix okra-idb nodes and entries to handle null transaction OK #3

Open
@garbados

Description

@garbados

The nodes and entries methods on okra-idb's IDBTree contain this worrying comment:

// TODO: fix this

The absence of logic for handling a null transaction means that methods like sync fail, like this:

Error: can only call nodes() from within a managed transaction

I've prototyped a simple fix, but I'm not familiar with the ins and outs of this project enough to add tests and such.

    async *nodes(level, lowerBound = null, upperBound = null, { reverse = false } = {}) {
        if (this.store.txn === null) {
            return this.store.read(() => this.nodes(level, lowerBound, upperBound, { reverse }))
        }
        else {
            yield* super.nodes(level, lowerBound, upperBound, { reverse });
        }
    }
    async *entries(lowerBound = null, upperBound = null, { reverse = false } = {}) {
        if (this.store.txn === null) {
            return this.store.read(() => this.entries(lowerBound, upperBound, { reverse }))
        }
        else {
            for await (const leaf of this.nodes(0, lowerBound ?? { key: null, inclusive: false }, upperBound, { reverse })) {
                assert(leaf.key !== null && leaf.value !== undefined, "invalid leaf entry");
                yield [leaf.key, leaf.value];
            }
        }
    }

That seems to do the trick on my machine, for whatever that's worth. Not sure if there are more complicated caveats lurking in the details.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions