Skip to content

Instantly share code, notes, and snippets.

@nodech
Last active November 23, 2017 13:55
Show Gist options
  • Save nodech/ddda93735fefb53a56c9fd1ef347e22c to your computer and use it in GitHub Desktop.
Save nodech/ddda93735fefb53a56c9fd1ef347e22c to your computer and use it in GitHub Desktop.
const Leveldown = require('leveldown');
const async = require('async');
const KEYS = 10000;
const options = {
createIfMissing : true,
errorIfExists : false,
compression : true,
cacheSize : 8 << 20,
writeBufferSize : 4 << 20,
maxOpenFiles : 64,
maxFileSize : 2 << 20,
paranoidChecks : false,
memory : false,
sync : false,
mapSize : 256 * (1024 << 20),
writeMap : false,
noSubdir : true,
bufferKeys : true,
}
const iteratorOptions = {
gte : 0,
lte : KEYS,
gt : null,
lt : null,
keys : true,
values : true,
fillCache : false,
keyAsBuffer : true,
valueAsBuffer : true,
reverse : false,
highWaterMark : 16 * 1024
}
const db = [];
for (let i = 0; i < 10; i++) {
db.push(new Leveldown(`./test-${i}`));
}
const batch = [];
for (let i = 0; i < KEYS; i++) {
batch.push({ type: 'del', key: i});
batch.push({ type: 'put', key: i, value: 'v' + i });
}
async.waterfall([
// simple open/close
db[0].open.bind(db, options),
db[0].close.bind(db),
log.bind(null, 'simple open/close -- api'),
// simple open close direct bindings
db[1].binding.open.bind(db[1].binding, options),
db[1].binding.close.bind(db[1].binding),
log.bind(null, 'simple open/close -- bindings'),
// try to close while writting
db[2].open.bind(db[2], options),
(cb) => {
const puts = [];
for (let i = 0; i < KEYS; i++) {
puts.push(db[2].put.bind(db[2], i, 'v' + i));
}
async.parallel(puts, (err) => {
console.log('Finished parallel puts -- api');
});
// don't wait for puts
cb();
},
db[2].close.bind(db[2]),
log.bind(null, 'write async and close -- api'),
// try to close while writting
db[3].binding.open.bind(db[3].binding, options),
(cb) => {
const puts = [];
for (let i = 0; i < KEYS; i++) {
puts.push(db[3].binding.put.bind(db[3].binding, i, 'v' + i));
}
async.parallel(puts, (err) => {
console.log('Finished parallel puts -- binding');
});
// don't wait for puts
cb();
},
db[3].binding.close.bind(db[3].binding),
log.bind(null, 'write async and close -- binding'),
// try batch putting
db[4].open.bind(db[4], options),
(cb) => {
db[4].batch(batch, log.bind(null, 'batch finished'));
//db[4].batch(batch, cb);
// don't wait for batch
cb();
},
db[4].close.bind(db[4]),
log.bind(null, 'batch write and close -- api'),
// try batch putting
db[5].binding.open.bind(db[5].binding, options),
log.bind(null, 'opened batching -- binding'),
(cb) => {
db[5].binding.batch(batch, log.bind(null, 'batch finished'));
//db[5].binding.batch(batch, cb);
// don't wait for batch
log('batch starteded')
cb();
},
log.bind(null, 'closing batch -- binding'),
db[5].binding.close.bind(db[5].binding),
log.bind(null, 'batch write and close -- binding'),
// test iterators
db[6].open.bind(db[6], options),
(cb) => {
let iterator = db[6].iterator(iteratorOptions);
iterator.next(function process(...args) {
if (args.length === 0) {
console.log('the finished...');
return;
}
if (args[0]) {
console.log('Error:', args[0].message);
return;
}
setImmediate(() => {
iterator.next(process);
})
})
cb();
},
db[6].close.bind(db[6]),
log.bind(null, 'iterator and close'),
db[7].binding.open.bind(db[7].binding, options),
(cb) => {
let iterator = db[7].binding.iterator(iteratorOptions);
iterator.next(function process(...args) {
if (args.length === 0) {
console.log('the finished...');
return;
}
if (args[0]) {
console.log('Binding: Error:', args[0].message);
return;
}
setImmediate(() => {
iterator.next(process);
})
})
cb();
},
db[7].binding.close.bind(db[7].binding),
log.bind(null, 'iterator and close'),
], (err) => {
if (err) {
console.error('!!!! Fail');
console.error(err);
return;
}
console.log('All tests succeed Success');
});
function log(message, cb) {
console.log(message);
if (typeof cb === 'function') {
cb();
} else if (cb) {
console.log(cb)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment