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

If hash is not present, get() returns element at 0th index #42

Open
ashutosh1206 opened this issue Mar 7, 2022 · 0 comments
Open

If hash is not present, get() returns element at 0th index #42

ashutosh1206 opened this issue Mar 7, 2022 · 0 comments

Comments

@ashutosh1206
Copy link

This is the get() method in EventStore:

get (hash) {
    return this.iterator({ gte: hash, limit: 1 }).collect()[0]
}

The get() method indirectly calls _read method:

 _read (ops, hash, amount, inclusive) {
    // Find the index of the gt/lt hash, or start from the beginning of the array if not found
    const index = ops.map((e) => e.hash).indexOf(hash)
    let startIndex = Math.max(index, 0)
    // If gte/lte is set, we include the given hash, if not, start from the next element
    startIndex += inclusive ? 0 : 1
    // Slice the array to its requested size
    const res = ops.slice(startIndex).slice(0, amount)
    return res
  }

If you call this method with a hash (let's say "foobar") that's not present in the EventStore, you'll notice that _read still returns the element at 0th index. We noticed this while using get() method in the FeedStore (https://github.com/orbitdb/orbit-db-feedstore/blob/main/src/FeedStore.js).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant