Created
October 25, 2016 00:11
-
-
Save hackergrrl/2eb32b718e2a97f98a9892e46b4a45cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hyperlog = require('./') | |
var level = require('level') | |
var through = require('through2') | |
var memdb = require('memdb') | |
var TopoSort = require('topo-sort'); | |
var dedupe = require('dedupe') | |
if (process.argv.length !== 4) { | |
console.error('USAGE: rebuild <LEVEL-DIR> <NEW-LEVEL-DIR>') | |
process.exit(1) | |
} | |
var db = level(process.argv[2]) | |
var log = hyperlog(db) | |
var newlog = hyperlog(level(process.argv[3])) | |
var nodes = {} | |
var tsort = new TopoSort(); | |
var total = 0 | |
log.createReadStream().pipe(through.obj(function (node, enc, next) { | |
nodes[node.key] = node | |
total++ | |
tsort.add(node.key, node.links) | |
next() | |
}, done)) | |
function done () { | |
var res = dedupe(tsort.sort().reverse()) | |
console.log('done sorting', res.length, total) | |
var inserted = 0 | |
insert(res.shift(), function (err) { | |
if (err) throw err | |
console.log('done') | |
}) | |
function insert (key, cb) { | |
if (!key) return cb() | |
log.get(key, function (err, node) { | |
if (err) throw err | |
newlog.add(node.links, node.value, function (err) { | |
inserted++ | |
console.log('inserted', inserted, '/', total) | |
if (err) return cb(err) | |
insert(res.shift(), cb) | |
}) | |
}) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment