Counting entries in a level database
prepconcede opened this issue · comments
Sorry if this is a stupid question, but i really need to count database entries. How can I achieve that? Thanks in advance
something like this should work:
let entries = 0
db.createKeyStream().on('data', () => entries++).on('end', () => console.log({ entries }))
or check out https://github.com/voltraco/level-count#readme
Basically you create a stream of all the keys, and count how many entries it emits. There are multiple ways to do the stream objects counting bit, eg:
let entries = 0
for await (const key of db.createKeyStream()) {
entries++
}
console.log({ entries })
Let's not close so fast; it's a good question because level doesn't offer any solution out of the box and that's because the best solution depends on data & app characteristics. E.g.:
- If you don't have a lot of entries, the solution offered by @juliangruber is good
- You can make that slightly faster by using an iterator, removing the overhead of streams
- If you never delete from the db, the keys themselves can be a counter
- If you perform more reads than writes, you could cache the count, to be updated whenever you
put()
ordel()
- If reads are slow on your db (for whatever reason) then you could maintain an index and count that instead
- If you only need an approximate count, then algorithms like hash tables or HyperLogLog could work for you.
Thank you for reopening, you're right that I addressed this naively. I guess this was the solution for my past use cases.