Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GitHub

This documentation is part of the "Projects with Books" initiative at zenOSmosis.

The source code for this project is available on GitHub.

DataStore: Read and Write Operations

Loading…

DataStore: Read and Write Operations

Relevant source files

The DataStore is the central entity of the rust-simd-r-drive storage engine. It provides a thread-safe, append-only interface for managing data with a focus on zero-copy reads and SIMD-accelerated writes. Operations are primarily defined through the DataStoreReader and DataStoreWriter traits, allowing for high-level abstractions over the underlying memory-mapped file.

Lifecycle and Initialization

A DataStore instance manages a buffered file writer, a memory-mapped view (Mmap), an atomic tail offset, and an in-memory KeyIndexer src/storage_engine/data_store.rs:27-33

Opening a Store

The DataStore::open() function is the primary entry point src/storage_engine/data_store.rs:84-117 It performs the following sequence:

  1. File Opening : Opens the file in read/write mode, creating it if it doesn’t exist via open_file_in_append_mode src/storage_engine/data_store.rs:161-170
  2. Mmap Initialization : Maps the file into memory via init_mmap src/storage_engine/data_store.rs:172-174
  3. Chain Recovery : Validates the backward-linked integrity chain to find the last valid entry via recover_valid_chain src/storage_engine/data_store.rs89
  4. Truncation : If corruption is detected (i.e., the valid chain is shorter than the file length), the file is truncated to the last known good state src/storage_engine/data_store.rs:91-104
  5. Index Building : The KeyIndexer is populated by scanning the validated file content src/storage_engine/data_store.rs108

Entity Relationship: Initialization

The following diagram illustrates the transition from a file system path to an active DataStore instance.

graph TD
    subgraph "Natural Language Space"
        Path["File System Path"]
Recovery["Integrity Recovery"]
end

    subgraph "Code Entity Space"
        DS_Open["DataStore::open(path)"]
OpenAppend["open_file_in_append_mode()"]
InitMmap["init_mmap()"]
Recover["recover_valid_chain()"]
BuildIdx["KeyIndexer::build()"]
DS_Struct["struct DataStore"]
end

 
   Path --> DS_Open
 
   DS_Open --> OpenAppend
 
   OpenAppend --> InitMmap
 
   InitMmap --> Recover
 
   Recovery -.-> Recover
 
   Recover --> BuildIdx
 
   BuildIdx --> DS_Struct

Title: DataStore Initialization Flow

Sources: src/storage_engine/data_store.rs:66-117 src/storage_engine/data_store.rs:161-174


Write Operations

Write operations are defined in the DataStoreWriter trait src/storage_engine/traits/writer.rs:4-152 All writes are append-only.

Standard and Batch Writes

Streaming Writes

For payloads larger than available RAM, write_stream allows writing data from any source implementing std::io::Read src/storage_engine/traits/writer.rs29

Write Data Flow

Title: Write Operation Data Flow

Sources: src/storage_engine/traits/writer.rs:5-138 src/storage_engine/data_store.rs:176-210


Read Operations

Read operations are defined in the DataStoreReader trait src/storage_engine/traits/reader.rs:4-160

Zero-Copy Access

The read(key) method returns an Option<EntryHandle> src/storage_engine/traits/reader.rs54

  1. The key is hashed, and the KeyIndexer provides the file offset src/storage_engine/traits/reader.rs:41-42
  2. The EntryHandle contains an Arc<Mmap>, allowing the application to access the payload as a slice (&[u8]) without copying data from the kernel buffer to user space src/storage_engine/traits/reader.rs:89-90

Batch and Hashed Reads

Streaming Reads

The EntryStream struct wraps an EntryHandle to provide a std::io::Read interface src/storage_engine/entry_stream.rs:44-47 Note that while the EntryHandle is zero-copy, the EntryStream::read() method does perform copies into the provided buffer src/storage_engine/entry_stream.rs:76-91

Sources: src/storage_engine/traits/reader.rs:4-138 src/storage_engine/entry_stream.rs:1-92


Management Operations

Deletion and Compaction

Copy, Move, and Rename

Summary of Traits

FeatureDataStoreReaderDataStoreWriter
Basic Opsread, exists, lenwrite, delete
Batchingbatch_read, batch_read_hashed_keysbatch_write, batch_write_with_key_hashes
StreamingEntryStream (via EntryHandle)write_stream
Metadataread_metadata, read_last_entryrename, copy, transfer

Sources: src/storage_engine/traits/reader.rs:4-160 src/storage_engine/traits/writer.rs:4-152 src/storage_engine/data_store.rs:420-475