Skip to content

Commit

Permalink
Updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor committed Nov 22, 2017
1 parent 0b09717 commit 918bcba
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 65 deletions.
110 changes: 45 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

[![npm version][npm-badge]][npm-url]
[![Build Status][travis-badge]][travis-url]
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
[![Coverage Status][coveralls-badge]][coveralls-url]

Key Value store on top of Upring.

[![js-standard-style](https://raw.githubusercontent.com/feross/standard/master/badge.png)](https://github.com/feross/standard)
Key Value store plugin for UpRing.

## Install

Expand All @@ -16,86 +15,67 @@ npm i upring-kv --save

## Usage

See [./bin.js](./bin.js) for exposing upring-kv over HTTP.
This file contains a small http API to get/put data into the
key-value store. Each URL equals to a given key.

To use is, follow these instructions. First, install some
dependencies:

```
npm i upring-kv pino baseswim -g
```

Then, we need to figure out what is our ip.

On Linux:

```sh
export MYIP=`ip addr show wlan0 | grep -Po 'inet \K[\d.]+'`
```

On Mac:

```sh
export MYIP=`ipconfig getifaddr en0`
```

The export phase needs to be done for every opened shell.

Then we can start our upring cluster. We will use a
[baseswim](http://npm.im/baseswim) node to simplify bootstrapping.

```sh
# on one shell
baseswim --host $MYIP --port 7979 | pino
# on another shell
upring-kv -p 3042 $MYIP:7979 | pino
# on another shell
upring-kv -p 3043 $MYIP:7979 | pino
# on another shell
upring-kv -p 3044 $MYIP:7979 | pino
```

Then we can query our key/value storage using basic curl.

```
curl -v localhost:3042
curl -X POST -d 'hello upring' localhost:3043
curl -v localhost:3044
# on another shell
curl localhost:3042?live=true # use SSE to send updates
# one more shell
curl -X POST -d 'by Matteo' localhost:3043
This library exposes the standard `upring` plugin interface.
Once you register it, it adds a `kv` name space with the API documented below.
```js
const upring = require('upring')({
logLevel: 'info',
base: [],
hashring: {
joinTimeout: 200,
replicaPoints: 10
}
})

upring.use(require('upring-kv'))

upring.on('up', onReady)

function onReady () {
upring.kv.put('hello', 'world', onPut)
}

function onPut (err) {
if (err) {
return upring.logger.error(err)
}
upring.kv.get('hello', onGet)
}

function onGet (err, value) {
if (err) {
return upring.logger.error(err)
}
console.log(value)
upring.close()
}
```

## API

* <a href="#constructor"><code>UpRingKV</code></a>
* <a href="#get"><code>kv#<b>get()</b></code></a>
* <a href="#put"><code>kv#<b>put()</b></code></a>
* <a href="#liveUpdates"><code>kv#<b>liveUpdates()</b></code></a>

-------------------------------------------------------
<a name="constructor"></a>
### new UpRingKV(opts)

All the options of [UpRing][upring], with the following added:

* `upring`: an already initialized `UpRing` instance that has not
already emitted `'up'`

-------------------------------------------------------
<a name="get"></a>
### kv.get(key, cb(err, value))

Get a value from the hashring.
Get a value from the hashring.
*async-await* is supported as well:
```js
await upring.kv.get('key')
```

-------------------------------------------------------
<a name="put"></a>
### kv.put(key, value, cb(err))

Put `value` in the hashring for the given key.
*async-await* is supported as well:
```js
await upring.kv.put('key', 'value')
```

-------------------------------------------------------
<a name="liveUpdates"></a>
Expand Down
35 changes: 35 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const upring = require('upring')({
logLevel: 'info',
base: [],
hashring: {
joinTimeout: 200,
replicaPoints: 10
}
})

upring.use(require('./kv'))

upring.on('up', onReady)

function onReady () {
console.log('upring ready')
upring.kv.put('hello', 'world', onPut)
}

function onPut (err) {
if (err) {
return upring.logger.error(err)
}
console.log('onPut')
upring.kv.get('hello', onGet)
}

function onGet (err, value) {
if (err) {
return upring.logger.error(err)
}
console.log(value)
upring.close()
}

0 comments on commit 918bcba

Please sign in to comment.