From d3cd9c96ff2fb00d07629bad8b2f128fa47827a5 Mon Sep 17 00:00:00 2001
From: Sanjay <sanjayprabhu@gmail.com>
Date: Mon, 5 Feb 2024 08:26:29 -0800
Subject: [PATCH] fix: don't force fetch message count for non-existent fids
 (#1653)

---
 apps/hubble/src/storage/stores/storageCache.ts      | 4 ++--
 apps/hubble/src/storage/stores/store.ts             | 2 +-
 apps/hubble/src/storage/stores/storeEventHandler.ts | 4 ++--
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/apps/hubble/src/storage/stores/storageCache.ts b/apps/hubble/src/storage/stores/storageCache.ts
index 99642f6280..f85f9c349f 100644
--- a/apps/hubble/src/storage/stores/storageCache.ts
+++ b/apps/hubble/src/storage/stores/storageCache.ts
@@ -132,9 +132,9 @@ export class StorageCache {
     log.info({ timeTakenMs: Date.now() - start, totalFids }, "storage cache prepopulation finished");
   }
 
-  async getMessageCount(fid: number, set: UserMessagePostfix): HubAsyncResult<number> {
+  async getMessageCount(fid: number, set: UserMessagePostfix, forceFetch = true): HubAsyncResult<number> {
     const key = makeKey(fid, set);
-    if (this._counts.get(key) === undefined) {
+    if (this._counts.get(key) === undefined && forceFetch) {
       let total = 0;
       await this._db.forEachIteratorByPrefix(
         makeMessagePrimaryKey(fid, set),
diff --git a/apps/hubble/src/storage/stores/store.ts b/apps/hubble/src/storage/stores/store.ts
index 2f276a1df2..f932f4f739 100644
--- a/apps/hubble/src/storage/stores/store.ts
+++ b/apps/hubble/src/storage/stores/store.ts
@@ -242,7 +242,7 @@ export abstract class Store<TAdd extends Message, TRemove extends Message> {
   async pruneMessages(fid: number): HubAsyncResult<number[]> {
     const commits: number[] = [];
 
-    const cachedCount = await this._eventHandler.getCacheMessageCount(fid, this._postfix);
+    const cachedCount = await this._eventHandler.getCacheMessageCount(fid, this._postfix, false);
     const units = await this._eventHandler.getCurrentStorageUnitsForFid(fid);
 
     if (units.isErr()) {
diff --git a/apps/hubble/src/storage/stores/storeEventHandler.ts b/apps/hubble/src/storage/stores/storeEventHandler.ts
index 62608ab8fc..419c954298 100644
--- a/apps/hubble/src/storage/stores/storeEventHandler.ts
+++ b/apps/hubble/src/storage/stores/storeEventHandler.ts
@@ -237,8 +237,8 @@ class StoreEventHandler extends TypedEmitter<StoreEvents> {
     });
   }
 
-  async getCacheMessageCount(fid: number, set: UserMessagePostfix): HubAsyncResult<number> {
-    return this._storageCache.getMessageCount(fid, set);
+  async getCacheMessageCount(fid: number, set: UserMessagePostfix, forceFetch = true): HubAsyncResult<number> {
+    return this._storageCache.getMessageCount(fid, set, forceFetch);
   }
 
   async getEarliestTsHash(fid: number, set: UserMessagePostfix): HubAsyncResult<Uint8Array | undefined> {