Process appends and joins through a queue

This commit is contained in:
haad 2024-02-21 08:57:05 +01:00
parent 734f50ed7e
commit 16b3358355

View File

@ -7,6 +7,7 @@
* ["Merkle-CRDTs: Merkle-DAGs meet CRDTs"]{@link https://arxiv.org/abs/2004.00107} * ["Merkle-CRDTs: Merkle-DAGs meet CRDTs"]{@link https://arxiv.org/abs/2004.00107}
*/ */
import LRU from 'lru' import LRU from 'lru'
import PQueue from 'p-queue'
import Entry from './entry.js' import Entry from './entry.js'
import Clock, { tickClock } from './clock.js' import Clock, { tickClock } from './clock.js'
import Heads from './heads.js' import Heads from './heads.js'
@ -81,6 +82,9 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
const _heads = await Heads({ storage: headsStorage, heads: logHeads }) const _heads = await Heads({ storage: headsStorage, heads: logHeads })
// Conflict-resolution sorting function // Conflict-resolution sorting function
sortFn = NoZeroes(sortFn || LastWriteWins) sortFn = NoZeroes(sortFn || LastWriteWins)
// Internal queues for processing appends and joins in their call-order
const appendQueue = new PQueue({ concurrency: 1 })
const joinQueue = new PQueue({ concurrency: 1 })
/** /**
* Returns the clock of the log. * Returns the clock of the log.
@ -153,6 +157,7 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
* @instance * @instance
*/ */
const append = async (data, options = { referencesCount: 0 }) => { const append = async (data, options = { referencesCount: 0 }) => {
const task = async () => {
// 1. Prepare entry // 1. Prepare entry
// 2. Authorize entry // 2. Authorize entry
// 3. Store entry // 3. Store entry
@ -189,6 +194,9 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
return entry return entry
} }
return appendQueue.add(task)
}
/** /**
* Join two logs. * Join two logs.
* *
@ -232,6 +240,7 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
* @instance * @instance
*/ */
const joinEntry = async (entry) => { const joinEntry = async (entry) => {
const task = async () => {
/* 1. Check if the entry is already in the log and return early if it is */ /* 1. Check if the entry is already in the log and return early if it is */
const isAlreadyInTheLog = await has(entry.hash) const isAlreadyInTheLog = await has(entry.hash)
if (isAlreadyInTheLog) { if (isAlreadyInTheLog) {
@ -309,6 +318,9 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
return true return true
} }
return joinQueue.add(task)
}
/** /**
* TODO * TODO
* @memberof module:Log~Log * @memberof module:Log~Log