Rename index to OplogStore

This commit is contained in:
haad 2025-05-09 12:36:24 +02:00
parent 46160978f8
commit 5dc8ea25fd
2 changed files with 18 additions and 19 deletions

View File

@ -11,7 +11,7 @@ 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 ConflictResolution from './conflict-resolution.js' import ConflictResolution from './conflict-resolution.js'
import OplogIndex from './oplog-index.js' import OplogStore from './oplog-store.js'
const { LastWriteWins, NoZeroes } = ConflictResolution const { LastWriteWins, NoZeroes } = ConflictResolution
@ -76,7 +76,7 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
access = access || await DefaultAccessController() access = access || await DefaultAccessController()
// Index and storage of entries for this Log // Index and storage of entries for this Log
const index = await OplogIndex({ logHeads, entryStorage, indexStorage, headsStorage, encryption }) const oplogStore = await OplogStore({ logHeads, entryStorage, indexStorage, headsStorage, encryption })
// Conflict-resolution sorting function // Conflict-resolution sorting function
sortFn = NoZeroes(sortFn || LastWriteWins) sortFn = NoZeroes(sortFn || LastWriteWins)
@ -105,7 +105,7 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
* @instance * @instance
*/ */
const heads = async () => { const heads = async () => {
const heads_ = await index.heads() const heads_ = await oplogStore.heads()
return heads_.sort(sortFn).reverse() return heads_.sort(sortFn).reverse()
} }
@ -136,11 +136,11 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
if (!hash) { if (!hash) {
throw new Error('hash is required') throw new Error('hash is required')
} }
return index.get(hash) return oplogStore.get(hash)
} }
const has = async (hash) => { const has = async (hash) => {
return index.has(hash) return oplogStore.has(hash)
} }
/** /**
@ -185,8 +185,8 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
throw new Error(`Could not append entry:\nKey "${identity.hash}" is not allowed to write to the log`) throw new Error(`Could not append entry:\nKey "${identity.hash}" is not allowed to write to the log`)
} }
// Add the entry to the index (=store and index it) // Add the entry to the oplog store (=store and index it)
const hash = await index.setHead(entry) const hash = await oplogStore.setHead(entry)
// Return the appended entry // Return the appended entry
return { ...entry, hash } return { ...entry, hash }
@ -216,7 +216,7 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
if (!isLog(log)) { if (!isLog(log)) {
throw new Error('Given argument is not an instance of Log') throw new Error('Given argument is not an instance of Log')
} }
await index.storage.merge(log.storage) await oplogStore.storage.merge(log.storage)
const heads = await log.heads() const heads = await log.heads()
for (const entry of heads) { for (const entry of heads) {
await joinEntry(entry) await joinEntry(entry)
@ -298,13 +298,12 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
await traverseAndVerify() await traverseAndVerify()
/* 4. Add missing entries to the index (=to the log) */ /* 4. Add missing entries to the oplog store (=to the log) */
/* 5. Add new entry to entries (for pinning) */ await oplogStore.addVerified(hashesToAdd.values())
await index.addVerified(hashesToAdd.values())
/* 6. Remove heads which new entries are connect to */ /* 6. Remove heads which new entries are connect to */
await index.removeHeads(connectedHeads.values()) await oplogStore.removeHeads(connectedHeads.values())
/* 7. Add the new entry to heads (=union with current heads) */ /* 7. Add the new entry to heads (=union with current heads) */
await index.addHead(entry) await oplogStore.addHead(entry)
return true return true
} }
@ -489,7 +488,7 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
* @instance * @instance
*/ */
const clear = async () => { const clear = async () => {
await index.clear() await oplogStore.clear()
} }
/** /**
@ -498,7 +497,7 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
* @instance * @instance
*/ */
const close = async () => { const close = async () => {
await index.close() await oplogStore.close()
} }
/** /**
@ -554,7 +553,7 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
close, close,
access, access,
identity, identity,
storage: index.storage, storage: oplogStore.storage,
encryption encryption
} }
} }

View File

@ -5,7 +5,7 @@ import MemoryStorage from '../storage/memory.js'
// Default storage for storing the Log and its entries. Default: Memory. Options: Memory, LRU, IPFS. // Default storage for storing the Log and its entries. Default: Memory. Options: Memory, LRU, IPFS.
const DefaultStorage = MemoryStorage const DefaultStorage = MemoryStorage
const OplogIndex = async ({ logHeads, entryStorage, headsStorage, indexStorage, encryption }) => { const OplogStore = async ({ logHeads, entryStorage, headsStorage, indexStorage, encryption }) => {
// Setup encryption and decryption functions // Setup encryption and decryption functions
const encryptEntryFn = encryption?.replication?.encrypt const encryptEntryFn = encryption?.replication?.encrypt
const decryptEntryFn = encryption?.replication?.decrypt const decryptEntryFn = encryption?.replication?.decrypt
@ -49,7 +49,7 @@ const OplogIndex = async ({ logHeads, entryStorage, headsStorage, indexStorage,
const setHead = async (entry) => { const setHead = async (entry) => {
const { hash, bytes } = await Entry.encode(entry, encryptEntryFn, encryptPayloadFn) const { hash, bytes } = await Entry.encode(entry, encryptEntryFn, encryptPayloadFn)
// The appended entry is now the latest head // The appended entry is now the latest head
await _heads.set([{ hash, ...entry }]) await _heads.set([{ hash, next: entry.next }])
// Add entry to the entry storage // Add entry to the entry storage
await _entries.put(hash, bytes) await _entries.put(hash, bytes)
// Add entry to the entry index // Add entry to the entry index
@ -109,4 +109,4 @@ const OplogIndex = async ({ logHeads, entryStorage, headsStorage, indexStorage,
} }
} }
export default OplogIndex export default OplogStore