Timestamped Field Store
MediumHash Map ยท Meta
Implement FieldStore, an in-memory store mapping a key and field to a string. Every method takes a timestamp t, strictly increasing across calls.
set(t, key, field, value)stores a value with no expiry, replacing any earlier one.set_with_ttl(t, key, field, value, ttl)is the same, but the value is visible only at times in[t, t + ttl).get(t, key, field)returns the visible value, or""if there is none.compare_and_set(t, key, field, expected, new)storesnewwith no expiry if the visible value equalsexpected, and returns whether it did.delete(t, key, field)removes the visible value and returns whether there was one.get_at(t, key, field, at_time)returns whatgetreturned, or would have returned, atat_time <= t.
Example 1
- Input:
- FieldStore(), set_with_ttl(1, "a", "x", "p", 4), get(4, "a", "x"), compare_and_set(5, "a", "x", "p", "q"), set(6, "a", "x", "r"), get_at(7, "a", "x", 3), get_at(8, "a", "x", 5), get_at(9, "a", "x", 6)
- Output:
- [null, null, "p", false, null, "p", "", "r"]
- Explanation
- The value expires at time 5, so the compare-and-set fails. Reading as of time 3 still sees
p; as of time 5 nothing is visible.
Example 2
- Input:
- FieldStore(), set(1, "u", "name", "ann"), set_with_ttl(2, "u", "name", "bea", 3), get(4, "u", "name"), get(5, "u", "name"), get_at(6, "u", "name", 1), get_at(7, "u", "name", 5)
- Output:
- [null, null, null, "bea", "", "ann", ""]
- Explanation
- When
beaexpires the field is empty;anndoes not come back.
Example 3
- Input:
- FieldStore(), set(1, "k", "f", "a"), delete(2, "k", "f"), compare_and_set(3, "k", "f", "a", "z"), get_at(4, "k", "f", 1)
- Output:
- [null, null, true, false, "a"]
Constraints
- At most
10^5calls are made 0 <= t <= 10^9, and1 <= ttl <= 10^90 <= at_time <= tin everyget_atcallkey,field,value,expectedandneware non-empty strings of at most 20 lowercase letters and digits