Skip to content

Commit 4e7f736

Browse files
committed
Correct docstrings
1 parent 3eeab55 commit 4e7f736

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

quixstreams/state/rocksdb/timestamped.py

+47-11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ def __init__(
4646
loads: LoadsFunc,
4747
changelog_producer: Optional[ChangelogProducer] = None,
4848
) -> None:
49+
"""
50+
Initializes a new `TimestampedPartitionTransaction`.
51+
52+
:param partition: The `TimestampedStorePartition` this transaction belongs to.
53+
:param dumps: The serialization function for keys/values.
54+
:param loads: The deserialization function for keys/values.
55+
:param changelog_producer: Optional `ChangelogProducer` for recording changes.
56+
"""
4957
super().__init__(
5058
partition=partition,
5159
dumps=dumps,
@@ -61,25 +69,23 @@ def __init__(
6169
)
6270

6371
@validate_transaction_status(PartitionTransactionStatus.STARTED)
64-
def get_last(
65-
self,
66-
timestamp: int,
67-
prefix: Any,
68-
) -> Optional[Any]:
72+
def get_last(self, timestamp: int, prefix: Any) -> Optional[Any]:
6973
"""Get the latest value for a prefix up to a given timestamp.
7074
7175
Searches both the transaction's update cache and the underlying RocksDB store
7276
to find the value associated with the given `prefix` that has the highest
7377
timestamp less than or equal to the provided `timestamp`.
7478
75-
The search prioritizes values from the update cache if their timestamps are
76-
more recent than those found in the store.
79+
The search considers both the update cache and the store. It returns the value
80+
associated with the key that has the numerically largest timestamp less than
81+
or equal to the provided `timestamp`. If multiple entries exist for the same
82+
prefix across the cache and store within the valid time range, the one with
83+
the highest timestamp is chosen.
7784
7885
:param timestamp: The upper bound timestamp (inclusive) in milliseconds.
7986
:param prefix: The key prefix.
8087
:return: The deserialized value if found, otherwise None.
8188
"""
82-
8389
prefix = self._ensure_bytes(prefix)
8490
min_eligible_timestamp = self._get_min_eligible_timestamp(prefix)
8591

@@ -130,7 +136,9 @@ def set_for_timestamp(
130136
to the parent `set` method. The parent method internally serializes these
131137
into a combined key before storing the value in the update cache.
132138
133-
Additionally, it triggers the expiration logic.
139+
Additionally, it updates the minimum eligible timestamp for the given prefix
140+
based on the `retention_ms`, which is used later during the flush process to
141+
expire old data.
134142
135143
:param timestamp: Timestamp associated with the value in milliseconds.
136144
:param value: The value to store.
@@ -144,7 +152,16 @@ def set_for_timestamp(
144152
)
145153
self._set_min_eligible_timestamp(prefix, min_eligible_timestamp)
146154

147-
def _flush(self, changelog_offset: Optional[int]):
155+
def _flush(self, changelog_offset: Optional[int]) -> None:
156+
"""
157+
Flushes the transaction.
158+
159+
This method first calls `_expire()` to remove outdated entries based on
160+
their timestamps and retention periods, then calls the parent class's
161+
`_flush()` method to persist changes.
162+
163+
:param changelog_offset: Optional offset for the changelog.
164+
"""
148165
self._expire()
149166
super()._flush(changelog_offset=changelog_offset)
150167

@@ -156,7 +173,6 @@ def _expire(self) -> None:
156173
This applies to both the in-memory update cache and the underlying
157174
RocksDB store within the current transaction.
158175
"""
159-
160176
updates = self._update_cache.get_updates()
161177
for prefix, cached in updates.items():
162178
min_eligible_timestamp = self._get_min_eligible_timestamp(prefix)
@@ -185,12 +201,32 @@ def _serialize_key(self, key: Union[int, bytes], prefix: bytes) -> bytes:
185201
raise ValueError(f"Invalid key type: {type(key)}")
186202

187203
def _get_min_eligible_timestamp(self, prefix: bytes) -> int:
204+
"""
205+
Retrieves the minimum eligible timestamp for a given prefix.
206+
207+
It first checks an in-memory cache (`self._min_eligible_timestamps`).
208+
If not found, it queries the underlying RocksDB store using `self.get()`.
209+
Defaults to 0 if no timestamp is found.
210+
211+
:param prefix: The key prefix (bytes).
212+
:return: The minimum eligible timestamp (int).
213+
"""
188214
cache = self._min_eligible_timestamps
189215
return (
190216
cache.timestamps.get(prefix) or self.get(key=cache.key, prefix=prefix) or 0
191217
)
192218

193219
def _set_min_eligible_timestamp(self, prefix: bytes, timestamp: int) -> None:
220+
"""
221+
Sets the minimum eligible timestamp for a given prefix.
222+
223+
Updates an in-memory cache (`self._min_eligible_timestamps`) and then
224+
persists this new minimum to the underlying RocksDB store using `self.set()`.
225+
The value is stored in a designated column family.
226+
227+
:param prefix: The key prefix (bytes).
228+
:param timestamp: The minimum eligible timestamp (int) to set.
229+
"""
194230
cache = self._min_eligible_timestamps
195231
cache.timestamps[prefix] = timestamp
196232
self.set(key=cache.key, value=timestamp, prefix=prefix, cf_name=cache.cf_name)

0 commit comments

Comments
 (0)