kriszyp / lmdb-js

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

Question: behaviour of dupSort and ordered-binary #99

Closed gogoout closed 2 years ago

gogoout commented 2 years ago

Hi, when running my test, I found dupSort doesn't really unique the key and value pair. See following example:

const db = lmdb.open('/tmp/test/lmdb', {
    dupSort: true,
    encoding: 'ordered-binary'
  });

  console.log(`insert keys...`);
  await db.put('key1', 'value1');
  await db.put('key1', 'value2');
  await db.put('key1', 'value2');

  console.log(`iterate keys...`);
  for (let value of db.getValues('key1')) {
    console.log(value); // 'value1', 'value2'
  }

  console.log(`remove 1 key`);
  await db.remove('key', 'value2'); // only remove the second value under key1
  for (let value of db.getValues('key1')) {
    console.log(value);//  'value1', 'value2'
  }

See even after remove key1, value2, it still get iterated. Is this the intended behaviour? I think when building secondary index(which presumably should be the primary use case for dupSort?) inserting with unique would be much easier, otherwise I think it's worth noted in the doc that user have to ensure the unqiue pair if needed.

kriszyp commented 2 years ago

See even after remove key1, value2, it still get iterated. Is this the intended behaviour?

It should be removed, but you are removing the value from "key", not "key1": await db.remove('key', 'value2'); // only remove the second value under key1 If you adjust that to 'key1' it should work.

I think when building secondary index(which presumably should be the primary use case for dupSort?)

Yes, that is definitely the primary use case. Were you suggesting different behavior for the puts, or just asking about how to remove a key/value from a dupSort table?

gogoout commented 2 years ago

Sorry, stupid typo. And it works, thanks for point out. Just asking about the removal key/pair behaviour. Thanks for the great lib and quick response. Closed now.