Commit f428d5070f3dc01fb29d716b2c4fdf1722204de6
readme
Dominic Tarr committed on 11/29/2016, 2:10:55 PMParent: 2c2005a0ab6c3b6a9d80225999d269debe9a0ec5
Files changed
README.md | changed |
README.md | |||
---|---|---|---|
@@ -2,8 +2,69 @@ | |||
2 | 2 … | ||
3 | 3 … | A flumeview into a reduce function. | |
4 | 4 … | Stream append-only log data into a reduce function to calculate a state. | |
5 | 5 … | ||
6 … | +## Example | ||
7 … | + | ||
8 … | +``` js | ||
9 … | +var FlumeLog = require('flumelog-offset') | ||
10 … | +var codec = require('flumecodec') | ||
11 … | +var Flume = require('flumedb') | ||
12 … | +var Reduce = require('flumeview-reduce') | ||
13 … | + | ||
14 … | +//statistics exports a reduce function that calculates | ||
15 … | +//mean, stdev, etc! | ||
16 … | +var statistics = require('statistics') | ||
17 … | + | ||
18 … | +//initialize a flumelog with a codec. | ||
19 … | +//this example uses flumelog-offset, but any flumelog is valid. | ||
20 … | +var log = FlumeLog(file, 1024*16, codec.json) //use any flume log | ||
21 … | + | ||
22 … | +//attach the reduce function. | ||
23 … | +var db = Flume(log).use('stats', | ||
24 … | + Reduce(1, statistics, function (data) { | ||
25 … | + return data.value | ||
26 … | + }) | ||
27 … | + | ||
28 … | +db.append({value: 1}, function (err) { | ||
29 … | + | ||
30 … | + db.stats.get(function (err, stats) { | ||
31 … | + console.log(stats) // => {mean: 1, stdev: 0, count: 1, sum: 1, ...} | ||
32 … | + }) | ||
33 … | +}) | ||
34 … | +``` | ||
35 … | + | ||
36 … | +## FlumeViewReduce(version, reduce, map?) => FlumeView | ||
37 … | + | ||
38 … | +construct a flumeview from this reduce function. `version` should be a number, | ||
39 … | +and must be provided. If you make a breaking change to either `reduce` or `map` | ||
40 … | +then increment `version` and the view will be rebuilt. | ||
41 … | + | ||
42 … | +`map` is optional. If map is applied, then each item in the log is passed to `map` | ||
43 … | +and then if the returned value is not null, it is passed to reduce. | ||
44 … | + | ||
45 … | +``` js | ||
46 … | +var _data = map(data) | ||
47 … | +if(_data != null) | ||
48 … | + state = reduce(state, map(data)) | ||
49 … | +``` | ||
50 … | + | ||
51 … | +using a `map` function is useful, because it enables efficiently streaming the realtime | ||
52 … | +changes in the state to a remote client. | ||
53 … | + | ||
54 … | +then, pass the flumeview to `db.use(name, flumeview)` | ||
55 … | +and you'll have access to the flumeview methods on `db[name]...` | ||
56 … | + | ||
57 … | +## db[name].get(cb) | ||
58 … | + | ||
59 … | +get the current state of the reduce. This will wait until the view is up to date, if necessary. | ||
60 … | + | ||
61 … | +## db[name].stream({live: boolean}) => PullSource | ||
62 … | + | ||
63 … | +Stream the changing reduce state. for this to work, a map function must be provided. | ||
64 … | + | ||
65 … | +If so, the same reduce function can be used to process the output. | ||
66 … | + | ||
6 | 67 … | ## License | |
7 | 68 … | ||
8 | 69 … | MIT | |
9 | 70 … |
Built with git-ssb-web