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.

StorageFileImportExt: Filesystem Import

Loading…

StorageFileImportExt: Filesystem Import

Relevant source files

The StorageFileImportExt trait extends the DataStore with capabilities to ingest entire directory trees from the local filesystem into the append-only store. It specifically addresses the need for bulk data migration and streaming file access, using relative paths as keys to maintain directory structure within the schema-less storage engine.

Overview and Purpose

The primary goal of StorageFileImportExt is to provide a high-level API for synchronizing filesystem assets with a DataStore extensions/src/storage_file_import_ext.rs:12-59 It abstracts the complexity of recursive directory walking and ensures that file contents are written using efficient streaming I/O rather than loading entire files into memory.

Key features include:

Sources : extensions/src/storage_file_import_ext.rs:1-59 extensions/src/storage_file_import_ext.rs:117-130

Implementation Detail: Recursive Import

The import_dir_recursively function serves as the entry point for filesystem ingestion. It validates the source directory using standard std::path::Path checks before initiating the walk extensions/src/storage_file_import_ext.rs:68-75

Data Flow: Filesystem to DataStore

The following diagram illustrates how a file on disk is transformed into a namespaced entry within the DataStore.

Import Pipeline: Path Normalization and Streaming Write

graph TD
    subgraph "Local Filesystem"
        A["File: ./assets/images/logo.png"]
end

    subgraph "StorageFileImportExt.import_dir_recursively()"
        B["walkdir::WalkDir"]
C["strip_prefix()"]
D["to_namespaced_key()"]
E["Unix-style Path: 'images/logo.png'"]
end

    subgraph "DataStore Logic"
        F["NamespaceHasher (Optional)"]
G["DataStore.write_stream()"]
end

 
   A --> B
 
   B --> C
 
   C --> E
 
   E --> D
 
   D --> F
 
   F --> G
 
   G --> H[("DataStore (.bin file)")]

Sources : extensions/src/storage_file_import_ext.rs:62-96 extensions/src/storage_file_import_ext.rs:117-130

Key Functions

FunctionResponsibility
import_dir_recursivelyValidates base_dir, initiates WalkDir, and calls write_stream for each file found extensions/src/storage_file_import_ext.rs:62-96
to_namespaced_keyNormalizes Path components into a UTF-8 string joined by / and applies optional NamespaceHasher extensions/src/storage_file_import_ext.rs:117-130
read_file_entryA convenience wrapper that reconstructs the namespaced key from a relative path to call DataStore.read() extensions/src/storage_file_import_ext.rs:98-105
open_file_streamRetrieves an EntryHandle and converts it into an EntryStream for std::io::Read compatibility extensions/src/storage_file_import_ext.rs:107-114

Sources : extensions/src/storage_file_import_ext.rs:61-130

Streaming and Zero-Copy Access

A critical performance feature of this extension is its use of streaming for both ingestion and retrieval.

Streaming Write

Instead of reading a file into a buffer and passing it to DataStore.write(), import_dir_recursively opens a std::fs::File and passes it to DataStore.write_stream() extensions/src/storage_file_import_ext.rs:90-91 This allows the core engine to pipe data directly from the filesystem into the memory-mapped store, minimizing heap allocations.

Streaming Read (EntryStream)

When a file is retrieved via open_file_stream(), the system returns an EntryStream extensions/src/storage_file_import_ext.rs113 This object wraps an EntryHandle, which itself is a view into the memory-mapped storage file.

Code Entity Relationship: Read Streaming

classDiagram
    class DataStore {+read(key) EntryHandle}
    class StorageFileImportExt {+import_dir_recursively(base_dir, namespace)\n+open_file_stream(rel_path, namespace) EntryStream}
    class EntryHandle {
        +as_slice() &[u8]
    }
    class EntryStream {-handle: EntryHandle\n-pos: usize\n+read(buf) io::Result}

    DataStore <|-- StorageFileImportExt : implements
    StorageFileImportExt ..> EntryHandle : retrieves
    EntryStream o-- EntryHandle : wraps
    EntryStream ..|> "std::io::Read" : implements

Sources : extensions/src/storage_file_import_ext.rs:61-115 extensions/src/storage_file_import_ext.rs:2-3

Verification and Testing

The extension is verified through comprehensive integration tests that simulate directory imports and content validation.

Sources : extensions/tests/storage_file_import_tests.rs:1-193

Error Handling

Sources : extensions/src/storage_file_import_ext.rs:62-96