level-create-batch
insert a batch of keys if and only if none of the keys already exist
example
Suppose you want to create a user account for a website using leveldb.
First you'll need to make sure that the requested username hasn't already been
taken. If you do a db.get()
to check the username without
locking the key first, if two requests
come in to register the same username before any data is written, both requests
will indicate that the username is available and the database will be left in an
inconsistent state.
You can solve the problem of multiple simultaneous create requests for single
keys using level-create, but now
suppose that when a user creates an account, you also want to create a username
and password in a separate key. Ordinarily in leveldb you could do this with
db.batch
:
db
db.batch()
is atomic, so if the server crashed between inserting the first key
and the second, both keys would not be written. Otherwise, a user might be
accidentally created who couldn't log in, or a login might be created for a user
that didn't exist.
We lose the atomicity benefits of db.batch()
when using individual key locks
with level-create, but with
level-create-batch, we can do safe multi-record inserts if and only if all the
requested keys do not yet exist:
var level = ;var db = ;var batch = ;var minimist = ;var argv =;var crypto = ;var shasum = ;var salt = crypto;var hash = ;var rows =key: 'users!' + argvuservalue: bio: argvbiokey: 'login!' + argvuservalue: salt: salt hash: hash;;
Now we can use this program to create accounts, but the program will properly fail to create an account if the username is already taken:
$ node useradd.js -u substack -p beepboop -b whatever
$ node useradd.js -u trex -p dino -b rawr
$ node useradd.js -u substack -p hax -b h4x3d
{ [Error: key already exists] code: 'EXISTS' }
methods
var batch =
batch(db, rows, cb)
Insert a batch of rows
into the leveldb database db
.
cb(err)
fires with any errors when the batch finishes or is aborted.
Aside from ordinary database errors, err.code
might be:
- 'LOCKED' - when any of the requested keys are locked
- 'EXISTS' - when any of the requested keys already exists
If a row has a type
property explicitly set to 'put'
, the data will be
inserted whether or not a key already exists. The keys will all still be locked
in any case until the operation finishes.
Otherwise the default row type is 'create'
, which fails if a key already
exists.
Rows can individually have keyEncoding
and valueEncoding
properties, which
db.batch()
respects.
install
With npm do:
npm install level-create-batch
license
MIT