Skip to content

Instantly share code, notes, and snippets.

@joernroeder
Last active August 29, 2015 14:05
Show Gist options
  • Save joernroeder/4ef5d696f27f0f9c5647 to your computer and use it in GitHub Desktop.
Save joernroeder/4ef5d696f27f0f9c5647 to your computer and use it in GitHub Desktop.
node-lmdb // cursor memory leak – The transaction is wrapped in a setImmediate loop
var lmdb = require('./build/Release/node-lmdb');
var env = new lmdb.Env();
env.open({
// Path to the environment
path: "./testdata",
// Maximum number of databases
maxDbs: 10
});
// Ensure that the database is empty
var dbi = env.openDbi({
name: "mydb4",
create: true
});
dbi.drop();
dbi = env.openDbi({
name: "mydb4",
create: true
});
// Write test values
var write = function () {
var txn0 = env.beginTxn();
txn0.putString(dbi, "a", "Helló1");
txn0.putString(dbi, "b", "Hello2");
txn0.putNumber(dbi, "c", 43);
/* key 'd' is omitted intentionally */
txn0.putBinary(dbi, "e", new Buffer("öüóőúéáű"));
txn0.putBoolean(dbi, "f", false);
txn0.putString(dbi, "g", "Hello6");
txn0.commit();
txn0 = null;
console.log("wrote initial values");
}
var printFunc = function(key, data) {
console.log("-----> key:", key);
console.log("-----> data:", data);
}
var runner = function () {
write();
// Begin transaction
var txn = env.beginTxn();
// Create cursor
var cursor = new lmdb.Cursor(txn, dbi);
console.log("first (expected a)");
cursor.goToFirst();
cursor.getCurrentString(printFunc);
console.log("next (expected b)");
cursor.goToNext();
cursor.getCurrentString(printFunc);
console.log("next (expected c)");
cursor.goToNext();
cursor.getCurrentNumber(printFunc);
console.log("next (expected e)");
cursor.goToNext();
cursor.getCurrentBinary(printFunc);
console.log("prev (expected c)");
cursor.goToPrev();
cursor.getCurrentNumber(printFunc);
console.log("last (expected g)");
cursor.goToLast();
cursor.getCurrentString(printFunc);
console.log("prev (expected f)");
cursor.goToPrev();
cursor.getCurrentBoolean(printFunc);
console.log("go to key 'b' (expected b)");
cursor.goToKey('b');
cursor.getCurrentString(printFunc);
console.log("go to range 'd' (expected e)");
cursor.goToRange('d');
cursor.getCurrentBinary(printFunc);
console.log("del (expected f)");
cursor.del();
cursor.getCurrentBoolean(printFunc);
console.log("");
console.log("now iterating through all the keys");
for (var found = cursor.goToFirst(); found; found = cursor.goToNext()) {
console.log("-----> key:", found);
}
// Close cursor
cursor.close();
// Commit transaction
txn.commit();
cursor = null;
txn = null;
// restart the loop
setImmediate(runner);
};
// kick things off
runner();
process.once('exit', function () {
dbi.close();
env.close();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment