Fix ComposedStorage

This commit is contained in:
haad 2023-03-09 08:33:42 +02:00
parent bf1165fd08
commit 946aefd4f6

View File

@ -1,41 +1,51 @@
const ComposedStorage = async (...storages) => { // Compose storages:
// const storage1 = await ComposedStorage(await LRUStorage(), await LevelStorage())
// const storage2 = await ComposedStorage(storage1, await IPFSBlockStorage())
const ComposedStorage = async (storage1, storage2) => {
const put = async (hash, data) => { const put = async (hash, data) => {
for await (const storage of storages) { await storage1.put(hash, data)
await storage.put(hash, data) await storage2.put(hash, data)
}
} }
const get = async (hash) => { const get = async (hash) => {
for await (const storage of storages) { let value = await storage1.get(hash)
const value = await storage.get(hash) if (!value) {
value = await storage2.get(hash)
if (value) { if (value) {
await storage1.put(hash, value)
}
}
return value return value
} }
}
}
const iterator = async function * () { const iterator = async function * () {
return storages[0].iterator() const keys = []
for (const storage of [storage1, storage2]) {
for await (const [key, value] of storage.iterator()) {
if (!keys[key]) {
keys[key] = true
yield [key, value]
}
}
}
} }
const merge = async (other) => { const merge = async (other) => {
for await (const storage1 of storages) { await storage1.merge(other)
for await (const storage2 of storages) { await storage2.merge(other)
await storage1.merge(storage2) await other.merge(storage1)
} await other.merge(storage2)
}
} }
const clear = async () => { const clear = async () => {
for await (const storage of storages) { await storage1.clear()
await storage.clear() await storage2.clear()
}
} }
const close = async () => { const close = async () => {
for await (const storage of storages) { await storage1.close()
await storage.close() await storage2.close()
}
} }
return { return {
@ -49,3 +59,4 @@ const ComposedStorage = async (...storages) => {
} }
export default ComposedStorage export default ComposedStorage