Skip to main content

FileStorage

Struct FileStorage 

Source
pub struct FileStorage { /* private fields */ }
Expand description

File-based storage manager

Manages persistent storage of documents and indices on the filesystem. Uses atomic writes to prevent corruption and supports configurable compression.

§Directory Structure

base_path/
├── doc1.data       # Serialized and compressed document
├── doc1.meta       # Metadata for document
├── index1.data     # Serialized and compressed index
└── index1.meta     # Metadata for index

Implementations§

Source§

impl FileStorage

Source

pub fn new(base_path: impl AsRef<Path>) -> Result<Self>

Create new file storage at the specified path

Creates the directory if it doesn’t exist. Uses no compression by default.

§Arguments
  • base_path - Directory path for storage
§Returns
  • Result<Self> - New FileStorage instance
§Errors

Returns error if directory creation fails or path is invalid.

§Examples
let storage = FileStorage::new("/tmp/my_storage").unwrap();
Source

pub fn with_codec(base_path: impl AsRef<Path>, codec: Codec) -> Result<Self>

Create file storage with specific compression codec

§Arguments
  • base_path - Directory path for storage
  • codec - Compression codec to use
§Returns
  • Result<Self> - New FileStorage instance
§Errors

Returns error if directory creation fails or path is invalid.

§Examples
let storage = FileStorage::with_codec("/tmp/my_storage", Codec::Gzip).unwrap();
Source

pub fn save_document( &self, id: &str, document: &Document, ) -> Result<CompressionStats>

Save document with compression

§Arguments
  • id - Unique identifier for the document
  • document - Document to save
§Returns
  • Result<CompressionStats> - Compression statistics
§Errors

Returns error if serialization or writing fails.

§Examples
let storage = FileStorage::new("/tmp/storage")?;
let doc = Document {
    id: "doc1".to_string(),
    content: "Test".to_string(),
    embedding: vec![0.1; 384],
    metadata: None,
};
let stats = storage.save_document("doc1", &doc)?;
println!("Saved with ratio: {:.2}", stats.ratio);
Source

pub fn load_document(&self, id: &str) -> Result<Document>

Load document

§Arguments
  • id - Unique identifier for the document
§Returns
  • Result<Document> - Loaded document
§Errors

Returns error if document doesn’t exist or deserialization fails.

§Examples
let storage = FileStorage::new("/tmp/storage")?;
let doc = storage.load_document("doc1")?;
println!("Loaded: {}", doc.id);
Source

pub fn save_flat_index( &self, name: &str, index: &FlatIndexWrapper, ) -> Result<CompressionStats>

Save FlatIndex

§Arguments
  • name - Name for the index
  • index - FlatIndex to save
§Returns
  • Result<CompressionStats> - Compression statistics
§Errors

Returns error if serialization or writing fails.

Source

pub fn load_flat_index(&self, name: &str) -> Result<FlatIndexWrapper>

Load FlatIndex

§Arguments
  • name - Name of the index
§Returns
  • Result<FlatIndex> - Loaded index
§Errors

Returns error if index doesn’t exist or deserialization fails.

Source

pub fn save_hnsw_index( &self, name: &str, index: &HNSWIndexWrapper, ) -> Result<CompressionStats>

Save HNSWIndex

§Arguments
  • name - Name for the index
  • index - HNSWIndex to save
§Returns
  • Result<CompressionStats> - Compression statistics
§Errors

Returns error if serialization or writing fails.

Source

pub fn load_hnsw_index(&self, name: &str) -> Result<HNSWIndexWrapper>

Load HNSWIndex

§Arguments
  • name - Name of the index
§Returns
  • Result<HNSWIndex> - Loaded index
§Errors

Returns error if index doesn’t exist or deserialization fails.

Source

pub fn delete(&self, name: &str) -> Result<()>

Delete item from storage

Removes both the data and metadata files.

§Arguments
  • name - Name of the item to delete
§Returns
  • Result<()> - Ok if successful
§Errors

Returns error if deletion fails. Does not error if item doesn’t exist.

§Examples
let storage = FileStorage::new("/tmp/storage")?;
storage.delete("doc1")?;
Source

pub fn list(&self) -> Result<Vec<String>>

List all items in storage

Returns names of all stored items (without extensions).

§Returns
  • Result<Vec<String>> - List of item names
§Errors

Returns error if directory reading fails.

§Examples
let storage = FileStorage::new("/tmp/storage")?;
let items = storage.list()?;
for item in items {
    println!("Found: {}", item);
}
Source

pub fn get_metadata(&self, name: &str) -> Result<StorageMetadata>

Get metadata for an item

§Arguments
  • name - Name of the item
§Returns
  • Result<StorageMetadata> - Item metadata
§Errors

Returns error if metadata doesn’t exist or can’t be read.

§Examples
let storage = FileStorage::new("/tmp/storage")?;
let meta = storage.get_metadata("doc1")?;
println!("Type: {}, Size: {} bytes", meta.item_type, meta.compressed_size);
Source

pub fn total_size(&self) -> Result<u64>

Get total storage size in bytes

Calculates the sum of all data and metadata files.

§Returns
  • Result<u64> - Total size in bytes
§Errors

Returns error if directory reading fails.

§Examples
let storage = FileStorage::new("/tmp/storage")?;
let size = storage.total_size()?;
println!("Storage uses {} bytes", size);
Source

pub fn clear(&self) -> Result<()>

Clear all storage

Removes all data and metadata files from storage.

§Returns
  • Result<()> - Ok if successful
§Errors

Returns error if file deletion fails.

§Examples
let storage = FileStorage::new("/tmp/storage")?;
storage.clear()?;
Source

pub fn exists(&self, name: &str) -> bool

Check if item exists in storage

§Arguments
  • name - Name of the item
§Returns
  • bool - true if item exists
§Examples
let storage = FileStorage::new("/tmp/storage")?;
if storage.exists("doc1") {
    println!("Document exists!");
}

Trait Implementations§

Source§

impl Debug for FileStorage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V