-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbuild-index.js
45 lines (40 loc) · 1.31 KB
/
build-index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var through = require('through2')
var path = require('path')
var level = require('level')
var varint = require('varint')
var peerwiki = require('./')
var i = 0
var db = level(path.join(__dirname, 'index.db'))
var wiki = peerwiki()
wiki.on('ready', function() {
wiki.createEntryPointerStream()
.pipe(through.obj(function(entry, enc, cb) {
wiki.readDirectoryEntry(entry, cb)
}))
.pipe(through.obj(function(entry, enc, cb) {
var read = function(err, entry) {
if (err) return cb(err)
if (entry.redirect !== undefined && entry.index === entry.redirect) return cb()
if (entry.cluster === undefined) return cb()
if (entry.index === entry.redirect) return cb()
wiki.readCluster({index:entry.cluster, blobs:false}, function(err, cluster) {
if (err) return cb(err)
cb(null, {
key: entry.url,
value: Buffer.concat([new Buffer(varint.encode(cluster.offset)), new Buffer(varint.encode(entry.blob))])
})
})
}
read(null, entry)
}))
.pipe(through.obj(function(data, enc, cb) {
console.log('Indexed #%d %s', i++, data.key)
db.put(data.key, data.value, function(err) {
if (err) return cb(err)
cb()
})
}))
.on('finish', function() {
process.exit(0)
})
})