From 0119a46fa599b1a6a107457af2f6036b7f95220e Mon Sep 17 00:00:00 2001 From: haad Date: Fri, 15 Apr 2016 16:28:56 +0200 Subject: [PATCH] Update README, fix examples --- README.md | 57 +++++++++++++++++++++-------- examples/benchmark.js | 21 ++++++++--- examples/{reader.js => eventlog.js} | 21 ++++++++--- examples/keyvalue.js | 25 +++++++++---- examples/keyvalueReader.js | 47 ------------------------ package.json | 1 + 6 files changed, 93 insertions(+), 79 deletions(-) rename examples/{reader.js => eventlog.js} (74%) delete mode 100644 examples/keyvalueReader.js diff --git a/README.md b/README.md index 7516389..0aa020b 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,18 @@ ## Introduction -Distributed, peer-to-peer Key-Value Store and Event Log on IPFS. +Distributed, peer-to-peer **Key-Value Store and Event Log** on IPFS. This is the Javascript implementation and it works both in **Node.js** and **Browsers**. - Stores all data in IPFS, including the database index - Aggregation happens on client side and data is eventually consistent -- Uses a LWW-element-set [CRDT](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type) and (sort of) Vector Clocks for partial ordering +- Uses a LWW-element-set [CRDT](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type) and [ipfs-log](https://github.com/haadcode/ipfs-log) for partial order - Designed to work offline first and to be easily embeddable to applications _Currently requires [orbit-server](https://github.com/haadcode/orbit-server) for pubsub communication. This will change in the future as soon as IPFS provides pubsub._ -_OrbitDB calls its namespaces channels. A channel is similar to "table", "keyspace", "topic", "feed" or "collection" in other db systems._ +![Screenshot](https://raw.githubusercontent.com/haadcode/orbit-db/master/screenshot.png) ## Install ``` @@ -21,29 +21,22 @@ npm install orbit-db ``` ## Examples -### Browser examples -In order to run the browser example, you need to have a local IPFS daemon running. Start it with the following command: -``` -API_ORIGIN=* ipfs daemon -``` - -Then open `examples/browser.html`. See the example code [here](https://github.com/haadcode/orbit-db/blob/master/examples/browser.html). - ### Node.js examples +*To run the examples, make sure to run a local [orbit-server](https://github.com/haadcode/orbit-server)* + Before running the examples, install dependencies with: ``` npm install ``` -Key-Value store example: +Key-Value store [example](https://github.com/haadcode/orbit-db/blob/master/examples/keyvalue.js): ``` node examples/keyvalue.js -node examples/keyvalueReader.js ``` -Event log example (run several in separate shells): +Event log [example](https://github.com/haadcode/orbit-db/blob/master/examples/eventlog.js) (run several in separate shells): ``` -node examples/reader.js +node examples/eventlog.js ``` Benchmark writes: @@ -51,9 +44,43 @@ Benchmark writes: node examples/benchmark.js ; ``` + +### Browser examples +In order to run the browser example, you need to have a local IPFS daemon running. Start it with the following command: +``` +API_ORIGIN=* ipfs daemon +``` + +Then open `examples/browser.html`. See the full example [here](https://github.com/haadcode/orbit-db/blob/master/examples/browser.html). + +```html + + + + + + + + + + +``` + ## API _See usage example below_ +_OrbitDB calls its namespaces channels. A channel is similar to "table", "keyspace", "topic", "feed" or "collection" in other db systems._ + connect(host, port, username, password) channel(name, password) diff --git a/examples/benchmark.js b/examples/benchmark.js index 2c3563b..f86091c 100644 --- a/examples/benchmark.js +++ b/examples/benchmark.js @@ -1,9 +1,10 @@ 'use strict'; -var await = require('asyncawait/await'); -var async = require('asyncawait/async'); -var OrbitClient = require('../src/Client'); -var Timer = require('./Timer'); +const await = require('asyncawait/await'); +const async = require('asyncawait/async'); +const ipfsd = require('ipfsd-ctl'); +const OrbitDB = require('../src/Client'); +const Timer = require('./Timer'); // usage: benchmark.js ; @@ -16,10 +17,20 @@ const password = ''; const channelName = process.argv[4] ? process.argv[4] : 'c1'; +const startIpfs = () => { + return new Promise((resolve, reject) => { + ipfsd.disposableApi((err, ipfs) => { + if(err) console.error(err); + resolve(ipfs); + }); + }); +}; + let run = (async(() => { try { // Connect - const orbit = await(OrbitClient.connect(host, port, username, password)); + const ipfs = await(startIpfs()); + const orbit = await(OrbitDB.connect(host, port, username, password, ipfs)); const db = await(orbit.channel(channelName)); // Metrics diff --git a/examples/reader.js b/examples/eventlog.js similarity index 74% rename from examples/reader.js rename to examples/eventlog.js index b22a60a..007ceea 100644 --- a/examples/reader.js +++ b/examples/eventlog.js @@ -1,9 +1,10 @@ 'use strict'; -const async = require('asyncawait/async'); -const await = require('asyncawait/await'); -const OrbitClient = require('../src/Client'); -const Timer = require('./Timer'); +const async = require('asyncawait/async'); +const await = require('asyncawait/await'); +const ipfsd = require('ipfsd-ctl'); +const OrbitDB = require('../src/Client'); +const Timer = require('./Timer'); // usage: reader.js @@ -17,9 +18,19 @@ const password = ''; const channelName = process.argv[4] ? process.argv[4] : 'test'; const prefix = process.argv[5] ? process.argv[5] : 'Hello'; +const startIpfs = () => { + return new Promise((resolve, reject) => { + ipfsd.disposableApi((err, ipfs) => { + if(err) console.error(err); + resolve(ipfs); + }); + }); +}; + let run = (async(() => { try { - const orbit = await(OrbitClient.connect(host, port, username, password)); + const ipfs = await(startIpfs()); + const orbit = await(OrbitDB.connect(host, port, username, password, ipfs)); const db = await(orbit.channel(channelName)); let count = 1; diff --git a/examples/keyvalue.js b/examples/keyvalue.js index cd357ef..07d0bc1 100644 --- a/examples/keyvalue.js +++ b/examples/keyvalue.js @@ -1,9 +1,10 @@ 'use strict'; -const async = require('asyncawait/async'); -const await = require('asyncawait/await'); -const OrbitClient = require('../src/Client'); -const Timer = require('./Timer'); +const async = require('asyncawait/async'); +const await = require('asyncawait/await'); +const ipfsd = require('ipfsd-ctl'); +const OrbitDB = require('../src/Client'); +const Timer = require('./Timer'); // usage: keyvalue.js @@ -16,10 +17,20 @@ const password = ''; const channel = process.argv[4] ? process.argv[4] : 'testing123'; +const startIpfs = () => { + return new Promise((resolve, reject) => { + ipfsd.disposableApi((err, ipfs) => { + if(err) console.error(err); + resolve(ipfs); + }); + }); +}; + let run = (async(() => { try { - const orbit = await(OrbitClient.connect(host, port, username, password)); - const db = await(orbit.channel(channelName)); + const ipfs = await(startIpfs()); + const orbit = await(OrbitDB.connect(host, port, username, password, ipfs)); + const db = await(orbit.channel(channel)); let count = 1; @@ -27,7 +38,7 @@ let run = (async(() => { const key = process.argv[5] ? process.argv[5] : 'greeting'; const value = process.argv[6] ? process.argv[6] : 'Hello world'; const timer = new Timer(true); - db.put(key, value + " " + count); + await(db.put(key, value + " " + count)); const result = db.get(key); console.log("---------------------------------------------------") diff --git a/examples/keyvalueReader.js b/examples/keyvalueReader.js deleted file mode 100644 index 97ddd14..0000000 --- a/examples/keyvalueReader.js +++ /dev/null @@ -1,47 +0,0 @@ -'use strict'; - -const async = require('asyncawait/async'); -const await = require('asyncawait/await'); -const OrbitClient = require('../src/Client'); -const Timer = require('./Timer'); - -// usage: keyvalueReader.js - -// orbit-server -const host = process.argv[2] ? process.argv[2] : 'localhost' -const port = 3333; - -const username = process.argv[3] ? process.argv[3] : 'LambOfGod'; -const password = ''; - -const channelName = process.argv[4] ? process.argv[4] : 'testing123'; -const key = process.argv[5] ? process.argv[5] : 'greeting'; - -let run = (async(() => { - try { - const orbit = await(OrbitClient.connect(host, port, username, password)); - const db = await(orbit.channel(channelName)); - - let count = 1; - - setInterval(async(() => { - let timer = new Timer(true); - const result = db.get(key); - - console.log("---------------------------------------------------") - console.log("Key | Value") - console.log("---------------------------------------------------") - console.log(`${key} | ${result}`); - console.log("---------------------------------------------------") - console.log(`Query #${count} took ${timer.stop(true)} ms\n`); - - count ++; - }), 1000); - } catch(e) { - console.error("error:", e); - console.error(e.stack); - process.exit(1); - } -}))(); - -module.exports = run; diff --git a/package.json b/package.json index 6604202..029bb57 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "json-loader": "^0.5.4", "lodash": "^4.3.0", "mocha": "^2.4.5", + "orbit-common": "^0.2.5", "stream-http": "^2.2.1", "webpack": "^1.12.15" },