From 03f0b1141e7641973b520dded74c408efab14f3e Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Fri, 7 Mar 2025 12:25:02 +0100 Subject: [PATCH] refactor: Pin verified entry. --- src/oplog/oplog-index.js | 5 +++-- src/storage/composed.js | 11 +++++++++++ src/storage/ipfs-block.js | 12 +++++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/oplog/oplog-index.js b/src/oplog/oplog-index.js index 68509a0..8fd67b0 100644 --- a/src/oplog/oplog-index.js +++ b/src/oplog/oplog-index.js @@ -75,9 +75,10 @@ const OplogIndex = async ({ logHeads, entryStorage, headsStorage, indexStorage, /* 4. Add missing entries to the index (=to the log) */ for (const hash of hashes) { await _index.put(hash, true) - const bytes = await getBytes(hash) /* 5. Add new entry to entries (for pinning) */ - await _entries.put(hash, bytes) + if (_entries.persist) { + await _entries.persist(hash) + } } } diff --git a/src/storage/composed.js b/src/storage/composed.js index 91b9b8d..5c35309 100644 --- a/src/storage/composed.js +++ b/src/storage/composed.js @@ -114,6 +114,16 @@ const ComposedStorage = async (storage1, storage2) => { await storage2.clear() } + const persist = async (hash) => { + if (storage1.persist) { + await storage1.persist(hash) + } + + if (storage2.persist) { + await storage2.persist(hash) + } + } + /** * Calls close on each of the composed storages. * @function @@ -129,6 +139,7 @@ const ComposedStorage = async (storage1, storage2) => { put, get, del, + persist, iterator, merge, clear, diff --git a/src/storage/ipfs-block.js b/src/storage/ipfs-block.js index a1c4acc..c0b823b 100644 --- a/src/storage/ipfs-block.js +++ b/src/storage/ipfs-block.js @@ -41,9 +41,7 @@ const IPFSBlockStorage = async ({ ipfs, pin, timeout } = {}) => { const { signal } = new TimeoutController(timeout || DefaultTimeout) await ipfs.blockstore.put(cid, data, { signal }) - if (pin && !(await ipfs.pins.isPinned(cid))) { - await drain(ipfs.pins.add(cid)) - } + await persist(hash) } const del = async (hash) => {} @@ -65,6 +63,13 @@ const IPFSBlockStorage = async ({ ipfs, pin, timeout } = {}) => { } } + const persist = async (hash) => { + const cid = CID.parse(hash, base58btc) + if (pin && !(await ipfs.pins.isPinned(cid))) { + await drain(ipfs.pins.add(cid)) + } + } + const iterator = async function * () {} const merge = async (other) => {} @@ -77,6 +82,7 @@ const IPFSBlockStorage = async ({ ipfs, pin, timeout } = {}) => { put, del, get, + persist, iterator, merge, clear,