fix(database): load the closed upper bound of the db_get_record window

db_get_record accepts a record up to half the window after the target,
but loaded only [start, end) from storage. A stored record on the upper
bound, or in the last second of a window with a fractional half width,
was not found when that part of the store was not in memory yet, and
the loaded range then stayed marked as covered. db_get_record now loads
the last second of the window as well; key_to_value relies on it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: Claude Code (claude-opus-5-5)
This commit is contained in:
Christin
2026-11-28 08:08:37 +01:00
committed by Bobby Noelte
co-authored by Claude Opus 5.5
parent c1340ff56c
commit c563043af2
2 changed files with 35 additions and 1 deletions
+7 -1
View File
@@ -1416,7 +1416,13 @@ class DatabaseRecordProtocolMixin(
# load the relevant range
# in case of unbounded escalates to FULL
search_start, search_end = self._search_window(target_timestamp, time_window)
await self._db_ensure_loaded(search_start, search_end, center_timestamp=target_timestamp)
# The window is closed: also load the records of its last second.
load_end = (
self._db_timestamp_after(search_end)
if isinstance(search_end, DatabaseTimestamp)
else search_end
)
await self._db_ensure_loaded(search_start, load_end, center_timestamp=target_timestamp)
# Exact match first (works for all three cases once loaded)
record = self._db_record_index.get(target_timestamp, None)
+28
View File
@@ -300,6 +300,34 @@ class TestDataSequenceDatabaseProtocol:
assert sequence.records[0].temperature == 23.0
assert sequence.records[-1].temperature == 26.0
async def test_key_to_value_reads_the_window_end_from_storage(
self, async_database_instance
) -> None:
"""A stored value at the closed upper window bound is found with a cold cache."""
sequence = SampleDataSequence()
await _reset_sequence_state(sequence)
target = to_datetime("2024-01-01T10:00:00Z")
await sequence.db_insert_record(
SampleDataRecord(date_time=target.add(seconds=2), temperature=21.0)
)
await sequence.db_insert_record(
SampleDataRecord(
date_time=target.add(hours=1, seconds=1, microseconds=500000), temperature=22.0
)
)
await sequence.db_save_records()
# 4 s window: the value 2 s after the target is on the upper bound.
await _reset_sequence_state(sequence)
value = await sequence.key_to_value("temperature", target, time_window=to_duration(4))
assert value == 21.0
# 3.6 s window: the value 1.5 s after the target is in the last second of the window.
await _reset_sequence_state(sequence)
value = await sequence.key_to_value(
"temperature", target.add(hours=1), time_window=to_duration(3.6)
)
assert value == 22.0
async def test_iterate_records_triggers_lazy_load(self, async_database_instance):
sequence = SampleDataSequence()
await _reset_sequence_state(sequence)