← 返回 openai 的题目列表Persistent Key-Value Store with Accurate Serialization/Deserialization
类型:online_judge
openai
Implement a KVStore class that supports the following operations: setting key-value pairs, retrieving values, and storing data to the file system with the ability to resume from it later. Both keys and values are strings, which may contain any characters. You must implement methods for serialization and deserialization. Accuracy in serialization and deserialization is crucial for ensuring data integrity.
Functional Details:
set(key: str, value: str): Set the specified key to the given value.
get(key: str) -> str: Return the value of the specified key. Return an empty string or appropriate error if the key does not exist.
save_to_file(filename: str): Store the current data into the specified file.
load_from_file(filename: str): Load data from the specified file and restore the state in memory.
Test Cases:
set('name', 'Alice'); get('name') should return 'Alice'.
set('foo', 'bar'); save_to_file('kv_store.txt'); load_from_file('kv_store.txt'); get('foo') should return 'bar'.
get('nonexistent') should be handled correctly, returning an empty string or error.
Key-value pairs containing special characters should be correctly handled.
The performance and correctness of serialization and deserialization should be ensured with a large amount of data.
Example
Input
set('name', 'Alice'); get('name')