Coding - Durable Key-Value Store Serialization
Implement a durable key-value store serialization system. The key-value store should be able to perform the following operations:
put(key, value): Insert or update a (key, value) pair into the store.get(key): Given a key, return the value associated with the key. If the key does not exist in the store, return null.remove(key): Remove the key-value pair with the given key from the store. If the key does not exist, do nothing.The system should be able to recover from a crash and continue operating with the latest state of the store. To achieve this, the system should periodically persist the state of the store to disk.
Constraints:
Hints:
Solution:
To implement a durable key-value store serialization system, you can follow these steps:
Here's a sample implementation in Python:
`python import pickle import os
class DurableKVStore: def init(self): self.store = {} self.log_file = "log.dat" self.state_file = "state.dat"
self.load_state()
def put(self, key, value):
self.store[key] = value
self.log("put", key, value)
def get(self, key):
return self.store.get(key, None)
def remove(self, key):
if key in self.store:
self.store.pop(key)
self.log("remove", key)
def log(self, op, key, value=None):
with open(self.log_file, "ab") as f:
pickle.dump((op, key, value), f)
def load_state(self):
if os.path.exists(self.state_file):
with open(self.state_file, "rb") as f:
self.store = pickle.load(f)
def save_state(self):
with open(self.state_file, "wb") as f:
pickle.dump(self.store, f)
def recover(self):
self.store = {}
with open(self.log_file, "rb") as f:
while True:
try:
op, key, value = pickle.load(f)
if op == "put":
self.store[key] = value
elif op == "remove":
if key in self.store:
self.store.pop(key)
except EOFError:
break
def persist(self):
self.save_state()
store = DurableKVStore() store.put("key1", "value1") print(store.get("key1")) # Output: value1 store.remove("key1") print(store.get("key1")) # Output: None `
This implementation uses a write-ahead log to record all operations and a hash table to store the key-value pairs in memory. The system periodically persists the state to disk and recovers from a crash by replaying the log.