From c563043af297f9179d7321213128e00b0dc8081f Mon Sep 17 00:00:00 2001 From: Christin Date: Fri, 20 Nov 2026 04:02:56 +0100 Subject: [PATCH] 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 Assisted-by: Claude Code (claude-opus-5-5) --- src/akkudoktoreos/core/databaseabc.py | 8 +++++++- tests/test_dataabcsequencedb.py | 28 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/akkudoktoreos/core/databaseabc.py b/src/akkudoktoreos/core/databaseabc.py index 43b788d2..d37705f6 100644 --- a/src/akkudoktoreos/core/databaseabc.py +++ b/src/akkudoktoreos/core/databaseabc.py @@ -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) diff --git a/tests/test_dataabcsequencedb.py b/tests/test_dataabcsequencedb.py index 4444bdc7..4006c013 100644 --- a/tests/test_dataabcsequencedb.py +++ b/tests/test_dataabcsequencedb.py @@ -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)