git ssb

1+

Dominic / secure-scuttlebutt



Tree: 90ce5321ef13ea1e8baf2789adf6cc2e3beccf13

Files: 90ce5321ef13ea1e8baf2789adf6cc2e3beccf13 / db.js

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

Built with git-ssb-web