Design an in-memory database that stores values in a nested structure:
key
(string) →
field
(string) →
value
(string)
(key, field)
entry may optionally have a
TTL
(time-to-live) measured in the same time units as the provided timestamps.
ts
.
ts
, it must behave as if it does not exist.
You are given a sequence of queries. Each query is one of the operations below. Implement the database so that each query that produces an output returns the correct string.
Assume all timestamps are non-decreasing across the query list.
SET ts key field value
value
at
(key, field)
with
no expiration
.
SET_TTL ts key field value ttl
value
at
(key, field)
that is valid on the interval
[ts, ts + ttl)
.
GET ts key field
""
if missing/expired.
DELETE ts key field
ts
).
"true"
if something was deleted, else
"false"
.
SCAN ts key
key
formatted as:
field1(value1),field2(value2),...
field
.
""
.
SCAN_PREFIX ts key prefix
SCAN
, but include only fields whose names start with
prefix
.
BACKUP ts
ts
(including TTL state).
RESTORE ts at_timestamp
t_backup
satisfies
t_backup <= at_timestamp
.
ts
, TTL entries must have their expiration recomputed using the remaining TTL at backup time:
t_exp
originally, then at backup time
t_backup
it had remaining TTL
rem = t_exp - t_backup
.
ts
, its new expiration becomes
ts + rem
.
t_backup
must not appear in the restored state.
"true"
if a snapshot was found and restored, else
"false"
.)
Implement a function/class to process the queries in order and return the list of outputs (strings) for the queries that produce outputs (GET, DELETE, SCAN, SCAN_PREFIX, and any required outputs for BACKUP/RESTORE depending on your chosen interface).