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 indexImplementations§
Source§impl FileStorage
impl FileStorage
Sourcepub fn new(base_path: impl AsRef<Path>) -> Result<Self>
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();Sourcepub fn with_codec(base_path: impl AsRef<Path>, codec: Codec) -> Result<Self>
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 storagecodec- 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();Sourcepub fn save_document(
&self,
id: &str,
document: &Document,
) -> Result<CompressionStats>
pub fn save_document( &self, id: &str, document: &Document, ) -> Result<CompressionStats>
Save document with compression
§Arguments
id- Unique identifier for the documentdocument- 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);Sourcepub fn load_document(&self, id: &str) -> Result<Document>
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);Sourcepub fn save_flat_index(
&self,
name: &str,
index: &FlatIndexWrapper,
) -> Result<CompressionStats>
pub fn save_flat_index( &self, name: &str, index: &FlatIndexWrapper, ) -> Result<CompressionStats>
Sourcepub fn load_flat_index(&self, name: &str) -> Result<FlatIndexWrapper>
pub fn load_flat_index(&self, name: &str) -> Result<FlatIndexWrapper>
Sourcepub fn save_hnsw_index(
&self,
name: &str,
index: &HNSWIndexWrapper,
) -> Result<CompressionStats>
pub fn save_hnsw_index( &self, name: &str, index: &HNSWIndexWrapper, ) -> Result<CompressionStats>
Sourcepub fn load_hnsw_index(&self, name: &str) -> Result<HNSWIndexWrapper>
pub fn load_hnsw_index(&self, name: &str) -> Result<HNSWIndexWrapper>
Sourcepub fn delete(&self, name: &str) -> Result<()>
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")?;Sourcepub fn list(&self) -> Result<Vec<String>>
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);
}Sourcepub fn get_metadata(&self, name: &str) -> Result<StorageMetadata>
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);Sourcepub fn total_size(&self) -> Result<u64>
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);Trait Implementations§
Auto Trait Implementations§
impl Freeze for FileStorage
impl RefUnwindSafe for FileStorage
impl Send for FileStorage
impl Sync for FileStorage
impl Unpin for FileStorage
impl UnsafeUnpin for FileStorage
impl UnwindSafe for FileStorage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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