Skip to content

Commit 6c8d833

Browse files
verbose
1 parent 754c889 commit 6c8d833

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

TimeBasedKVStore/time_based_k_v_store.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,32 @@ def set(self, key: str, value: str, timestamp: int) -> None:
88

99
def get(self, key: str, timestamp: int) -> str:
1010
if key in self._dic:
11+
# fetch list at given key
1112
li = self._dic[key]
13+
# init L and R of the list
1214
l, r = 0, len(self._dic[key]) - 1
1315

16+
# return empty string if asked for what
17+
# the KV store does not have stored
1418
if li[l][1] > timestamp:
1519
return ""
20+
# return the last one if timestamp
21+
# is in range or is equal to
1622
elif li[r][1] <= timestamp:
1723
return li[r][0]
24+
25+
# do a BS on the TS
1826
else:
1927
while l <= r:
2028
mid = l + (r - l) // 2
2129

30+
# found
2231
if li[mid][1] == timestamp:
2332
return li[mid][0]
24-
33+
# adjust l
2534
if li[mid][1] < timestamp:
2635
l = mid + 1
36+
# adjust r
2737
else:
2838
r = mid - 1
2939

0 commit comments

Comments
 (0)