Major perfomance improvements. Post v2.

This commit is contained in:
haad 2016-03-09 20:22:18 +01:00
parent 7c8d60a4a5
commit 003efb61ba
10 changed files with 41 additions and 22 deletions

View File

@ -36,7 +36,7 @@ class OrbitClient {
put: (key, data) => this.db.put(channel, password, key, data), put: (key, data) => this.db.put(channel, password, key, data),
get: (key, options) => { get: (key, options) => {
const items = this._iterator(channel, password, { key: key }).collect(); const items = this._iterator(channel, password, { key: key }).collect();
return items[0] ? items[0].value : null; return items[0] ? items[0].content : null;
}, },
close: () => this._pubsub.unsubscribe(channel) close: () => this._pubsub.unsubscribe(channel)
} }
@ -90,7 +90,7 @@ class OrbitClient {
throw e; throw e;
} }
} }
this.user = { username: username, id: 'TODO: user id' } this.user = { username: username, id: username } // TODO: user id from ipfs hash
this.network = { host: host, port: port, name: 'TODO: network name' } this.network = { host: host, port: port, name: 'TODO: network name' }
} }
} }

View File

@ -41,7 +41,7 @@ class OrbitDB {
// Get items from the db // Get items from the db
query(channel, password, opts) { query(channel, password, opts) {
console.log("--> Query:", channel, password, opts); console.log("--> Query:", channel, opts);
if(!opts) opts = {}; if(!opts) opts = {};
@ -52,7 +52,7 @@ class OrbitDB {
if(opts.key) { if(opts.key) {
// Key-Value, search latest key first // Key-Value, search latest key first
result = this._read(operations.reverse(), opts.key, 1, true); result = this._read(operations.reverse(), opts.key, 1, true).map((f) => f.value);
} else if(opts.gt || opts.gte) { } else if(opts.gt || opts.gte) {
// Greater than case // Greater than case
result = this._read(operations, opts.gt ? opts.gt : opts.gte, amount, opts.gte ? opts.gte : false); result = this._read(operations, opts.gt ? opts.gt : opts.gte, amount, opts.gte ? opts.gte : false);
@ -108,6 +108,22 @@ class OrbitDB {
return null; return null;
}; };
// var _fetchAsync = async(() => {
// return new Promise(async((resolve, reject) => {
// const handle = sequence
// .async()
// .map(async((f) => await(f.fetchPayload()))) // IO - fetch the actual OP from ipfs. consider merging with LL.
// .skipWhile((f) => key && f.key !== key) // Drop elements until we have the first one requested
// .map(_createLWWSet) // Return items as LWW (ignore values after the first found)
// // .filter((f) => f !== null) // Make sure we don't have empty ones
// .drop(inclusive ? 0 : 1) // Drop the 'gt/lt' item, include 'gte/lte' item
// .take(amount)
// .toArray();
// handle.onComplete(resolve);
// }));
// })
// return await(_fetchAsync());
// Find an items from the sequence (list of operations) // Find an items from the sequence (list of operations)
return sequence return sequence
.map((f) => await(f.fetchPayload())) // IO - fetch the actual OP from ipfs. consider merging with LL. .map((f) => await(f.fetchPayload())) // IO - fetch the actual OP from ipfs. consider merging with LL.

View File

@ -5,7 +5,6 @@ const await = require('asyncawait/await');
const ipfsAPI = require('orbit-common/lib/ipfs-api-promised'); const ipfsAPI = require('orbit-common/lib/ipfs-api-promised');
const OrbitDBItem = require('../post/OrbitDBItem'); const OrbitDBItem = require('../post/OrbitDBItem');
const Post = require('../post/Post'); const Post = require('../post/Post');
const MetaInfo = require('./MetaInfo');
class Operation { class Operation {
static create(ipfs, log, user, operation, key, value, data) { static create(ipfs, log, user, operation, key, value, data) {
@ -23,13 +22,11 @@ class Operation {
static _createOperation(ipfs, user, operation, key, value) { static _createOperation(ipfs, user, operation, key, value) {
var createOperationAsync = async(() => { var createOperationAsync = async(() => {
return new Promise(async((resolve, reject) => { return new Promise(async((resolve, reject) => {
const size = -1;
const meta = new MetaInfo("orbitdb-op", size, user.username, new Date().getTime());
const data = { const data = {
operation: operation, operation: operation,
key: key, key: key,
value: value, value: value,
meta: meta by: user.id || 'empty'
}; };
const op = await(Post.create(ipfs, Post.Types.OrbitDBItem, data)); const op = await(Post.create(ipfs, Post.Types.OrbitDBItem, data));
resolve(op.Hash); resolve(op.Hash);

View File

@ -21,7 +21,7 @@ class OrbitNode extends Node {
this.Payload = JSON.parse(payload.Data); this.Payload = JSON.parse(payload.Data);
if(this.Payload.value) { if(this.Payload.value) {
const value = await(ipfsAPI.getObject(this._ipfs, this.Payload.value)); const value = await(ipfsAPI.getObject(this._ipfs, this.Payload.value));
this.Payload.value = JSON.parse(value.Data)["content"]; this.Payload.value = JSON.parse(value.Data);
} }
} }
let res = this.Payload; let res = this.Payload;

View File

@ -6,6 +6,7 @@ const TextPost = require('./TextPost');
class FilePost extends TextPost { class FilePost extends TextPost {
constructor(content, file, size) { constructor(content, file, size) {
super(content); super(content);
this.type = "file";
this.file = file; this.file = file;
this.size = size; this.size = size;
} }

View File

@ -1,10 +1,9 @@
'use strict'; 'use strict';
class MetaInfo { class MetaInfo {
constructor(type, size, from, ts) { constructor(type, size, ts) {
this.type = type; this.type = type;
this.size = size; this.size = size;
this.from = from;
this.ts = ts; this.ts = ts;
} }
} }

View File

@ -1,11 +1,13 @@
'use strict'; 'use strict';
class OrbitDBItem { class OrbitDBItem {
constructor(operation, key, value, metaInfo) { constructor(operation, key, value, metaInfo, by) {
this.type = "orbit-db-op";
this.op = operation; this.op = operation;
this.key = key; this.key = key;
this.value = value; this.value = value;
this.meta = metaInfo; this.meta = metaInfo;
this.by = by;
} }
} }

View File

@ -8,6 +8,7 @@ const Encryption = require('orbit-common/lib/Encryption');
const TextPost = require('./TextPost'); const TextPost = require('./TextPost');
const FilePost = require('./FilePost'); const FilePost = require('./FilePost');
const OrbitDBItem = require('./OrbitDBItem'); const OrbitDBItem = require('./OrbitDBItem');
const MetaInfo = require('./MetaInfo');
const PostTypes = { const PostTypes = {
Message: TextPost, Message: TextPost,
@ -28,8 +29,10 @@ class Posts {
} else if(type === PostTypes.File) { } else if(type === PostTypes.File) {
post = new PostTypes.File(data.content, data.file, data.size); post = new PostTypes.File(data.content, data.file, data.size);
} else if(type == PostTypes.OrbitDBItem) { } else if(type == PostTypes.OrbitDBItem) {
post = new PostTypes.OrbitDBItem(data.operation, data.key, data.value, data.meta); post = new PostTypes.OrbitDBItem(data.operation, data.key, data.value, data.meta, data.by);
} }
const size = data.size ? data.size : Buffer.byteLength(data, 'utf8');
post.meta = new MetaInfo(post.type, size, new Date().getTime());
const res = await (ipfsAPI.putObject(ipfs, JSON.stringify(post))); const res = await (ipfsAPI.putObject(ipfs, JSON.stringify(post)));
resolve(res); resolve(res);
}) })

View File

@ -6,6 +6,7 @@ const Encryption = require('orbit-common/lib/Encryption');
class TextPost { class TextPost {
constructor(content) { constructor(content) {
this.content = content; this.content = content;
this.type = "text";
this.ts = new Date().getTime(); this.ts = new Date().getTime();
} }

View File

@ -112,8 +112,8 @@ describe('Orbit Client', function() {
const items = db.iterator().collect(); const items = db.iterator().collect();
assert.equal(items.length, 1); assert.equal(items.length, 1);
assert.equal(items[0].op, 'ADD'); assert.equal(items[0].op, 'ADD');
assert.equal(items[0].value, 'hello1'); assert.equal(items[0].value.content, 'hello1');
assert.notEqual(items[0].meta, null); assert.notEqual(items[0].value.meta, null);
done(); done();
})); }));
@ -126,8 +126,8 @@ describe('Orbit Client', function() {
assert.equal(items.length, 1); assert.equal(items.length, 1);
assert.equal(items[0].hash.startsWith('Qm'), true); assert.equal(items[0].hash.startsWith('Qm'), true);
assert.equal(items[0].op, 'ADD'); assert.equal(items[0].op, 'ADD');
assert.equal(items[0].value, 'hello3'); assert.equal(items[0].value.content, 'hello3');
assert.notEqual(items[0].meta, null); assert.notEqual(items[0].value.meta, null);
done(); done();
})); }));
}); });
@ -171,8 +171,8 @@ describe('Orbit Client', function() {
assert.notEqual(next, null); assert.notEqual(next, null);
assert.equal(next.op, 'ADD'); assert.equal(next.op, 'ADD');
assert.equal(next.key.startsWith('Qm'), true); assert.equal(next.key.startsWith('Qm'), true);
assert.equal(next.value, 'hello4'); assert.equal(next.value.content, 'hello4');
assert.notEqual(next.meta, null); assert.notEqual(next.value.meta, null);
done(); done();
})); }));
@ -199,7 +199,7 @@ describe('Orbit Client', function() {
const second = iter.next().value; const second = iter.next().value;
assert.equal(first.key, items2[items2.length - 1]); assert.equal(first.key, items2[items2.length - 1]);
assert.equal(second, null); assert.equal(second, null);
assert.equal(first.value, 'hello4'); assert.equal(first.value.content, 'hello4');
done(); done();
})); }));
}); });
@ -225,8 +225,8 @@ describe('Orbit Client', function() {
it('returns all items', async((done) => { it('returns all items', async((done) => {
const messages = db.iterator({ limit: -1 }).collect(); const messages = db.iterator({ limit: -1 }).collect();
assert.equal(messages.length, items.length); assert.equal(messages.length, items.length);
assert.equal(messages[0].value, 'hello0'); assert.equal(messages[0].value.content, 'hello0');
assert.equal(messages[messages.length - 1].value, 'hello4'); assert.equal(messages[messages.length - 1].value.content, 'hello4');
done(); done();
})); }));