pub struct Storage { /* private fields */ }Expand description
Anda DB storage layer implementation based on object_store.
Implementations§
Source§impl Storage
impl Storage
Sourcepub async fn connect(
path: String,
object_store: Arc<dyn ObjectStore>,
config: StorageConfig,
) -> Result<Storage, DBError>
pub async fn connect( path: String, object_store: Arc<dyn ObjectStore>, config: StorageConfig, ) -> Result<Storage, DBError>
Connects to or initializes the storage at the given path.
Attempts to load existing metadata. If not found, initializes with the provided config.
§Arguments
path- The base path for this storage instance within the object store.object_store- The underlying object store implementation.config- The configuration for the storage layer.
§Errors
Returns DBError if metadata loading fails (other than NotFound) or initialization fails.
Sourcepub async fn store_metadata(
&self,
check_point: u64,
now_ms: u64,
) -> Result<(), DBError>
pub async fn store_metadata( &self, check_point: u64, now_ms: u64, ) -> Result<(), DBError>
Stores the current storage metadata (config and stats) to the object store.
This operation is rate-limited by last_saved timestamp to avoid frequent writes.
It increments the internal version counter upon successful save.
§Arguments
check_point- The current checkpoint value, used forcheck_point.now_ms- The current timestamp in milliseconds, used forlast_saved.
§Errors
Returns DBError if writing the metadata fails.
Sourcepub fn metadata(&self) -> StorageMetadata
pub fn metadata(&self) -> StorageMetadata
Returns a copy of the current storage metadata.
Sourcepub fn object_chunk_size(&self) -> usize
pub fn object_chunk_size(&self) -> usize
Returns the configured object chunk size.
Sourcepub fn stats(&self) -> StorageStats
pub fn stats(&self) -> StorageStats
Returns a copy of the current storage statistics.
Sourcepub async fn fetch<T>(
&self,
doc_path: &str,
) -> Result<(T, ObjectVersion), DBError>where
T: DeserializeOwned,
pub async fn fetch<T>(
&self,
doc_path: &str,
) -> Result<(T, ObjectVersion), DBError>where
T: DeserializeOwned,
Sourcepub async fn fetch_bytes(
&self,
doc_path: &str,
) -> Result<(Bytes, ObjectVersion), DBError>
pub async fn fetch_bytes( &self, doc_path: &str, ) -> Result<(Bytes, ObjectVersion), DBError>
Sourcepub async fn get<T>(
&self,
doc_path: &str,
) -> Result<(T, ObjectVersion), DBError>where
T: DeserializeOwned,
pub async fn get<T>(
&self,
doc_path: &str,
) -> Result<(T, ObjectVersion), DBError>where
T: DeserializeOwned,
Gets and deserializes a document, potentially using the cache.
If the cache is enabled and the object is found and considered “small”, it will be served from the cache. Otherwise, it fetches from the object store and potentially caches it if small enough.
§Arguments
doc_path- The relative path of the document within the storage base path.
§Errors
Returns DBError if the object is not found, fetching fails, or deserialization fails.
Sourcepub async fn stream_reader(
&self,
doc_path: &str,
) -> Result<Pin<Box<dyn AsyncRead>>, DBError>
pub async fn stream_reader( &self, doc_path: &str, ) -> Result<Pin<Box<dyn AsyncRead>>, DBError>
Sourcepub async fn create<T>(
&self,
doc_path: &str,
doc: &T,
) -> Result<ObjectVersion, DBError>where
T: Serialize,
pub async fn create<T>(
&self,
doc_path: &str,
doc: &T,
) -> Result<ObjectVersion, DBError>where
T: Serialize,
Creates a new document in the object store. Fails if the document already exists.
Serializes the document using ciborium and compresses it if enabled.
This method is suitable only for objects smaller than max_small_object_size.
§Arguments
doc_path- The relative path for the new document.doc- The document to serialize and store.
§Errors
Returns DBError if serialization fails, the object is too large, or the put operation fails (e.g., object already exists).
Sourcepub async fn put<T>(
&self,
doc_path: &str,
doc: &T,
version: Option<ObjectVersion>,
) -> Result<ObjectVersion, DBError>where
T: Serialize,
pub async fn put<T>(
&self,
doc_path: &str,
doc: &T,
version: Option<ObjectVersion>,
) -> Result<ObjectVersion, DBError>where
T: Serialize,
Puts (creates or overwrites/updates) a document in the object store.
Serializes the document using ciborium and compresses it if enabled.
This method is suitable only for objects smaller than max_small_object_size.
Use stream_writer for larger objects.
§Arguments
doc_path- The relative path of the document.doc- The document to serialize and store.version- IfSome, performs a conditional update based on the providedObjectVersion(e.g., ETag match). IfNone, overwrites unconditionally.
§Errors
Returns DBError if serialization fails, the object is too large, or the put operation fails (e.g., conditional update fails).
Sourcepub async fn put_bytes(
&self,
doc_path: &str,
data: Bytes,
mode: PutMode,
) -> Result<ObjectVersion, DBError>
pub async fn put_bytes( &self, doc_path: &str, data: Bytes, mode: PutMode, ) -> Result<ObjectVersion, DBError>
Puts raw bytes into the object store.
Compresses the data if enabled.
This method is suitable only for data smaller than max_small_object_size.
Use stream_writer for larger data.
§Arguments
doc_path- The relative path of the object.data- The raw bytes to store.mode- ThePutMode(Create, Overwrite, Update).
§Errors
Returns DBError if the data is too large or the put operation fails.
Sourcepub fn to_writer(&self, doc_path: &str, mode: PutMode) -> SingleWriter
pub fn to_writer(&self, doc_path: &str, mode: PutMode) -> SingleWriter
Creates an asynchronous writer (AsyncWrite) for writing small objects (< max_small_object_size).
Data is buffered in memory and written in a single put operation on poll_flush or poll_close.
Use stream_writer for large objects.
§Arguments
doc_path- The relative path of the object.mode- ThePutMode(Create, Overwrite, Update).
Sourcepub fn stream_writer(&self, doc_path: &str) -> Pin<Box<dyn AsyncWrite>>
pub fn stream_writer(&self, doc_path: &str) -> Pin<Box<dyn AsyncWrite>>
Creates an asynchronous writer (AsyncWrite) for streaming large objects.
Data is written in chunks using the underlying object store’s multipart upload or equivalent. Handles compression automatically if enabled.
§Arguments
doc_path- The relative path of the object.
Sourcepub fn list<T>(
&self,
prefix: Option<&str>,
offset: Option<&str>,
) -> BoxStream<'_, Result<(T, ObjectVersion), DBError>>where
T: DeserializeOwned + Send,
pub fn list<T>(
&self,
prefix: Option<&str>,
offset: Option<&str>,
) -> BoxStream<'_, Result<(T, ObjectVersion), DBError>>where
T: DeserializeOwned + Send,
Lists documents in the storage, optionally filtering by prefix and starting from an offset.
Fetches and deserializes each listed object. This can be inefficient for large listings.
Consider using object_store.list directly if only metadata is needed.
§Arguments
prefix- Optional path prefix to filter the listing.offset- Optional path to start the listing after.
§Returns
A stream of Result<(T, ObjectVersion), DBError>.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Storage
impl !RefUnwindSafe for Storage
impl Send for Storage
impl Sync for Storage
impl Unpin for Storage
impl !UnwindSafe for Storage
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for 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