mirror of
https://github.com/coracle-social/flotilla.git
synced 2025-12-10 10:57:04 +00:00
Optimize event storage
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
import {verifiedSymbol} from "nostr-tools/pure"
|
|
||||||
import {
|
import {
|
||||||
always,
|
always,
|
||||||
on,
|
on,
|
||||||
@@ -31,6 +30,7 @@ import {
|
|||||||
MESSAGE,
|
MESSAGE,
|
||||||
DIRECT_MESSAGE,
|
DIRECT_MESSAGE,
|
||||||
DIRECT_MESSAGE_FILE,
|
DIRECT_MESSAGE_FILE,
|
||||||
|
verifiedSymbol,
|
||||||
} from "@welshman/util"
|
} from "@welshman/util"
|
||||||
import type {Zapper, TrustedEvent} from "@welshman/util"
|
import type {Zapper, TrustedEvent} from "@welshman/util"
|
||||||
import type {RepositoryUpdate} from "@welshman/relay"
|
import type {RepositoryUpdate} from "@welshman/relay"
|
||||||
@@ -122,19 +122,24 @@ const syncEvents = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const id of update.removed) {
|
for (const id of update.removed) {
|
||||||
added = added.filter(event => !update.removed.has(event.id))
|
|
||||||
removed.add(id)
|
removed.add(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (removed.size > 0) {
|
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 current = await collection.getShard(shard)
|
||||||
const modified = concat(
|
const filtered = current.filter(e => !removedInShard?.includes(e.id))
|
||||||
current.filter(e => !chunk.includes(e.id)),
|
const sorted = sortBy(e => -rankEvent(e), concat(filtered, addedInShard))
|
||||||
added,
|
const pruned = sorted.slice(0, 10_000)
|
||||||
)
|
|
||||||
const pruned = sortBy(e => -rankEvent(e), modified).slice(0, 10_000)
|
|
||||||
|
|
||||||
await collection.setShard(shard, pruned)
|
await collection.setShard(shard, pruned)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user