level-create-batch

1.1.0 • Public • Published

level-create-batch

insert a batch of keys if and only if none of the keys already exist

build status

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.batch([
  { type: 'put', key: 'user!substack', value: { bio: 'beep boop' } },
  { type: 'put', key: 'login!substack', value: { salt: '...', hash: '...' } }
])

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 = require('level');
var db = level('/tmp/users.db', { valueEncoding: 'json' });
var batch = require('level-create-batch');
 
var minimist = require('minimist');
var argv = minimist(process.argv.slice(2), {
    alias: { u: 'user', p: 'pass', b: 'bio' }
});
 
var crypto = require('crypto');
var shasum = require('shasum');
 
var salt = crypto.randomBytes(32);
var hash = shasum(Buffer.concat([ salt, Buffer(argv.pass) ]));
 
var rows = [
    {
        key: 'users!' + argv.user,
        value: { bio: argv.bio }
    },
    {
        key: 'login!' + argv.user,
        value: { salt: salt, hash: hash }
    }
];
 
batch(db, rows, function (err) {
    if (err) {
        console.error(err);
        process.exit(1);
    }
});

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 = require('level-create-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

Package Sidebar

Install

npm i level-create-batch

Weekly Downloads

7

Version

1.1.0

License

MIT

Last publish

Collaborators

  • maxogden