Files: 612fe50d680268eabc44b1146954eb2bff79eb31 / 5_flume_view_reading.js
1056 bytesRaw
1 | var Flume = require('flumedb') |
2 | var FlumeLog = require('flumelog-offset') |
3 | var codec = require('flumecodec') |
4 | var pull = require('pull-stream') |
5 | |
6 | var FlumeView = require('flumeview-reduce') |
7 | |
8 | var log = FlumeLog('demo_log', { codec: codec.json }) |
9 | |
10 | var db = Flume(log) |
11 | .use('stats', FlumeView( |
12 | 2, //version |
13 | (acc, val) => { // reducer, |
14 | console.log('+') |
15 | return { |
16 | sum: acc.sum + val, |
17 | squareSum: acc.squareSum + val*val |
18 | } |
19 | }, |
20 | (msg) => msg.count, // map, |
21 | null, // codec |
22 | { |
23 | sum: 0, |
24 | squareSum: 0 |
25 | } //initial |
26 | )) |
27 | // .use('someOtherView', otherView) |
28 | |
29 | // writing pull-stream |
30 | pull( |
31 | pull.values([1,2,3,4,5,6,7]), |
32 | pull.asyncMap((val, cb) => setTimeout( |
33 | () => cb(null, val), |
34 | 500 |
35 | )), |
36 | pull.drain( |
37 | val => db.append({ count: val }, (err, seq) => { |
38 | console.log(`FlumeDB.append: appended '${val}' at offset of ${seq} bytes`) |
39 | db.stats.get((err, val) => console.log('the view state:', err, val)) |
40 | }), |
41 | () => { |
42 | console.log('DONE: append stream') |
43 | } |
44 | ) |
45 | ) |
46 | |
47 |
Built with git-ssb-web