Merge pull request #12 from orbitdb/fix/log-iterator

Fix log iterator
This commit is contained in:
Hayden Young 2023-02-22 19:51:31 +08:00 committed by GitHub
commit 96bd3eab1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 67 additions and 69 deletions

View File

@ -248,33 +248,34 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
const traversed = {}
// Current entry during traversal
let entry
// Start traversal
// Start traversal and process stack until it's empty (traversed the full log)
while (stack.length > 0) {
// Process stack until it's empty (traversed the full log)
// or until shouldStopFn returns true
const done = await shouldStopFn(entry)
if (done === true) {
break
}
// Get the next entry from the stack
entry = stack.pop()
const hash = entry.hash
// If we have an entry that we haven't traversed yet, process it
if (entry && !traversed[hash]) {
// Add to the hashes we've traversed
traversed[hash] = true
// Yield the current entry
yield entry
// Add hashes of next entries to the stack from entry's
// causal connection (next) and references to history (refs)
for (const nextHash of [...entry.next, ...entry.refs]) {
// Check if we've already traversed this entry
if (!traversed[nextHash]) {
// Fetch the next entry
const next = await get(nextHash)
if (next) {
// Add the next entry in front of the stack and sort
stack = [next, ...stack].sort(sortFn)
if (entry) {
const hash = entry.hash
// If we have an entry that we haven't traversed yet, process it
if (!traversed[hash]) {
// Yield the current entry
yield entry
// If we should stop traversing, stop here
const done = await shouldStopFn(entry)
if (done === true) {
break
}
// Add to the hashes we've traversed
traversed[hash] = true
// Add hashes of next entries to the stack from entry's
// causal connection (next) and references to history (refs)
for (const nextHash of [...entry.next, ...entry.refs]) {
// Check if we've already traversed this entry
if (!traversed[nextHash]) {
// Fetch the next entry
const next = await get(nextHash)
if (next) {
// Add the next entry in front of the stack and sort
stack = [next, ...stack].sort(sortFn)
}
}
}
}
@ -337,12 +338,11 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
const start = (lt || (lte || await heads())).filter(isDefined)
const end = (gt || gte) ? await get(gt || gte) : null
const amountToIterate = end || amount === -1
? -1
: (lte || lt ? amount - 1 : amount)
const amountToIterate = (end || amount === -1) ? -1 : amount
let count = 0
const shouldStopTraversal = async (entry) => {
count++
if (!entry) {
return false
}
@ -352,7 +352,6 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
if (end && Entry.isEqual(entry, end)) {
return true
}
count++
return false
}
@ -376,7 +375,7 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
}
if (useBuffer) {
const endIndex = buffer.keys.length - 1
const endIndex = buffer.keys.length
const startIndex = endIndex - amount
const keys = buffer.keys.slice(startIndex, endIndex)
for (const key of keys) {

View File

@ -29,7 +29,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
ipfsd = await startIpfs(IPFS, config.daemon1)
ipfs = ipfsd.api
const [identities, testIdentities] = await createTestIdentities([ipfs])
const [identities, testIdentities] = await createTestIdentities(ipfs)
identities1 = identities[0]
testIdentity1 = testIdentities[0]

View File

@ -30,7 +30,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
ipfsd = await startIpfs(IPFS, config.daemon1)
ipfs = ipfsd.api
const [identities, testIdentities] = await createTestIdentities([ipfs])
const [identities, testIdentities] = await createTestIdentities(ipfs)
identities1 = identities[0]
testIdentity1 = testIdentities[0]
@ -219,7 +219,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
}
strictEqual(all.length, 1)
deepStrictEqual(all.map(e => e.value), ['hello2'])
deepStrictEqual(all.map(e => e.value), ['hello1'])
})
it('returns next two items greater than root', async () => {
@ -229,7 +229,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
}
strictEqual(all.length, 2)
deepStrictEqual(all.map(e => e.value), ['hello2', 'hello3'])
deepStrictEqual(all.map(e => e.value), ['hello1', 'hello2'])
})
it('returns first item less than head', async () => {

View File

@ -1,16 +1,16 @@
import { deepStrictEqual, strictEqual } from 'assert'
import rimraf from 'rimraf'
import { Log, Entry } from '../src/oplog/index.js'
import { Identities } from '../src/identities/index.js'
import KeyStore from '../src/key-store.js'
import { Feed, Database } from '../src/db/index.js'
import { IPFSBlockStorage, LevelStorage } from '../src/storage/index.js'
import { Log, Entry } from '../../src/oplog/index.js'
import { Identities } from '../../src/identities/index.js'
import KeyStore from '../../src/key-store.js'
import { Feed, Database } from '../../src/db/index.js'
import { IPFSBlockStorage, LevelStorage } from '../../src/storage/index.js'
// Test utils
import { config, testAPIs, getIpfsPeerId, waitForPeers, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import connectPeers from './utils/connect-nodes.js'
import waitFor from './utils/wait-for.js'
import { createTestIdentities, cleanUpTestIdentities } from './fixtures/orbit-db-identity-keys.js'
import connectPeers from '../utils/connect-nodes.js'
import waitFor from '../utils/wait-for.js'
import { createTestIdentities, cleanUpTestIdentities } from '../fixtures/orbit-db-identity-keys.js'
const { sync: rmrf } = rimraf
const { createIdentity } = Identities

View File

@ -1,16 +1,16 @@
import { deepStrictEqual, strictEqual } from 'assert'
import rimraf from 'rimraf'
import { Log, Entry } from '../src/oplog/index.js'
import { Identities } from '../src/identities/index.js'
import KeyStore from '../src/key-store.js'
import { KeyValue, KeyValuePersisted, Database } from '../src/db/index.js'
import { IPFSBlockStorage, LevelStorage } from '../src/storage/index.js'
import { Log, Entry } from '../../src/oplog/index.js'
import { Identities } from '../../src/identities/index.js'
import KeyStore from '../../src/key-store.js'
import { KeyValue, KeyValuePersisted, Database } from '../../src/db/index.js'
import { IPFSBlockStorage, LevelStorage } from '../../src/storage/index.js'
// Test utils
import { config, testAPIs, getIpfsPeerId, waitForPeers, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import connectPeers from './utils/connect-nodes.js'
import waitFor from './utils/wait-for.js'
import { createTestIdentities, cleanUpTestIdentities } from './fixtures/orbit-db-identity-keys.js'
import connectPeers from '../utils/connect-nodes.js'
import waitFor from '../utils/wait-for.js'
import { createTestIdentities, cleanUpTestIdentities } from '../fixtures/orbit-db-identity-keys.js'
const { sync: rmrf } = rimraf
const { createIdentity } = Identities

View File

@ -40,7 +40,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
peerId1 = await getIpfsPeerId(ipfs1)
peerId2 = await getIpfsPeerId(ipfs2)
const [identities, testIdentities] = await createTestIdentities([ipfs1, ipfs2])
const [identities, testIdentities] = await createTestIdentities(ipfs1, ipfs2)
identities1 = identities[0]
identities2 = identities[1]
testIdentity1 = testIdentities[0]

View File

@ -40,7 +40,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
peerId1 = await getIpfsPeerId(ipfs1)
peerId2 = await getIpfsPeerId(ipfs2)
const [identities, testIdentities] = await createTestIdentities([ipfs1, ipfs2])
const [identities, testIdentities] = await createTestIdentities(ipfs1, ipfs2)
identities1 = identities[0]
identities2 = identities[1]
testIdentity1 = testIdentities[0]

View File

@ -28,7 +28,7 @@ const signingKeys = {
userD: userD_,
}
const createTestIdentities = async (ipfsInstances) => {
const createTestIdentities = async (ipfs1, ipfs2) => {
rmrf('./keys_1')
const keystore = new KeyStore('./keys_1')
@ -41,16 +41,13 @@ const createTestIdentities = async (ipfsInstances) => {
await keystore.addKey(key, value)
}
const identities = []
const testIdentities = []
// Create an identity for each peers
const identities1 = await Identities({ keystore, ipfs: ipfs1 })
const identities2 = await Identities({ keystore, ipfs: ipfs2 })
const testIdentity1 = await identities1.createIdentity({ id: 'userA' })
const testIdentity2 = await identities2.createIdentity({ id: 'userB' })
let i = 0
for (const ipfsInstance of ipfsInstances) {
identities.push(await Identities({ keystore, ipfs: ipfsInstance }))
testIdentities.push(await identities[i].createIdentity({ id: 'user'+(++i) }))
}
return [identities, testIdentities]
return [[identities1, identities2], [testIdentity1, testIdentity2]]
}
const cleanUpTestIdentities = async (identities) => {

View File

@ -89,6 +89,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
const result = await all(it)
strictEqual([...result].length, 10)
strictEqual(result[0].hash, startHash)
})
it('returns entries with lte and amount', async () => {
@ -146,7 +147,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
let i = 0
for await (const entry of it) {
strictEqual(entry.payload, 'entry' + (73 - i++))
strictEqual(entry.payload, 'entry' + (72 - i++))
}
strictEqual(i, amount)
@ -163,6 +164,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
const result = await all(it)
strictEqual([...result].length, amount)
strictEqual(result[result.length - 1].hash, startHash)
})
it('returns entries with gte and amount', async () => {
@ -175,7 +177,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
let i = 0
for await (const entry of it) {
strictEqual(entry.payload, 'entry' + (79 - i++))
strictEqual(entry.payload, 'entry' + (78 - i++))
}
strictEqual(i, amount)