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