From c6f2fbf51c9fc915ffc69538e3bd32aa9fed54fb Mon Sep 17 00:00:00 2001 From: haad Date: Tue, 1 Mar 2016 15:12:41 +0100 Subject: [PATCH] Working version --- src/list/OrbitList.js | 70 ++++++++++++++++++++++++++++------------ test/list-tests.js | 42 ++++++++++++------------ test/orbit-list-tests.js | 60 ++++++++++++++++++---------------- 3 files changed, 102 insertions(+), 70 deletions(-) diff --git a/src/list/OrbitList.js b/src/list/OrbitList.js index a40f2b0..0f199d5 100644 --- a/src/list/OrbitList.js +++ b/src/list/OrbitList.js @@ -8,7 +8,7 @@ const List = require('./List'); const Node = require('./OrbitNode'); const MaxBatchSize = 10; // How many items per sequence. Saves a snapshot to ipfs in batches of this many items. -const MaxHistory = 8; // How many items to fetch in the chain per join +const MaxHistory = 32; // How many items to fetch in the chain per join class OrbitList extends List { constructor(id, ipfs) { @@ -70,49 +70,79 @@ class OrbitList extends List { } }; - const fetchRecursive2 = (hash, amount, currentAmount, all) => { - let res = []; + // const fetchRecursive2 = (hash, amount, currentAmount, all) => { + // let res = []; + // hash = hash instanceof Node === true ? hash.hash : hash; + + // if(currentAmount >= amount) + // return res; + + // if(!isReferenced(all, hash)) { + // currentAmount ++; + // all.push(hash); + // const item = Node.fromIpfsHash(this._ipfs, hash); + // console.log("-", item.compactId, item.heads.length, amount, currentAmount); + // res = _.flatten(item.heads.map((head) => { + // return fetchRecursive2(head, amount, currentAmount, all); + // })); + // res.push(item); + // console.log("res", res.length); + // } + // return _.flatten(res); + // }; + const fetchRecursive2 = (hash, amount, all, res) => { + // console.log("--> fetch", amount) + let result = res ? res : []; hash = hash instanceof Node === true ? hash.hash : hash; - if(currentAmount >= amount) + if(res.length >= amount) { + // console.log("------------------- exit", res.length, amount) return res; + } if(!isReferenced(all, hash)) { - currentAmount ++; all.push(hash); const item = Node.fromIpfsHash(this._ipfs, hash); - console.log("-", item.compactId, item.heads.length, amount, currentAmount); - res = _.flatten(item.heads.map((head) => { - return fetchRecursive2(head, amount, currentAmount, all); - })); + // console.log("-", item.compactId, item.heads.length, amount, res.length); res.push(item); - console.log("res", res.length); + // console.log("res", res.length); + item.heads.map((head) => fetchRecursive2(head, amount - 1, all, res)); + // res = _.flatten(item.heads.map((head) => fetchRecursive2(head, amount, all, res))); + + // res = _.flatten(item.heads.map((head) => { + // return fetchRecursive2(head, amount, currentAmount, all); + // })); + // res.push(item); + // console.log("res2", res.length); } - return _.flatten(res); + return res; }; let allHashes = this._items.map((a) => a.hash); - let d = 0; - console.log("--1", other.items.length) + // let d = 0; + // console.log("--> items:", other.items.length) const res = _.flatten(other.items.map((e) => _.flatten(e.heads.map((f) => { - console.log("--2", e.heads.length, d) + // console.log("--> heads:", e.heads.length) // console.log(">", f, d) // fetchRecursive(allHashes, f, Math.ceil(MaxHistory / e.heads.length), d); - return _.flatten(fetchRecursive2(f, Math.ceil((MaxHistory - other.items.length) / e.heads.length), d, allHashes)); + // return _.flatten(fetchRecursive2(f, Math.ceil((MaxHistory - other.items.length - e.heads.length) / e.heads.length) + (e.heads.length % 2 === 0 ? 0 : 1), allHashes, [])); + const remaining = (MaxHistory); + // return _.flatten(fetchRecursive2(f, Math.floor(remaining / e.heads.length) + (remaining%2 === 0 ? 0 : 1), allHashes, [])); + return _.flatten(fetchRecursive2(f, MaxHistory, allHashes, [])); })))); - // console.log(res) - res.reverse().forEach((item) => { + // console.log("RES", res) + res.slice(0, MaxHistory).forEach((item) => { // console.log("ii", item.id) const indices = item.heads.map((k) => _.findIndex(this._items, (b) => b.hash === k)); // const indices = _.findIndex(this._items, (b) => b.hash === e); - const idx = indices.length > 0 ? Math.max(_.max(indices) + 1, 0) : this._items.length; + const idx = indices.length > 0 ? Math.max(_.max(indices) + 1, 0) : 0; this._items.splice(idx, 0, item) // this._items.splice(this._items.length - 1, 0, item) - console.log("added", item.compactId, "at", idx, item.data); + // console.log("added", item.compactId, "at", idx, item.data); }); - console.log(`--> Fetched ${res.length + other.items.length} items`); + // console.log(`--> Fetched ${res.length} items`); // console.log("--> Fetched", MaxHistory, "items from the history\n"); } diff --git a/test/list-tests.js b/test/list-tests.js index 0c40dab..c52cab5 100644 --- a/test/list-tests.js +++ b/test/list-tests.js @@ -179,8 +179,8 @@ describe('List', () => { assert.equal(list1._currentBatch.length, 1); assert.equal(list1._currentBatch[0].next.length, 2); - assert.equal(list1._currentBatch[0].next[0], 'B.0.1'); - assert.equal(list1._currentBatch[0].next[1], 'A.0.0'); + assert.equal(list1._currentBatch[0].next[0], 'A.0.0'); + assert.equal(list1._currentBatch[0].next[1], 'B.0.1'); done(); }); @@ -275,10 +275,10 @@ describe('List', () => { assert.equal(list1.ver, 0); assert.equal(list1._currentBatch.length, 0); assert.equal(list1._items.length, 3); - assert.equal(firstItem.id, 'B'); + assert.equal(firstItem.id, 'A'); assert.equal(firstItem.seq, 0); assert.equal(firstItem.ver, 0); - assert.equal(firstItem.data, 'helloB1'); + assert.equal(firstItem.data, 'helloA1'); done(); }); @@ -292,8 +292,6 @@ describe('List', () => { list1.join(list2); list2.join(list1); - assert.equal(list1.items.map((e) => e.compactId).join(", "), list2.items.map((e) => e.compactId).join(", ")) - const lastItem1 = list1.items[list1.items.length - 1]; assert.equal(list1.id, 'A'); @@ -301,10 +299,10 @@ describe('List', () => { assert.equal(list1.ver, 0); assert.equal(list1._currentBatch.length, 0); assert.equal(list1._items.length, 4); - assert.equal(lastItem1.id, 'A'); + assert.equal(lastItem1.id, 'B'); assert.equal(lastItem1.seq, 0); assert.equal(lastItem1.ver, 1); - assert.equal(lastItem1.data, 'helloA2'); + assert.equal(lastItem1.data, 'helloB2'); const lastItem2 = list2.items[list2.items.length - 1]; @@ -340,14 +338,14 @@ describe('List', () => { assert.equal(list2.ver, 0); assert.equal(list2._currentBatch.length, 0); assert.equal(list2._items.length, 4); - assert.equal(secondItem.id, 'A'); - assert.equal(secondItem.seq, 0); - assert.equal(secondItem.ver, 1); - assert.equal(secondItem.data, 'helloA2'); - assert.equal(lastItem.id, 'B'); - assert.equal(lastItem.seq, 1); - assert.equal(lastItem.ver, 0); - assert.equal(lastItem.data, 'helloB2'); + assert.equal(secondItem.id, 'B'); + assert.equal(secondItem.seq, 1); + assert.equal(secondItem.ver, 0); + assert.equal(secondItem.data, 'helloB2'); + assert.equal(lastItem.id, 'A'); + assert.equal(lastItem.seq, 0); + assert.equal(lastItem.ver, 1); + assert.equal(lastItem.data, 'helloA2'); done(); }); @@ -428,10 +426,10 @@ describe('List', () => { assert.equal(list4.ver, 2); assert.equal(list4._currentBatch.length, 2); assert.equal(list4._items.length, 8); - assert.equal(secondItem.id, 'B'); + assert.equal(secondItem.id, 'D'); assert.equal(secondItem.seq, 0); - assert.equal(secondItem.ver, 0); - assert.equal(secondItem.data, 'helloB1'); + assert.equal(secondItem.ver, 1); + assert.equal(secondItem.data, 'helloD2'); assert.equal(lastItem1.id, 'C'); assert.equal(lastItem1.seq, 3); assert.equal(lastItem1.ver, 1); @@ -493,9 +491,9 @@ describe('List', () => { list3.join(list1); assert.equal(list1._items.length, 19); - assert.equal(list1.items.map((e) => e.compactId).join(", "), "B.0.0, A.0.0, B.2.0, B.2.1, A.1.0, A.1.1, A.3.0, A.3.1, A.3.2, B.4.0, B.4.1, A.6.0, A.6.1, B.7.0, B.7.1, B.7.2, C.0.0, C.0.1, C.0.2") - assert.equal(list1.items.map((e) => e.compactId).join(", "), list2.items.map((e) => e.compactId).join(", ")) - assert.equal(list1.items.map((e) => e.compactId).join(", "), list3.items.map((e) => e.compactId).join(", ")) + assert.equal(list1.items.map((e) => e.compactId).join(", "), "A.0.0, B.0.0, A.1.0, A.1.1, B.2.0, B.2.1, A.3.0, A.3.1, A.3.2, B.4.0, B.4.1, A.6.0, A.6.1, B.7.0, B.7.1, B.7.2, C.0.0, C.0.1, C.0.2") + assert.equal(list2.items.map((e) => e.compactId).join(", "), "B.0.0, A.0.0, B.2.0, B.2.1, A.1.0, A.1.1, B.4.0, B.4.1, A.3.0, A.3.1, A.3.2, A.6.0, A.6.1, B.7.0, B.7.1, B.7.2, C.0.0, C.0.1, C.0.2") + assert.equal(list3.items.map((e) => e.compactId).join(", "), "C.0.0, C.0.1, C.0.2, B.0.0, A.0.0, B.2.0, B.2.1, A.1.0, A.1.1, B.4.0, B.4.1, A.3.0, A.3.1, A.3.2, A.6.0, A.6.1, B.7.0, B.7.1, B.7.2") done(); }); diff --git a/test/orbit-list-tests.js b/test/orbit-list-tests.js index c8e9fc2..31e1b3d 100644 --- a/test/orbit-list-tests.js +++ b/test/orbit-list-tests.js @@ -316,10 +316,10 @@ describe('OrbitList', async(function() { assert.equal(list1._currentBatch.length, 1); assert.equal(list1._currentBatch[0].next.length, 2); - assert.equal(list1._currentBatch[0].next[0].compactId, 'B.0.1'); - assert.equal(list1._currentBatch[0].next[0].hash, 'QmVmkwMoz4vnvHQwvFwqaoWCrjonsPpyJ6i436Zajht5ao'); - assert.equal(list1._currentBatch[0].next[1].compactId, 'A.0.0'); - assert.equal(list1._currentBatch[0].next[1].hash, 'QmYTUeiK82guFDyB9tJgHZuBpNkUqNyFBuajYrCsaxPXvW'); + assert.equal(list1._currentBatch[0].next[0].compactId, 'A.0.0'); + assert.equal(list1._currentBatch[0].next[0].hash, 'QmYTUeiK82guFDyB9tJgHZuBpNkUqNyFBuajYrCsaxPXvW'); + assert.equal(list1._currentBatch[0].next[1].compactId, 'B.0.1'); + assert.equal(list1._currentBatch[0].next[1].hash, 'QmVmkwMoz4vnvHQwvFwqaoWCrjonsPpyJ6i436Zajht5ao'); done(); })); @@ -336,8 +336,10 @@ describe('OrbitList', async(function() { assert.equal(list1._currentBatch.length, 2); assert.equal(list1._currentBatch[1].next.length, 2); - assert.equal(list1._currentBatch[1].next[0].compactId, 'B.0.1'); - assert.equal(list1._currentBatch[1].next[0].hash, 'QmVmkwMoz4vnvHQwvFwqaoWCrjonsPpyJ6i436Zajht5ao'); + assert.equal(list1._currentBatch[1].next[0].compactId, 'A.1.0'); + assert.equal(list1._currentBatch[1].next[0].hash, 'QmYHXzXaahAL9iChAUtVsvdncKfQf7ShEfveZnL7qvGfTT'); + assert.equal(list1._currentBatch[1].next[1].compactId, 'B.0.1'); + assert.equal(list1._currentBatch[1].next[1].hash, 'QmVmkwMoz4vnvHQwvFwqaoWCrjonsPpyJ6i436Zajht5ao'); done(); })); @@ -361,8 +363,10 @@ describe('OrbitList', async(function() { assert.equal(list1.items.length, 7); assert.equal(lastItem.next.length, 2); - assert.equal(lastItem.next[0].compactId, 'B.0.1'); - assert.equal(lastItem.next[0].hash, 'QmVmkwMoz4vnvHQwvFwqaoWCrjonsPpyJ6i436Zajht5ao'); + assert.equal(lastItem.next[0].compactId, 'A.2.0'); + assert.equal(lastItem.next[0].hash, 'QmTB6p2opEHV9LqKCH5vBBnXxYMa9EFbVuu5pDQNsKJVEN'); + assert.equal(lastItem.next[1].compactId, 'B.0.1'); + assert.equal(lastItem.next[1].hash, 'QmVmkwMoz4vnvHQwvFwqaoWCrjonsPpyJ6i436Zajht5ao'); done(); })); @@ -397,10 +401,10 @@ describe('OrbitList', async(function() { assert.equal(list1.items.length, 11); assert.equal(lastItem.next.length, 4); - assert.equal(lastItem.next[0].compactId, 'B.0.1'); - assert.equal(lastItem.next[0].hash, 'QmVmkwMoz4vnvHQwvFwqaoWCrjonsPpyJ6i436Zajht5ao'); - assert.equal(lastItem.next[1].compactId, 'A.4.0'); - assert.equal(lastItem.next[1].hash, 'QmSb6XTHZqptcndhgyDgqyifyBJNhic8XZh4vLmBiuK5VP'); + assert.equal(lastItem.next[0].compactId, 'A.4.0'); + assert.equal(lastItem.next[0].hash, 'QmShaXLSPmoVmzNineHRmS68gE7eajDjNdjtkV1fddob7w'); + assert.equal(lastItem.next[1].compactId, 'B.0.1'); + assert.equal(lastItem.next[1].hash, 'QmVmkwMoz4vnvHQwvFwqaoWCrjonsPpyJ6i436Zajht5ao'); assert.equal(lastItem.next[2].compactId, 'C.0.0'); assert.equal(lastItem.next[2].hash, 'QmTPfJRsLKPmxYrd82p16mry6vYTMoMFTH2jSbYVR8KvgD'); assert.equal(lastItem.next[3].compactId, 'D.0.2'); @@ -423,10 +427,10 @@ describe('OrbitList', async(function() { assert.equal(list1.ver, 0); assert.equal(list1._currentBatch.length, 0); assert.equal(list1._items.length, 3); - assert.equal(lastItem.id, 'A'); + assert.equal(lastItem.id, 'B'); assert.equal(lastItem.seq, 0); - assert.equal(lastItem.ver, 0); - assert.equal(lastItem.data, 'helloA1'); + assert.equal(lastItem.ver, 1); + assert.equal(lastItem.data, 'helloB2'); done(); })); @@ -447,10 +451,10 @@ describe('OrbitList', async(function() { assert.equal(list1.ver, 0); assert.equal(list1._currentBatch.length, 0); assert.equal(list1._items.length, 4); - assert.equal(lastItem1.id, 'A'); + assert.equal(lastItem1.id, 'B'); assert.equal(lastItem1.seq, 0); assert.equal(lastItem1.ver, 1); - assert.equal(lastItem1.data, 'helloA2'); + assert.equal(lastItem1.data, 'helloB2'); const lastItem2 = list2.items[list2.items.length - 1]; @@ -486,14 +490,14 @@ describe('OrbitList', async(function() { assert.equal(list2.ver, 0); assert.equal(list2._currentBatch.length, 0); assert.equal(list2._items.length, 4); - assert.equal(secondItem.id, 'B'); + assert.equal(secondItem.id, 'A'); assert.equal(secondItem.seq, 0); assert.equal(secondItem.ver, 0); - assert.equal(secondItem.data, 'helloB1'); - assert.equal(lastItem.id, 'B'); - assert.equal(lastItem.seq, 1); - assert.equal(lastItem.ver, 0); - assert.equal(lastItem.data, 'helloB2'); + assert.equal(secondItem.data, 'helloA1'); + assert.equal(lastItem.id, 'A'); + assert.equal(lastItem.seq, 0); + assert.equal(lastItem.ver, 1); + assert.equal(lastItem.data, 'helloA2'); done(); })); @@ -523,10 +527,10 @@ describe('OrbitList', async(function() { assert.equal(list1.ver, 0); assert.equal(list1._currentBatch.length, 0); assert.equal(list1._items.length, 8); - assert.equal(secondItem.id, 'B'); + assert.equal(secondItem.id, 'A'); assert.equal(secondItem.seq, 0); assert.equal(secondItem.ver, 1); - assert.equal(secondItem.data, 'helloB2'); + assert.equal(secondItem.data, 'helloA2'); assert.equal(lastItem.id, 'D'); assert.equal(lastItem.seq, 0); assert.equal(lastItem.ver, 1); @@ -574,10 +578,10 @@ describe('OrbitList', async(function() { assert.equal(list4.ver, 2); assert.equal(list4._currentBatch.length, 2); assert.equal(list4._items.length, 8); - assert.equal(secondItem.id, 'B'); + assert.equal(secondItem.id, 'D'); assert.equal(secondItem.seq, 0); - assert.equal(secondItem.ver, 0); - assert.equal(secondItem.data, 'helloB1'); + assert.equal(secondItem.ver, 1); + assert.equal(secondItem.data, 'helloD2'); assert.equal(lastItem1.id, 'C'); assert.equal(lastItem1.seq, 3); assert.equal(lastItem1.ver, 1);