我正在使用以下Node.JS插件,它允许您在Node应用程序中使用LMDB: https://github.com/Venemo/node-lmdb
正如你所看到的,你可以在这里使用游标,但是当我浏览游标时,我得到了按键排序的结果,我希望按时间顺序得到它们,这是我第一次进入游标。
无论如何可以做到吗?
谢谢!
答案 0 :(得分:0)
您可以做的是创建一个新的数据库,其中键是整数(按时间顺序)。它看起来像这样:
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();
一般情况下,如果您需要不同的排序顺序,则需要以某种方式操作密钥。