mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-10-07 22:57:07 +00:00
Major perfomance improvements. Post v2.
This commit is contained in:
@@ -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' }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
})
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user