git ssb

1+

Dominic / secure-scuttlebutt



Tree: b4df1e66bea6275126d6d9c508356dc44e62b504

Files: b4df1e66bea6275126d6d9c508356dc44e62b504 / db.js

1557 bytesRaw
1var path = require('path')
2var ViewLevel = require('flumeview-level')
3var ViewHashTable = require('flumeview-hashtable')
4
5module.exports = function (dir, keys) {
6 var db = require('./minimal')(dir)
7
8 .use('keys', ViewHashTable(1, function (key) {
9 var b = new Buffer(key.substring(1,7), 'base64').readUInt32BE(0)
10 return b
11 })
12 )
13 .use('clock', require('./indexes/clock')())
14 .use('feed', require('./indexes/feed')())
15 .use('links', require('./indexes/links')(keys))
16 .use('time', ViewLevel(1, function (data) {
17 return [data.timestamp]
18 }))
19
20 db.progress = {}
21 var prog = db.progress.indexes = {start: 0, current: 0, target: 0}
22 var ts = Date.now()
23
24 db.since(function () {
25 prog.target = db.since.value
26 if(Date.now() > ts + 100)
27 update()
28 })
29
30 function update () {
31 ts = Date.now()
32 //iterate over the current views, so we capture plugins
33 //as well as the built ins.
34 var current = 0, n = 0
35 for(var k in db)
36 if(db[k] && 'function' === typeof db[k].since) {
37 n++
38 var c = db[k].since.value
39 current += (Number.isInteger(c) ? c : -1)
40 }
41 prog.current = ~~(current / n)
42 //if the progress bar is complete, move the starting point
43 //up to the current position!
44 if(prog.start <= 0)
45 prog.start = prog.current
46 else if(prog.current == prog.target)
47 prog.start = prog.target
48
49 }
50
51 // unref is only available when running inside node
52 var timer = setInterval(update, 200)
53 timer.unref && timer.unref()
54
55 return db
56}
57
58

Built with git-ssb-web