From 0cc25913c06deac74961c31b4c7c7560ebfdcb5c Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Mon, 13 Oct 2025 12:46:56 -0700 Subject: [PATCH] Optimize event storage --- src/app/util/storage.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/app/util/storage.ts b/src/app/util/storage.ts index 1a0831a..19376b4 100644 --- a/src/app/util/storage.ts +++ b/src/app/util/storage.ts @@ -1,4 +1,3 @@ -import {verifiedSymbol} from "nostr-tools/pure" import { always, on, @@ -31,6 +30,7 @@ import { MESSAGE, DIRECT_MESSAGE, DIRECT_MESSAGE_FILE, + verifiedSymbol, } from "@welshman/util" import type {Zapper, TrustedEvent} from "@welshman/util" import type {RepositoryUpdate} from "@welshman/relay" @@ -122,19 +122,24 @@ const syncEvents = async () => { } for (const id of update.removed) { - added = added.filter(event => !update.removed.has(event.id)) removed.add(id) } } if (removed.size > 0) { - for (const [shard, chunk] of groupBy(last, Array.from(removed))) { + added = added.filter(e => !removed.has(e.id)) + + const removedByShard = groupBy(id => last(id), removed) + const addedByShard = groupBy(e => last(e.id), added) + const shards = new Set([...removedByShard.keys(), ...addedByShard.keys()]) + + for (const shard of shards) { + const removedInShard = removedByShard.get(shard) + const addedInShard = addedByShard.get(shard) || [] const current = await collection.getShard(shard) - const modified = concat( - current.filter(e => !chunk.includes(e.id)), - added, - ) - const pruned = sortBy(e => -rankEvent(e), modified).slice(0, 10_000) + const filtered = current.filter(e => !removedInShard?.includes(e.id)) + const sorted = sortBy(e => -rankEvent(e), concat(filtered, addedInShard)) + const pruned = sorted.slice(0, 10_000) await collection.setShard(shard, pruned) }