← 返回 openai 的题目列表Implement a Durable Key-Value Store with File Segmentation
类型:online_judge
Problem: Implement a Durable Key-Value Store with File Segmentation
Implement a durable key-value store that persists data to disk and can reload it after a process restart.
Support the following operations:
SET key value: Insert or overwrite a key-value pair.
GET key: Return the value for a key; print NOT_FOUND if the key does not exist.
DEL key: Delete a key. Deleting a missing key produces no output.
REOPEN: Simulate closing and reopening the store. All successfully persisted data must remain readable afterward.
Persistence requirements
Data must be written to the supplied storage directory, not kept only in memory.
An initial version may store all data in one file.
Follow-up: no data file may exceed 1024 bytes. As data grows, automatically split it across multiple files.
Keys and values are UTF-8 strings. Each encoded key-value record is at most 1024 bytes.
After repeated SETs for the same key, a reload must return the newest value.
After DEL, the key must remain absent after a reload.
Input format
Line 1: storage directory path store_dir
Line 2: number of operations Q
Next Q lines: one command, formatted as SET key value, GET key, DEL key, or REOPEN.
value contains no newline but may contain spaces. In a SET command, everything after the first space following key is the value.
Output format
For every GET, print one line containing its value or NOT_FOUND.
Constraints
1 <= Q <= 10^4
The UTF-8 encoded length of each key and value is at most 512 bytes.
Every data file must be at most 1024 bytes.
Example
Input
/tmp/kv_case1
5
SET name alice
GET name
REOPEN
GET name
GET missing
Output
alice
alice
NOT_FOUND