You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It looks like there are two bugs with the iterator, first off the example gives a false sense of the data type that is returned.
iter.next() returns an array of even length, not just an array with two items. They can be larger, and will have look like value1, key1, ...., valuen, keyn]
This code snippet shows what actually works
constdb=newLevelDB(dbPath)awaitdb.open()constiter=db.getIterator({keys: true,values: true})while(entries=awaititer.next()){for(leti=0;i<entries.length;i=i+2){constentry={key: entries[i+1],value: entries[i]}// you now have a key/value pair you can act on}}awaitdb.close()
This is the data type regardless of if you pass { keys: false } or { values: false }, all that does is leave empty Buffers (or strings if you asked for them) in those places in the entries array.
Second, the last set of entries is always discarded and is never returned from the iterator. This line is where that bug is:
Even if the iterator returned val.finished = true, val.array may still have the last set of data in it and that should be returned. A subsequent call to the iterator will then return an empty val.array and of course finished will still be true.
The fix is to always return val.array. I don't think it can ever be empty. The last array of data will return with finished = true which will set this.finished in the iterator, and subsequent calls will short out on this line:
Thanks for the report, looks like the cache logic was not ported correctly. Can you verify if #9 fixes?
As for the final check, should be fixed: if the DB is empty, per the C++ bindings we'd return empty array [] instead of going through the finished check, so it does need to be there. It looks like a similar issue is present for 1-entry DBs. So this should be addressed on the C++ side, but I think handling on the JS side is OK for now.
It looks like there are two bugs with the iterator, first off the example gives a false sense of the data type that is returned.
iter.next()
returns an array of even length, not just an array with two items. They can be larger, and will have look likevalue1, key1, ...., valuen, keyn]
This code snippet shows what actually works
This is the data type regardless of if you pass
{ keys: false }
or{ values: false }
, all that does is leave empty Buffers (or strings if you asked for them) in those places in the entries array.Second, the last set of entries is always discarded and is never returned from the iterator. This line is where that bug is:
node-leveldb-zlib/ts/iterator.ts
Line 45 in 2c15e48
Even if the iterator returned
val.finished = true
,val.array
may still have the last set of data in it and that should be returned. A subsequent call to the iterator will then return an emptyval.array
and of coursefinished
will still be true.The fix is to always return val.array. I don't think it can ever be empty. The last array of data will return with
finished = true
which will setthis.finished
in the iterator, and subsequent calls will short out on this line:node-leveldb-zlib/ts/iterator.ts
Line 41 in 2c15e48
The text was updated successfully, but these errors were encountered: