Skip to content

Commit cc57c7f

Browse files
committed
chore: cleand up duplicates
1 parent cba3cac commit cc57c7f

5 files changed

Lines changed: 34 additions & 520 deletions

File tree

time_based_storage/src/time_based_storage/base.py

Lines changed: 0 additions & 117 deletions
This file was deleted.

time_based_storage/src/time_based_storage/core/heap.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ def get_range(self, start_time: datetime, end_time: datetime) -> List[T]:
4444
List of values within the specified time range
4545
"""
4646
# Find the start index using binary search
47-
start_idx = bisect.bisect_left(self._heap, (start_time, None))
47+
start_idx = bisect.bisect_left([ts for ts, _ in self._heap], start_time)
4848
# Find the end index using binary search
49-
end_idx = bisect.bisect_right(self._heap, (end_time, None))
50-
51-
# Return values in the range
49+
end_idx = bisect.bisect_right([ts for ts, _ in self._heap], end_time)
50+
51+
# Return values within the range
5252
return [value for _, value in self._heap[start_idx:end_idx]]
5353

5454
def get_duration(self, duration: float) -> List[T]:
@@ -61,16 +61,31 @@ def get_duration(self, duration: float) -> List[T]:
6161
Returns:
6262
List of values within the specified duration
6363
"""
64-
if not self._heap:
65-
return []
66-
6764
now = datetime.now()
6865
start_time = now.fromtimestamp(now.timestamp() - duration)
6966
return self.get_range(start_time, now)
7067

71-
def clear(self) -> None:
72-
"""Clear all stored values."""
73-
self._heap.clear()
68+
def get_earliest(self) -> Optional[Tuple[datetime, T]]:
69+
"""
70+
Get the earliest value in the storage.
71+
72+
Returns:
73+
Tuple of (timestamp, value) for the earliest entry, or None if empty
74+
"""
75+
return self._heap[0] if self._heap else None
76+
77+
def get_latest(self) -> Optional[Tuple[datetime, T]]:
78+
"""
79+
Get the latest value in the storage.
80+
81+
Returns:
82+
Tuple of (timestamp, value) for the latest entry, or None if empty
83+
"""
84+
if not self._heap:
85+
return None
86+
# Find the latest timestamp using binary search
87+
latest_idx = bisect.bisect_right([ts for ts, _ in self._heap], datetime.max) - 1
88+
return self._heap[latest_idx] if latest_idx >= 0 else None
7489

7590
def get_all(self) -> List[T]:
7691
"""
@@ -101,7 +116,7 @@ def get_value_at(self, timestamp: datetime) -> Optional[T]:
101116
The value at the specified timestamp, or None if not found
102117
"""
103118
# Use binary search to find the timestamp
104-
idx = bisect.bisect_left(self._heap, (timestamp, None))
119+
idx = bisect.bisect_left([ts for ts, _ in self._heap], timestamp)
105120
if idx < len(self._heap) and self._heap[idx][0] == timestamp:
106121
return self._heap[idx][1]
107122
return None
@@ -117,11 +132,11 @@ def remove(self, timestamp: datetime) -> bool:
117132
True if the value was removed, False if not found
118133
"""
119134
# Use binary search to find the timestamp
120-
idx = bisect.bisect_left(self._heap, (timestamp, None))
135+
idx = bisect.bisect_left([ts for ts, _ in self._heap], timestamp)
121136
if idx < len(self._heap) and self._heap[idx][0] == timestamp:
122-
# Remove the item and maintain heap property
123-
self._heap[idx] = self._heap[-1]
124-
self._heap.pop()
137+
# Remove the item at the found index
138+
self._heap.pop(idx)
139+
# Reheapify the list
125140
heapq.heapify(self._heap)
126141
return True
127142
return False
@@ -143,3 +158,7 @@ def is_empty(self) -> bool:
143158
True if the storage is empty, False otherwise
144159
"""
145160
return len(self._heap) == 0
161+
162+
def clear(self) -> None:
163+
"""Clear all values from the storage."""
164+
self._heap.clear()

time_based_storage/src/time_based_storage/heap.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)