我正在使用以下 Node.JS 插件,它允许您在 Node 应用程序中使用 LMDB: https ://github.com/Venemo/node-lmdb
如您所见,您可以在此处使用游标,但是当我通过游标时,我会得到按键排序的结果,并且我想按时间顺序获取它们,我将第一个插入到最后一个。
无论如何它可以做到吗?
谢谢!
我正在使用以下 Node.JS 插件,它允许您在 Node 应用程序中使用 LMDB: https ://github.com/Venemo/node-lmdb
如您所见,您可以在此处使用游标,但是当我通过游标时,我会得到按键排序的结果,并且我想按时间顺序获取它们,我将第一个插入到最后一个。
无论如何它可以做到吗?
谢谢!
您可以做的是创建一个新数据库,其中键是整数(按时间顺序)。它看起来像这样:
var lmdb = require("node-lmdb");
var env = new lmdb.Env();
env.open({
// Path to the environment
path: "./path/to/db",
// Maximum number of databases
maxDbs: 10
});
var foo = env.openDbi({
name: "foo",
create: true
});
var fooChronological = env.openDbi({
name: "foo-chronological",
create: true,
// This is needed in order to implement chronological sorting
keyIsUint32: true
});
function getMax(txn) {
// This is the number of records we've created so far
var max = txn.getNumber(foo, "max");
// If the max doesn't exist yet, default to 0
if (max == null) {
return 0;
} else {
return max;
}
}
function setMax(txn, max) {
txn.putNumber(foo, "max", max);
}
// Create a new record
function newRecord(txn, key, value) {
var max = getMax(txn);
// Put the record into the foo db
txn.putString(foo, key, value);
// Put the key into the fooChronological db in chronological order
txn.putString(fooChronological, max, key);
// Increment the max
setMax(txn, max + 1);
}
function each(txn, db, f) {
var cursor = new lmdb.Cursor(txn, db);
try {
var found = cursor.goToFirst();
while (found != null) {
cursor.getCurrentString(f);
found = cursor.goToNext();
}
} finally {
cursor.close();
}
}
function eachChronological(txn, f) {
each(txn, fooChronological, function (_, key) {
f(key, txn.getString(foo, key));
});
}
var txn = env.beginTxn();
newRecord(txn, "test", "1");
newRecord(txn, "abc", "2");
newRecord(txn, "z", "3");
newRecord(txn, "cde", "4");
newRecord(txn, "ghc", "5");
newRecord(txn, "foo", "6");
newRecord(txn, "bar", "7");
newRecord(txn, "qux", "8");
newRecord(txn, "corge", "9");
newRecord(txn, "yes", "10");
newRecord(txn, "no", "11");
// This prints it in lexiographical order
each(txn, foo, console.log);
// This prints it in chronological order
eachChronological(txn, console.log);
txn.commit();
还有其他一些方法,例如您可以将按时间顺序排列的整数存储在密钥本身中,那么您只需要一个数据库:
var lmdb = require("node-lmdb");
var env = new lmdb.Env();
env.open({
// Path to the environment
path: "./path/to/db",
// Maximum number of databases
maxDbs: 10
});
var foo = env.openDbi({
name: "foo",
create: true
});
function getMax(txn) {
// This is the number of records we've created so far
var max = txn.getNumber(foo, "max");
// If the max doesn't exist yet, default to 0
if (max == null) {
return 0;
} else {
return max;
}
}
function setMax(txn, max) {
txn.putNumber(foo, "max", max);
}
function padLeft(str, num, fill) {
return new Array(num - str.length + 1).join(fill) + str;
}
// Create a new record
function newRecord(txn, key, value) {
var max = getMax(txn);
// Put the record into the foo db
// The padding length is 10 because 2147483647 is 10 characters long
txn.putString(foo, padLeft("" + max, 10, "0") + key, value);
// Increment the max
setMax(txn, max + 1);
}
function each(txn, db, f) {
var cursor = new lmdb.Cursor(txn, db);
try {
var found = cursor.goToFirst();
while (found != null) {
cursor.getCurrentString(f);
found = cursor.goToNext();
}
} finally {
cursor.close();
}
}
var txn = env.beginTxn();
newRecord(txn, "test", "1");
newRecord(txn, "abc", "2");
newRecord(txn, "z", "3");
newRecord(txn, "cde", "4");
newRecord(txn, "ghc", "5");
newRecord(txn, "foo", "6");
newRecord(txn, "bar", "7");
newRecord(txn, "qux", "8");
newRecord(txn, "corge", "9");
newRecord(txn, "yes", "10");
newRecord(txn, "no", "11");
// This prints it in chronological order
each(txn, foo, function (key, value) {
if (key !== "max") {
console.log({ index: +key.slice(0, 10), key: key.slice(10), value: value });
}
});
txn.commit();
通常,如果您想要不同的排序顺序,那么您将需要以某种方式操作密钥。