From abc5184e1955d65f5d05a2191b805c46bde89a07 Mon Sep 17 00:00:00 2001
From: Jack Robison <jackrobison@lbry.io>
Date: Wed, 12 Oct 2022 11:50:44 -0400
Subject: [PATCH] add future effective amount index

-increase lru cache size for effective amount column famlily to 64mb
---
 hub/db/common.py   |  1 +
 hub/db/prefixes.py | 43 ++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/hub/db/common.py b/hub/db/common.py
index 0b20ca9..1f01736 100644
--- a/hub/db/common.py
+++ b/hub/db/common.py
@@ -50,6 +50,7 @@ class DB_PREFIXES(enum.Enum):
     hashX_mempool_status = b'g'
     reposted_count = b'j'
     effective_amount = b'i'
+    future_effective_amount = b'k'
 
 
 COLUMN_SETTINGS = {}  # this is updated by the PrefixRow metaclass
diff --git a/hub/db/prefixes.py b/hub/db/prefixes.py
index d854f06..d0f929b 100644
--- a/hub/db/prefixes.py
+++ b/hub/db/prefixes.py
@@ -1783,7 +1783,7 @@ class EffectiveAmountPrefixRow(PrefixRow):
     prefix = DB_PREFIXES.effective_amount.value
     key_struct = struct.Struct(b'>20s')
     value_struct = struct.Struct(b'>QQ')
-
+    cache_size = 1024 * 1024 * 64
     key_part_lambdas = [
         lambda: b'',
         struct.Struct(b'>20s').pack
@@ -1810,6 +1810,46 @@ class EffectiveAmountPrefixRow(PrefixRow):
         return cls.pack_key(claim_hash), cls.pack_value(effective_amount, support_sum)
 
 
+class FutureEffectiveAmountKey(NamedTuple):
+    claim_hash: bytes
+
+
+class FutureEffectiveAmountValue(NamedTuple):
+    future_effective_amount: int
+
+
+class FutureEffectiveAmountPrefixRow(PrefixRow):
+    prefix = DB_PREFIXES.future_effective_amount.value
+    key_struct = struct.Struct(b'>20s')
+    value_struct = struct.Struct(b'>Q')
+    cache_size = 1024 * 1024 * 64
+
+    key_part_lambdas = [
+        lambda: b'',
+        struct.Struct(b'>20s').pack
+    ]
+
+    @classmethod
+    def pack_key(cls, claim_hash: bytes):
+        return super().pack_key(claim_hash)
+
+    @classmethod
+    def unpack_key(cls, key: bytes) -> FutureEffectiveAmountKey:
+        return FutureEffectiveAmountKey(*super().unpack_key(key))
+
+    @classmethod
+    def pack_value(cls, future_effective_amount: int) -> bytes:
+        return super().pack_value(future_effective_amount)
+
+    @classmethod
+    def unpack_value(cls, data: bytes) -> FutureEffectiveAmountValue:
+        return FutureEffectiveAmountValue(*cls.value_struct.unpack(data))
+
+    @classmethod
+    def pack_item(cls, claim_hash: bytes,  future_effective_amount: int):
+        return cls.pack_key(claim_hash), cls.pack_value(future_effective_amount)
+
+
 class PrefixDB(BasePrefixDB):
     def __init__(self, path: str, reorg_limit: int = 200, max_open_files: int = 64,
                  secondary_path: str = '', unsafe_prefixes: Optional[typing.Set[bytes]] = None):
@@ -1853,6 +1893,7 @@ class PrefixDB(BasePrefixDB):
         self.hashX_status = HashXStatusPrefixRow(db, self._op_stack)
         self.hashX_mempool_status = HashXMempoolStatusPrefixRow(db, self._op_stack)
         self.effective_amount = EffectiveAmountPrefixRow(db, self._op_stack)
+        self.future_effective_amount = FutureEffectiveAmountPrefixRow(db, self._op_stack)
 
 
 def auto_decode_item(key: bytes, value: bytes) -> Union[Tuple[NamedTuple, NamedTuple], Tuple[bytes, bytes]]: