To allow reads to continue uninterrupted during background writes, LittDB uses an unflushed data map (a thread-safe key->value map).
Write Workflow:
- Immediately add the data to the
unflushed data map. - Write the data to the file asynchronously (e.g., using buffered file stream APIs).
- The write operation returns as soon as the data is in the map and the buffer.
Read Workflow:
To ensure consistency, LittDB searches in this specific order:
- The Cache: For high-speed access to recent data.
- The Unflushed Data Map: To catch data that has been written but not yet committed to disk.
- On Disk: Using the
keymap and the data file for committed data.
The Flush() Method:
Users can call Flush() to force synchronization. This method:
- Flushes all data in buffers to disk.
- Empties the
unflushed data map. - Writes the key-address pairs to the
keymap for each entry before removal. - Blocks until all operations are complete.
This pattern provides read-your-writes consistency: if a reader looks for data currently being written, it will find it in the unflushed data map. If it finds an entry in the keymap, the data is safely on disk.
// Conceptual usage of the Flush mechanism
db.put("my_key", "my_value");
// Data is now in the unflushed data map
db.Flush();
// Data is now guaranteed to be on disk and in the keymap