# flumeview-reduce

> flumeview as a reduce function

Latest version **1.4.0** (published 2021-01-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install flumeview-reduce
pnpm add flumeview-reduce
yarn add flumeview-reduce
bun add flumeview-reduce
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.0 |
| Published | 2021-01-11 |
| First published | 2016-11-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 22.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | 'Dominic Tarr' |
| Maintainers | dominictarr, arj03, christianbundy, regular |

## Links

- npm: https://www.npmjs.com/package/flumeview-reduce
- Repository: https://github.com/flumedb/flumeview-reduce
- Issues: https://github.com/flumedb/flumeview-reduce/issues
- npm.io page: https://npm.io/package/flumeview-reduce

## Dependencies (7)

- [obv](https://npm.io/package/obv.md) 0.0.1
- [deep-equal](https://npm.io/package/deep-equal.md) ^1.0.1
- [flumecodec](https://npm.io/package/flumecodec.md) 0.0.0
- [atomic-file](https://npm.io/package/atomic-file.md) ^2.0.0
- [pull-notify](https://npm.io/package/pull-notify.md) ^0.1.1
- [pull-stream](https://npm.io/package/pull-stream.md) ^3.5.0
- [async-single](https://npm.io/package/async-single.md) ^1.0.5

## Recent versions

- 1.4.0 (latest) — 2021-01-11
- 1.3.17 — 2020-06-10
- 1.3.16 — 2019-03-25
- 1.3.15 — 2019-02-07
- 1.3.14 — 2018-10-02
- 1.3.13 — 2018-02-19
- 1.3.12 — 2018-01-10
- 1.3.11 — 2018-01-09
- 1.3.10 — 2017-12-05
- 1.3.9 — 2017-11-07
- 1.3.8 — 2017-10-29
- 1.3.7 — 2017-10-25
- 1.3.6 — 2017-10-13
- 1.3.5 — 2017-10-01
- 1.3.4 — 2017-09-19
- … 17 more at https://npm.io/package/flumeview-reduce/versions

## README

# flumeview-reduce

A flumeview into a reduce function.
Stream append-only log data in the order the log is written into a reduce function to calculate a state.

## Example

``` js
var FlumeLog = require('flumelog-offset')
var codec = require('flumecodec')
var Flume = require('flumedb')
var Reduce = require('flumeview-reduce')

//statistics exports a reduce function that calculates
//mean, stdev, etc!
var statistics = require('statistics')

//initialize a flumelog with a codec.
//this example uses flumelog-offset, but any flumelog is valid.
var log = FlumeLog(file, 1024*16, codec.json) //use any flume log

//attach the reduce function.
var db = Flume(log)
  .use('stats', Reduce(
    1,                                    // version
    statistics,                           // reducer
    function (data) { return data.value } // map
  ))

db.append({value: 1}, function (err) {

  db.stats.get(function (err, stats) {
    console.log(stats) // => {mean: 1, stdev: 0, count: 1, sum: 1, ...}
  })
})
```

## FlumeViewReduce(version, reduce, map?, codec?, initialState?) => FlumeView

construct a flumeview from this reduce function. `version` should be a number,
and must be provided. If you make a breaking change to either `reduce` or `map`
then increment `version` and the view will be rebuilt.

`map` is optional. If map is applied, then each item in the log is passed to `map`
and then if the returned value is not null, it is passed to reduce.

``` js
var _value = map(value)
if(_value != null)
  state = reduce(state, _value, seq)
```

using a `map` function is useful, because it enables efficiently streaming the realtime
changes in the state to a remote client.

then, pass the flumeview to `db.use(name, flumeview)`
and you'll have access to the flumeview methods on `db[name]...`

`codec` (optional) - specify the codec to use in the event your log uses the filesystem.
`initialState` (optional) - declare an initial state for your reducer. This will be ignored if a persisted state is found.

## db[name].get(cb)

get the current state of the reduce. This will wait until the view is up to date, if necessary.

## db[name].stream({live: boolean}) => PullSource

Creates a [pull-stream](https://github.com/pull-stream/pull-stream) whose:
- first value is the current state of the view,
- following values are not the view state, but the new _values_ (they're had your `map` applied, but the `reducer` hasn't been applied yet).

This is a light-weight for a remote client to keep up to date with the view - get a snapshot, and then update it themselves. This way we don't need to send a massive view every time there's a new log entry.

```js
var db = Flume(log)
  .use('stats', Reduce(2, myReducer, myMap))

var viewState // this is our view we're calculating remotely
pull(
  db.stats.stream({live:true}),
  pull.drain(function(value) {
    if (!view) viewState = value      // store the current snapshot (the first value)
    else myReducer(viewState, value)  // update the snapshot use reducer + mapped values

    console.log(value)                // do something with 
  }
)

db.append(
  // ... some code that adds new code to the log, triggering stream.
)
```

## Stores

`flumeview-reduce` currently includes several _store_ implementations,
this is how the actual data is persisted. the current implementations are

* 'flumeview-reduce/store/fs' - store in a file.
* 'flumeview-reduce/store/local-storage' - `localStorage`, in a browser
* 'flumeview-reduce/store/remote' - a meta store that keeps a local copy of a remote view.

to set a store, you must set up flumeview-reduce via the lower level dependency injection api.

``` js
var Store = require('flumeview-reduce/store/fs')
var createReduce = require('flumeview-reduce/inject')

var Reduce = createReduce(Store)

//then use Reduce normally

var view = db.use('view', Reduce(version, reduce, map)) //etc

//since remote is most interesting

var Remote = require('flumeview-reduce/store/remote')
function get (opts, cb) {
  //call the get method on the remote copy of the flumeview
  view.get(opts, cb)
}
var RemoteReduce = createReduce(Remote(get, Store, codec))

var remoteView = _db.use('view', Reduce(version, reduce, map)) //etc
//make sure you pass the exact same reduce and map functions to the remote view!
```

## License

MIT

---
_Source: https://npm.io/package/flumeview-reduce · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
