Closed alexghr closed 11 months ago
keys which are Buffers (this should be supported right?)
No, Buffers are not supported for round-trip type-preserving with ordered-binary. lmdb-js allows you to provide buffers as keys, but it assumes these buffers are primitives values that already encoded with the ordered-binary format (this is primarily provided and useful if you want to encode a key one time, and use it many times). For example, these result in the same entry:
test.put(42, 'value for 42');
// and
const buf = lmdb.keyValueToBuffer(42);
test.put(buf, 'value for 42');
The first example is much faster for one time use (it avoids buffer creation), whereas the second one would be faster if you were encoding 42 to a buffer once and using that same encoding of 42 many times (more than 10x).
However, if you want to always provide a buffer and get a buffer back (from getRange), you should use keyEncoding: 'binary'
. Also, worth noting that the keyEncoding: 'uint32'
is also available for compact/efficient encoding of 32-bit integers (would be faster than using numToBuf
for each put
since it avoids buffer creation).
it assumes these buffers are primitives values that already encoded with the ordered-binary format
Oh, that's not how I read the documentation at all. The example in the readme makes it sound like buffers are just supported alongside any primitive JS value. Thanks for clarification, I'll update my code
I'll close this issue as answered.
I'm using a database with key encoding set to
ordered-binary
where I'm storing data against keys which are Buffers (this should be supported right?).I need to perform
getRange
queries on this data and I need both the keys and values. The keys I get back from the iterator are using their internal representation though, instead of the original key.The actual keys I'm using in my code are arrays that look like
["some prefix", <buffer>]
but I've simplified the code to just individual Buffers here:The output:
The values are returned in order, but the keys are completely off (and the element counts aren't consistent either). I've tried the mapping functions exported from the
lmdb
package but couldn't get the original key backIs there any way I can get the original key as part of the iteration or should I store the key with the object?
I've got installed lmdb v2.9.1 and ordered-binary v1.4.1