Custom Data Store
We have a product problem to solve that requires us to implement a custom data store for key-value pairs
First, implement a Key-Value store with simple add, get, and delete functions.
We now want to add some advanced functionality to our key value store: keeping track of historical values. Modify the Key-Value Store to keep track of previous values for a given key. Specifically, we want to implement a function get_at_effective_date that takes a key and a specific time, and returns the value for the key at the specified point in time. Here is an example:
// At 7:00 AM, we initialize the key value store kvs = new KeyValueStore()
// At 7:30 AM, we add an entry for the key ‘a’ kvs.add(‘a’, 10)
// At 8:00 AM, we update the entry for the key ‘a’ kvs.add(‘a’, 20)
// At 8:30 AM, we ask for the current value of the key ‘a’ kvs.get(‘a’) // returns 20
// At 8:35 AM, we ask what the value was for the key ‘a’ at 7:35 AM kvs.get_at_effective_date(‘a’, DateTime.parse(‘7:35 AM’)) // returns 10
Here is one way to implement a key-value store with history tracking in Python:
|
|
The key addition is tracking timestamped historical values for each key, then binary searching to find the latest value on or before the requested effective date.
This allows reconstructing the state of the key-value store at any point in time.