LMDB光标按时间顺序而不是按字母顺序移动-Node.JS插件

LMDB光标按时间顺序而不是按字母顺序移动-Node.JS插件,node.js,plugins,lmdb,Node.js,Plugins,Lmdb,我正在使用以下Node.JS插件,允许您在节点应用程序中使用LMDB: 正如你所看到的,你可以在这里使用游标,但是当我通过游标时,我会得到按键排序的结果,我想按时间顺序得到它们,从第一个到最后一个 有什么办法可以做到吗 谢谢 您可以创建一个新数据库,其中键是整数(按时间顺序)。它看起来像这样: var lmdb = require("node-lmdb"); var env = new lmdb.Env(); env.open({ // Path to the environment

我正在使用以下Node.JS插件,允许您在节点应用程序中使用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();
通常,如果需要不同的排序顺序,则需要以某种方式操纵键