Skip to content

Instantly share code, notes, and snippets.

@micahscopes
Created January 21, 2020 02:00
Show Gist options
  • Save micahscopes/c705dc5f478adb229c8aae5eea3b992c to your computer and use it in GitHub Desktop.
Save micahscopes/c705dc5f478adb229c8aae5eea3b992c to your computer and use it in GitHub Desktop.
Rewind hypertrie
const ram = require("random-access-memory");
const hypertrie = require("hypertrie");
const db = hypertrie(ram);
let xItems = ["little", "bits", "of", "info"].map(
item => ({
type: "put",
key: "/x",
value: item
})
);
let yItems = ["1", "2", "3", "4"].map(item => ({
type: "put",
key: "/x/y",
value: item
}));
db.batch(xItems.concat(yItems), function() {
let history = db.history({ reverse: true });
for (let i = db.version; i--; i > 1) {
history.next((_, obj) => {
if (obj && obj.key === "x") {
console.log(
obj.seq,
obj.key,
obj.value.toString(),
obj.hash.toString("hex")
);
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment