kriszyp / lmdb-js

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

Is calling `open` twice with 2 unique names but 1 path the same as openDB? #261

Closed TimDaub closed 1 year ago

TimDaub commented 1 year ago

Hey,

for a while I have used open({name: "a", path: "./data"}) and then in another module, I have conveniently used open({name: "b", path: "./data"}). I've now recently run into an issue with atomicity because a storage operation of db_a and db_b rely on each other. So I read more on transactions and atomicity using openDB (https://github.com/kriszyp/lmdb-js#dbopendbdatabase-stringnamestring) and it is exactly what I'd need. However, for now I've been using open with two different names but the same path. So will I have to convert one to the other or is "open" and "openDB" interchangeable in this case, as long as its called with the same path?

kriszyp commented 1 year ago

They are different in that open() is called as a direction function, whereas openDB() is a method on the database returned from open(). It is probably easiest to do:

root = open({path: './data'});
dbA = root.openDB('a');
dbB = root.openDB('b'); 
TimDaub commented 1 year ago

OK, but say I call open twice with the same path but different name and I then do

dbA.transaction(() => { dbA.put(…); dbB.put(…); });

Is there now a guarantee that values for dbA and dbB are written atomically? Or does that only work if I open the connection like you showed it and then call root.transaction(…)?

kriszyp commented 1 year ago

No, that would only work if they were opened from the same database. Note, that another way you could do this is:

dbA= open({name: "a", path: './data'});
dbB = root.openDB('b'); 
TimDaub commented 1 year ago

ok great thanks!