kriszyp / lmdb-js

Simple, efficient, ultra-fast, scalable data store wrapper for LMDB
Other
505 stars 41 forks source link

string / array key questions #276

Closed archivaldo closed 5 months ago

archivaldo commented 7 months ago

Hi there!

I want to use lmdb for a simple key/value project.

my db structure is:

Key: string Value: {}

I've run into a problem: I want to retrieve the last 10 items that were inserted, but it seems that any inserted string key is ordered by alphabetic index.

To address this issue, I've attempted to use an array key [int, string] to preserve the insertion order. However, I'm unable to retrieve any items because the client will query just for the string, not for the int value.

My questions are:

Is there any way to keep the insertion order for a single string key database?

Is there a method to retrieve a value using any of the array member from a key?

kriszyp commented 7 months ago

Is there any way to keep the insertion order for a single string key database? Is there a method to retrieve a value using any of the array member from a key?

No, there is not (at least not efficiently, you could load all the records in the KV store and filter them, but that wouldn't scale well). It sounds like your basic requirement is the be able to efficiently access data by two different properties (the string key and an insertion number), and this is most appropriately done with a secondary index. You would want to have a second KV store that uses insertion number as the key, and a reference back to the primary key in the value, so you could lookup the primary value by the primary key or the secondary insertion index key. You can use openDB to create the secondary KV store as part of the same database to ensure atomic consistency across both your stores as you add values to both.