fix(devices): keep fractional seconds when finding the nearest valid value

key_to_value now finds the nearest valid value with the nearest-record
search of db_get_record and a new record_filter: from the target it walks
outwards on each side to the first record that holds the key, within the
window. Distances keep the fractional seconds of the record stamps, as
before the previous commit, whose window scan compared whole seconds and
picked a value 1.9 s away over one 0.1 s away. The walk also replaces
formatting the stamp of every record in the window (15 to 20 ms per
lookup with one record per second, now 0.1 to 0.3 ms; about 6 ms when
the key is missing in the window).

Additional changes
Database docs describe the record_filter of db_get_record

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 b2c5820925
commit c1340ff56c
4 changed files with 56 additions and 37 deletions
+5 -1
View File
@@ -373,7 +373,7 @@ db_insert_record(record, mark_dirty=True)
### Retrieval
```python
db_get_record(target_timestamp, time_window=None)
db_get_record(target_timestamp, time_window=None, record_filter=None)
```
**Search Strategies:**
@@ -384,6 +384,10 @@ db_get_record(target_timestamp, time_window=None)
| `UNBOUND_WINDOW` | Nearest record (unlimited search) |
| `Duration` | Nearest within symmetric window |
With `record_filter`, only records for which the filter returns `True` count, e.g. records
holding a value for a key (`key_to_value` uses this). The nearest-record search walks outwards
from the target to the first matching record on each side, within the window.
**Memory-First:** Checks in-memory index before querying database.
### Deletion