← 返回 xai 的题目列表In-Memory Database Implementation
类型:online_judge
Implement an in-memory database task with multiple levels of features. The database supports records identified by string keys, each containing multiple string field-value pairs.
Level 1
Basic Operations:
set(key, field, value)
get(key, field)
delete(key, field)
Level 2
Adds read-only listing features:
scan(key) → returns field(value) list sorted lexicographically
scan_by_prefix(key, prefix) → same format, filtered by field prefix
Level 3
Introduces timestamps and TTL (Time-To-Live):
All operations have _at versions with a timestamp
set_at_with_ttl defines a valid interval [timestamp, timestamp + ttl)
Expired fields should not appear in get_at, scan_at, or prefix scans
Time always moves forward, and tests never mix timestamped and non-timestamped APIs
Level 4
Adds backup and restore:
backup(timestamp) stores the database state with remaining TTL
restore(timestamp, timestamp_to_restore) restores from the latest backup before or at the given time
TTL expiration must be recalculated after restore
Please implement the features, solve with code, and pass the related tests.
Example
Input
set user1 name Alice
get user1 name