@@ -89,20 +89,29 @@ def get_last(self, timestamp: int, prefix: Any) -> Optional[Any]:
89
89
prefix = self ._ensure_bytes (prefix )
90
90
min_eligible_timestamp = self ._get_min_eligible_timestamp (prefix )
91
91
92
+ if timestamp < min_eligible_timestamp :
93
+ return None
94
+
92
95
lower_bound = self ._serialize_key (min_eligible_timestamp , prefix )
93
96
# +1 because upper bound is exclusive
94
97
upper_bound = self ._serialize_key (timestamp + 1 , prefix )
95
98
96
- value : Optional [bytes ] = None
97
-
98
99
deletes = self ._update_cache .get_deletes ()
99
100
updates = self ._update_cache .get_updates ().get (prefix , {})
100
101
101
102
cached = sorted (updates .items (), reverse = True )
102
- for cached_key , cached_value in cached :
103
- if lower_bound <= cached_key < upper_bound and cached_key not in deletes :
104
- value = cached_value
105
- break
103
+ value : Optional [bytes ] = None
104
+ cached_key : Optional [bytes ] = None
105
+ # First check the boundaries to skip the iteration if the cached values
106
+ # are outside the search boundaries
107
+ if cached and cached [0 ][0 ] >= lower_bound and cached [- 1 ][0 ] <= upper_bound :
108
+ for cached_key , cached_value in cached :
109
+ if (
110
+ lower_bound <= cached_key < upper_bound
111
+ and cached_key not in deletes
112
+ ):
113
+ value = cached_value
114
+ break
106
115
107
116
stored = self ._partition .iter_items (
108
117
lower_bound = lower_bound ,
@@ -113,7 +122,7 @@ def get_last(self, timestamp: int, prefix: Any) -> Optional[Any]:
113
122
if stored_key in deletes :
114
123
continue
115
124
116
- if value is None or cached_key < stored_key :
125
+ if value is None or ( cached_key and cached_key < stored_key ) :
117
126
value = stored_value
118
127
119
128
# We only care about the first not deleted item when
@@ -198,7 +207,7 @@ def _serialize_key(self, key: Union[int, bytes], prefix: bytes) -> bytes:
198
207
return prefix + SEPARATOR + int_to_int64_bytes (key )
199
208
elif isinstance (key , bytes ):
200
209
return prefix + SEPARATOR + key
201
- raise ValueError (f"Invalid key type: { type (key )} " )
210
+ raise TypeError (f"Invalid key type: { type (key )} " )
202
211
203
212
def _get_min_eligible_timestamp (self , prefix : bytes ) -> int :
204
213
"""
0 commit comments