From 003efb61ba95aaf97a566d3247b42b6dc7fe049d Mon Sep 17 00:00:00 2001 From: haad Date: Wed, 9 Mar 2016 20:22:18 +0100 Subject: [PATCH] Major perfomance improvements. Post v2. --- src/OrbitClient.js | 4 ++-- src/OrbitDB.js | 20 ++++++++++++++++++-- src/db/Operation.js | 5 +---- src/list/OrbitNode.js | 2 +- src/post/FilePost.js | 1 + src/{db => post}/MetaInfo.js | 3 +-- src/post/OrbitDBItem.js | 4 +++- src/post/Post.js | 5 ++++- src/post/TextPost.js | 1 + test/orbit-client-tests.js | 18 +++++++++--------- 10 files changed, 41 insertions(+), 22 deletions(-) rename src/{db => post}/MetaInfo.js (68%) diff --git a/src/OrbitClient.js b/src/OrbitClient.js index 117cb86..eee90be 100644 --- a/src/OrbitClient.js +++ b/src/OrbitClient.js @@ -36,7 +36,7 @@ class OrbitClient { put: (key, data) => this.db.put(channel, password, key, data), get: (key, options) => { 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) } @@ -90,7 +90,7 @@ class OrbitClient { 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' } } } diff --git a/src/OrbitDB.js b/src/OrbitDB.js index c6bc3f6..d284c97 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -41,7 +41,7 @@ class OrbitDB { // Get items from the db query(channel, password, opts) { - console.log("--> Query:", channel, password, opts); + console.log("--> Query:", channel, opts); if(!opts) opts = {}; @@ -52,7 +52,7 @@ class OrbitDB { if(opts.key) { // 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) { // Greater than case result = this._read(operations, opts.gt ? opts.gt : opts.gte, amount, opts.gte ? opts.gte : false); @@ -108,6 +108,22 @@ class OrbitDB { 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) return sequence .map((f) => await(f.fetchPayload())) // IO - fetch the actual OP from ipfs. consider merging with LL. diff --git a/src/db/Operation.js b/src/db/Operation.js index 902b958..e35365f 100644 --- a/src/db/Operation.js +++ b/src/db/Operation.js @@ -5,7 +5,6 @@ const await = require('asyncawait/await'); const ipfsAPI = require('orbit-common/lib/ipfs-api-promised'); const OrbitDBItem = require('../post/OrbitDBItem'); const Post = require('../post/Post'); -const MetaInfo = require('./MetaInfo'); class Operation { static create(ipfs, log, user, operation, key, value, data) { @@ -23,13 +22,11 @@ class Operation { static _createOperation(ipfs, user, operation, key, value) { var createOperationAsync = async(() => { return new Promise(async((resolve, reject) => { - const size = -1; - const meta = new MetaInfo("orbitdb-op", size, user.username, new Date().getTime()); const data = { operation: operation, key: key, value: value, - meta: meta + by: user.id || 'empty' }; const op = await(Post.create(ipfs, Post.Types.OrbitDBItem, data)); resolve(op.Hash); diff --git a/src/list/OrbitNode.js b/src/list/OrbitNode.js index a77a1ad..4388dc8 100644 --- a/src/list/OrbitNode.js +++ b/src/list/OrbitNode.js @@ -21,7 +21,7 @@ class OrbitNode extends Node { this.Payload = JSON.parse(payload.Data); if(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; diff --git a/src/post/FilePost.js b/src/post/FilePost.js index da99c6c..7b992ee 100644 --- a/src/post/FilePost.js +++ b/src/post/FilePost.js @@ -6,6 +6,7 @@ const TextPost = require('./TextPost'); class FilePost extends TextPost { constructor(content, file, size) { super(content); + this.type = "file"; this.file = file; this.size = size; } diff --git a/src/db/MetaInfo.js b/src/post/MetaInfo.js similarity index 68% rename from src/db/MetaInfo.js rename to src/post/MetaInfo.js index b460e7c..f68f953 100644 --- a/src/db/MetaInfo.js +++ b/src/post/MetaInfo.js @@ -1,10 +1,9 @@ 'use strict'; class MetaInfo { - constructor(type, size, from, ts) { + constructor(type, size, ts) { this.type = type; this.size = size; - this.from = from; this.ts = ts; } } diff --git a/src/post/OrbitDBItem.js b/src/post/OrbitDBItem.js index 785e66a..29a1c35 100644 --- a/src/post/OrbitDBItem.js +++ b/src/post/OrbitDBItem.js @@ -1,11 +1,13 @@ 'use strict'; class OrbitDBItem { - constructor(operation, key, value, metaInfo) { + constructor(operation, key, value, metaInfo, by) { + this.type = "orbit-db-op"; this.op = operation; this.key = key; this.value = value; this.meta = metaInfo; + this.by = by; } } diff --git a/src/post/Post.js b/src/post/Post.js index 1568840..420b7e0 100644 --- a/src/post/Post.js +++ b/src/post/Post.js @@ -8,6 +8,7 @@ const Encryption = require('orbit-common/lib/Encryption'); const TextPost = require('./TextPost'); const FilePost = require('./FilePost'); const OrbitDBItem = require('./OrbitDBItem'); +const MetaInfo = require('./MetaInfo'); const PostTypes = { Message: TextPost, @@ -28,8 +29,10 @@ class Posts { } else if(type === PostTypes.File) { post = new PostTypes.File(data.content, data.file, data.size); } 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))); resolve(res); }) diff --git a/src/post/TextPost.js b/src/post/TextPost.js index d9f7760..313bbe5 100644 --- a/src/post/TextPost.js +++ b/src/post/TextPost.js @@ -6,6 +6,7 @@ const Encryption = require('orbit-common/lib/Encryption'); class TextPost { constructor(content) { this.content = content; + this.type = "text"; this.ts = new Date().getTime(); } diff --git a/test/orbit-client-tests.js b/test/orbit-client-tests.js index a339471..12f8c6b 100644 --- a/test/orbit-client-tests.js +++ b/test/orbit-client-tests.js @@ -112,8 +112,8 @@ describe('Orbit Client', function() { const items = db.iterator().collect(); assert.equal(items.length, 1); assert.equal(items[0].op, 'ADD'); - assert.equal(items[0].value, 'hello1'); - assert.notEqual(items[0].meta, null); + assert.equal(items[0].value.content, 'hello1'); + assert.notEqual(items[0].value.meta, null); done(); })); @@ -126,8 +126,8 @@ describe('Orbit Client', function() { assert.equal(items.length, 1); assert.equal(items[0].hash.startsWith('Qm'), true); assert.equal(items[0].op, 'ADD'); - assert.equal(items[0].value, 'hello3'); - assert.notEqual(items[0].meta, null); + assert.equal(items[0].value.content, 'hello3'); + assert.notEqual(items[0].value.meta, null); done(); })); }); @@ -171,8 +171,8 @@ describe('Orbit Client', function() { assert.notEqual(next, null); assert.equal(next.op, 'ADD'); assert.equal(next.key.startsWith('Qm'), true); - assert.equal(next.value, 'hello4'); - assert.notEqual(next.meta, null); + assert.equal(next.value.content, 'hello4'); + assert.notEqual(next.value.meta, null); done(); })); @@ -199,7 +199,7 @@ describe('Orbit Client', function() { const second = iter.next().value; assert.equal(first.key, items2[items2.length - 1]); assert.equal(second, null); - assert.equal(first.value, 'hello4'); + assert.equal(first.value.content, 'hello4'); done(); })); }); @@ -225,8 +225,8 @@ describe('Orbit Client', function() { it('returns all items', async((done) => { const messages = db.iterator({ limit: -1 }).collect(); assert.equal(messages.length, items.length); - assert.equal(messages[0].value, 'hello0'); - assert.equal(messages[messages.length - 1].value, 'hello4'); + assert.equal(messages[0].value.content, 'hello0'); + assert.equal(messages[messages.length - 1].value.content, 'hello4'); done(); }));