git ssb

1+

Dominic / secure-scuttlebutt



Commit 08d58af752cf82ca1090ab7c5b9949afecbc3fdb

move lastDB into async index

Dominic Tarr committed on 8/31/2016, 6:34:01 AM
Parent: 7873256511d8e3260f4c04ca6f3506d2600b19e9

Files changed

index.jschanged
indexes/last.jsadded
index.jsView
@@ -59,9 +59,9 @@
5959 var sysDB = db.sublevel('sys')
6060 var logDB = db.sublevel('log')
6161 var feedDB = require('./indexes/feed')(db)
6262 var clockDB = require('./indexes/clock')(db)
63- var lastDB = db.sublevel('lst')
63 + var lastDB = require('./indexes/last')(db)
6464 var indexDB = db.sublevel('idx')
6565 var appsDB = db.sublevel('app')
6666
6767 function get (db, key) {
@@ -73,15 +73,21 @@
7373 db.add = Validator(db)
7474
7575 var realtime = Notify()
7676
77- db.seen = 0
77 + var await = u.await()
78 + var set = await.set
79 + await.set = null
80 + var waiting = []
81 + db.seen = await
7882 db.post(function (op) {
79- db.seen = op.ts || op.timestamp
83 + set(Math.max(op.ts || op.timestamp, await.get()||0))
8084 })
85 +
8186 peek.last(logDB, {keys: true}, function (err, key) {
82- db.seen = key
87 + set(Math.max(key || 0, await.get()||0))
8388 })
89 +
8490 db.pre(function (op, _add, _batch) {
8591 var msg = op.value
8692 var id = op.key
8793 // index by sequence number
@@ -93,19 +99,9 @@
9399 }
94100
95101 var localtime = op.timestamp = timestamp()
96102
97- // index the latest message from each author
98103 add({
99- key: msg.author, value: {sequence: msg.sequence, ts: localtime },
100- type: 'put', prefix: lastDB
101- })
102-
103- // index messages in the order _received_
104- // this will be used to pass to plugins which
105- // must create their indexes asyncly.
106-
107- add({
108104 key: localtime, value: id,
109105 type: 'put', prefix: logDB
110106 })
111107
@@ -197,26 +193,16 @@
197193 function toSeq (latest) {
198194 return isNumber(latest) ? latest : latest.sequence
199195 }
200196
201- db.latest = function (opts) {
202- return pull(
203- pl.read(lastDB, opts),
204- pull.map(function (data) {
205- var d = {id: data.key, sequence: toSeq(data.value), ts: data.value.ts }
206- return d
207- })
208- )
209- }
210-
211197 function lookup(keys, values) {
212198 return paramap(function (key, cb) {
213199 if(key.sync) return cb(null, key)
214200 if(!values) return cb(null, key)
215201 db.get(key, function (err, msg) {
216202 if (err) cb(err)
217203 else {
218- cb(null, msgFmt(keys, values, { key: key, value: msg }))
204 + cb(null, u.format(keys, values, { key: key, value: msg }))
219205 }
220206 })
221207 })
222208 }
@@ -246,8 +232,10 @@
246232 keys = opts.keys.generate()
247233 return createFeed(db, keys, opts)
248234 }
249235
236 + db.latest = lastDB.latest
237 +
250238 db.latestSequence = function (id, cb) {
251239 lastDB.get(id, cb)
252240 }
253241
@@ -489,11 +477,12 @@
489477
490478 var _close = db.close
491479
492480 db.close = function (cb) {
493- var n = 3
481 + var n = 4
494482 clockDB.close(next)
495483 feedDB.close(next)
484 + lastDB.close(next)
496485 _close.call(db, next)
497486 function next (err) {
498487 if(n < 0) return
499488 if(err) return n = -1, cb(err)
@@ -504,6 +493,4 @@
504493
505494 return db
506495 }
507496
508-
509-
indexes/last.jsView
@@ -1,0 +1,45 @@
1 +var Follower = require('../follower')
2 +var pull = require('pull-stream')
3 +var path = require('path')
4 +var ltgt = require('ltgt')
5 +var u = require('../util')
6 +
7 +function isNumber (n) {
8 + return typeof n === 'number'
9 +}
10 +
11 +function toSeq (latest) {
12 + return isNumber(latest) ? latest : latest.sequence
13 +}
14 +
15 +module.exports = function (db) {
16 + var indexPath = path.join(db.location, 'last')
17 + var index = Follower(db, indexPath, 1, function (data) {
18 + if(data.sync) return
19 + console.log({
20 + key: data.value.author, value: {sequence: data.value.sequence, ts: data.timestamp },
21 + type: 'put'
22 + })
23 + return {
24 + key: data.value.author, value: {sequence: data.value.sequence, ts: data.timestamp },
25 + type: 'put'
26 + }
27 + })
28 +
29 + index.latest = function (opts) {
30 + opts = opts || {}
31 + if(!(opts.gt || opts.gte))
32 + opts.gt = '\x00'
33 + return pull(
34 + index.read(opts),
35 + pull.map(function (data) {
36 + var d = {id: data.key, sequence: toSeq(data.value), ts: data.value.ts }
37 + return d
38 + })
39 + )
40 + }
41 +
42 + return index
43 +
44 +}
45 +

Built with git-ssb-web