Files: f166b2d9af67a06ce2412e01babeb6fe10a9496d / store.js
1268 bytesRaw
1 | var Flume = require('flumedb') |
2 | var OffsetLog = require('flumelog-offset') |
3 | var mkdirp = require('mkdirp') |
4 | var ViewHashTable = require('flumeview-hashtable') |
5 | |
6 | var codec = require('flumecodec/json') |
7 | var path = require('path') |
8 | var hash = require('ssb-keys/util').hash |
9 | |
10 | function getId(msg) { |
11 | return '%'+hash(JSON.stringify(msg, null, 2)) |
12 | } |
13 | |
14 | module.exports = function (config) { |
15 | //we'll store out of order messages in their own log |
16 | //so that we don't interfere with the views on in in-order messages |
17 | //it does mean we'll download them again later if we follow |
18 | //this feed, but it makes everything simpler overall |
19 | //and the point of messages is to be small |
20 | //and the point of ooo messages is that there |
21 | //is not really that many. |
22 | |
23 | var log = OffsetLog( |
24 | path.join(config.path, 'ooo', 'log.offset'), |
25 | {blockSize:1024*16, codec:codec} |
26 | ) |
27 | var store = Flume(log) |
28 | .use('keys', ViewHashTable(2, function (key) { |
29 | var b = new Buffer(key.substring(1,7), 'base64').readUInt32BE(0) |
30 | return b |
31 | })) |
32 | |
33 | store.add = function (msg, cb) { |
34 | var data = { |
35 | key: getId(msg), |
36 | value: msg, |
37 | timestamp: Date.now() |
38 | } |
39 | store.append(data, function (err) { |
40 | if(err) cb(err) |
41 | else cb(null, data) |
42 | }) |
43 | } |
44 | |
45 | return store |
46 | } |
47 |
Built with git-ssb-web