Rename Client to OrbitDB and set it as module's entry point. Move files to appropriate directories.

This commit is contained in:
haad
2016-04-27 08:48:12 +02:00
parent 81a54f762d
commit 3d12247bdb
18 changed files with 59 additions and 103 deletions

View File

@@ -11,7 +11,7 @@
"engines": {
"node": "^4.x.x"
},
"main": "src/Client.js",
"main": "src/OrbitDB.js",
"dependencies": {
"buffer": "^4.5.1",
"ipfs-log": "^1.2.8",

View File

@@ -3,9 +3,9 @@
const EventEmitter = require('events').EventEmitter;
const logger = require('logplease').create("orbit-db.Client");
const PubSub = require('./PubSub');
const CounterStore = require('./db/CounterStore');
const KeyValueStore = require('./db/KeyValueStore');
const EventStore = require('./db/EventStore');
const CounterStore = require('./stores/counters/CounterStore');
const KeyValueStore = require('./stores/kvstore/KeyValueStore');
const EventStore = require('./stores/eventlog/EventStore');
class Client {
constructor(ipfs, options) {

View File

@@ -1,22 +0,0 @@
'use strict';
const OpTypes = require('./OpTypes');
const OrbitDBItem = require('../post/OrbitDBItem');
const Post = require('../post/Post');
class Operation {
static create(ipfs, operation, key, value) {
const data = {
operation: operation,
key: key,
value: value
};
return Post.create(ipfs, Post.Types.OrbitDBItem, data);
}
static get Types() {
return OpTypes;
}
}
module.exports = Operation;

View File

@@ -1,8 +1,7 @@
'use strict';
const Log = require('ipfs-log');
const Cache = require('../Cache');
const DBOperation = require('./Operation');
const Log = require('ipfs-log');
const Cache = require('../Cache');
class OperationsLog {
constructor(ipfs, dbname, events, opts) {
@@ -17,7 +16,7 @@ class OperationsLog {
}
get ops() {
return this._log.items.map((f) => this._cached[f.payload]);
return this._log.items.map((f) => this._cached[f.hash]);
}
create(id) {
@@ -57,34 +56,35 @@ class OperationsLog {
}
addOperation(operation, key, value) {
let post;
return DBOperation.create(this._ipfs, operation, key, value)
.then((result) => {
return this._log.add(result.Hash).then((node) => {
return { node: node, op: result.Post };
});
})
.then((result) => {
this._cachePayload(result.node.payload, result.op);
return result;
})
const post = {
op: operation,
key: key,
value: value,
meta: {
size: Buffer.byteLength(value ? JSON.stringify(value) : '', 'utf8'),
ts: new Date().getTime()
}
};
return this._log.add(post)
.then((result) => this._cachePayload(result.hash, post))
.then((result) => {
return Log.getIpfsHash(this._ipfs, this._log).then((hash) => {
this.lastWrite = hash;
Cache.set(this.dbname, hash);
this.events.emit('data', this.dbname, hash);
return result.op.hash;
return result.hash;
});
})
});
}
_cacheInMemory(log) {
const promises = log.items
.map((f) => f.payload)
.map((f) => f.hash)
.filter((f) => !this._cached[f])
.map((f) => {
return this._ipfs.object.get(f)
.then((obj) => this._cachePayload(f, JSON.parse(obj.Data)))
.then((obj) => this._cachePayload(f, JSON.parse(obj.Data)["payload"]))
});
return Promise.all(promises);
@@ -96,6 +96,7 @@ class OperationsLog {
if(payload.key === null) Object.assign(payload, { key: hash });
this._cached[hash] = payload;
}
return this._cached[hash];
}
}

View File

@@ -1,7 +1,7 @@
'use strict';
const EventEmitter = require('events').EventEmitter;
const OperationsLog = require('./OperationsLog');
const OperationsLog = require('../oplog/OperationsLog');
const DefaultIndex = require('./DefaultIndex');
class Store {

View File

@@ -1,6 +1,6 @@
'use strict';
const Counter = require('./crdts/GCounter');
const Counter = require('../../crdts/GCounter');
class CounterIndex {
constructor() {

View File

@@ -1,8 +1,8 @@
'use strict';
const Store = require('./Store');
const Store = require('../Store');
const CounterIndex = require('./CounterIndex');
const OpTypes = require('./Operation').Types;
const OpTypes = require('../../oplog/OpTypes');
class CounterStore extends Store {
constructor(ipfs, options) {

View File

@@ -1,6 +1,6 @@
'use strict';
const OpTypes = require('./Operation').Types;
const OpTypes = require('../../oplog/OpTypes');
class EventLogIndex {
constructor() {

View File

@@ -1,9 +1,9 @@
'use strict';
const Lazy = require('lazy.js');
const Store = require('./Store');
const OpTypes = require('./Operation').Types;
const Store = require('../Store');
const EventLogIndex = require('./EventLogIndex');
const OpTypes = require('../../oplog/OpTypes');
class EventStore extends Store {
constructor(ipfs, options) {

View File

@@ -1,6 +1,6 @@
'use strict';
const OpTypes = require('./Operation').Types;
const OpTypes = require('../../oplog/OpTypes');
class KeyValueIndex {
constructor() {

View File

@@ -1,8 +1,8 @@
'use strict';
const Store = require('./Store');
const Store = require('../Store');
const KVIndex = require('./KeyValueIndex');
const OpTypes = require('./Operation').Types;
const OpTypes = require('../../oplog/OpTypes');
class KeyValueStore extends Store {
constructor(ipfs, options) {

View File

@@ -1,12 +1,12 @@
'use strict';
const _ = require('lodash');
const path = require('path');
const assert = require('assert');
const async = require('asyncawait/async');
const await = require('asyncawait/await');
const ipfsd = require('ipfsd-ctl');
const OrbitClient = require('../src/Client');
const _ = require('lodash');
const path = require('path');
const assert = require('assert');
const async = require('asyncawait/async');
const await = require('asyncawait/await');
const ipfsd = require('ipfsd-ctl');
const OrbitDB = require('../src/OrbitDB');
// Mute logging
require('logplease').setLogLevel('ERROR');
@@ -44,9 +44,7 @@ describe('Orbit Client', function() {
try {
ipfs = await(startIpfs());
client = await(OrbitClient.connect('localhost', 3333, username, password, ipfs, { allowOffline: true }));
// db = await(client.channel(channel, '', false));
// db.delete();
client = await(OrbitDB.connect('localhost', 3333, username, password, ipfs, { allowOffline: true }));
} catch(e) {
console.log(e);
assert.equal(e, null);
@@ -134,9 +132,10 @@ describe('Orbit Client', function() {
*/
describe('Add events', function() {
beforeEach(async(() => {
beforeEach(async((done) => {
db = await(client.eventlog(channel, false));
db.delete();
done();
}));
it('adds an item to an empty channel', async((done) => {
@@ -183,8 +182,6 @@ describe('Orbit Client', function() {
beforeEach(async(() => {
db = await(client.eventlog(channel, false));
db.delete();
// const items = db.iterator().collect();
// assert.equal(items.length, 0);
}));
it('deletes an item when only one item in the database', async((done) => {
@@ -530,10 +527,6 @@ describe('Orbit Client', function() {
});
describe('Key-Value Store', function() {
// before(() => {
// db.delete();
// });
beforeEach(async((done) => {
db = await(client.kvstore(channel, '', false));
db.delete();
@@ -545,15 +538,9 @@ describe('Orbit Client', function() {
});
it('put', async((done) => {
// db = await(client.kvstore(channel, '', false));
await(db.put('key1', 'hello!'));
const value = db.get('key1');
// let all = db.iterator().collect();
assert.equal(value, 'hello!');
// assert.equal(all.length, 1);
// assert.equal(all[0].hash.startsWith('Qm'), true);
// assert.equal(all[0].key, 'key1');
// assert.notEqual(all[0].meta, null);
done();
}));
@@ -572,6 +559,19 @@ describe('Orbit Client', function() {
done();
}));
it('put/get - multiple keys', async((done) => {
await(db.put('key1', 'hello1'));
await(db.put('key2', 'hello2'));
await(db.put('key3', 'hello3'));
const v1 = db.get('key1');
const v2 = db.get('key2');
const v3 = db.get('key3');
assert.equal(v1, 'hello1');
assert.equal(v2, 'hello2');
assert.equal(v3, 'hello3');
done();
}));
it('deletes a key', async((done) => {
await(db.put('key1', 'hello!'));
await(db.del('key1'));
@@ -590,28 +590,6 @@ describe('Orbit Client', function() {
done();
}));
it('put - multiple keys', async((done) => {
await(db.put('key1', 'hello1'));
await(db.put('key2', 'hello2'));
await(db.put('key3', 'hello3'));
// const all = db.iterator().collect();
// assert.equal(all.length, 1);
done();
}));
it('get - multiple keys', async((done) => {
await(db.put('key1', 'hello1'));
await(db.put('key2', 'hello2'));
await(db.put('key3', 'hello3'));
const v1 = db.get('key1');
const v2 = db.get('key2');
const v3 = db.get('key3');
assert.equal(v1, 'hello1');
assert.equal(v2, 'hello2');
assert.equal(v3, 'hello3');
done();
}));
it('get - integer value', async((done) => {
await(db.put('key1', 123));
const v1 = db.get('key1');
@@ -623,7 +601,6 @@ describe('Orbit Client', function() {
const val = { one: 'first', two: 2 };
await(db.put('key1', val));
const v1 = db.get('key1');
// console.log(v1)
assert.equal(_.isEqual(v1, val), true);
done();
}));

View File

@@ -4,7 +4,7 @@ const assert = require('assert');
const Promise = require('bluebird');
const rimraf = require('rimraf')
const ipfsd = require('ipfsd-ctl');
const OrbitClient = require('../src/Client');
const OrbitDB = require('../src/OrbitDB');
const OrbitServer = require('orbit-server/src/server');
// Mute logging
@@ -42,7 +42,7 @@ describe('CounterStore', function() {
startIpfs().then((res) => {
ipfs = res;
Promise.map([username, username2], (login) => {
return OrbitClient.connect('localhost', 3333, login, '', ipfs, { allowOffline: false, cacheFile: './orbit-db-cache.json' });
return OrbitDB.connect('localhost', 3333, login, '', ipfs, { allowOffline: false, cacheFile: './orbit-db-cache.json' });
}).then((clients) => {
client1 = clients[0];
client2 = clients[1];