llm_weaver/storage/mod.rs
1use async_trait::async_trait;
2use serde::{de::DeserializeOwned, Serialize};
3use std::fmt::{Debug, Display};
4
5use crate::{Config, TapestryFragment, TapestryId};
6
7#[cfg(feature = "rocksdb")]
8pub mod rocksdb;
9
10/// A storage handler trait designed for saving and retrieving fragments of a tapestry.
11///
12/// # Usage
13///
14/// Implementations of `TapestryChestHandler` should provide the storage and retrieval mechanisms
15/// tailored to specific use-cases or storage backends, such as databases, file systems, or
16/// in-memory stores.
17#[async_trait]
18pub trait TapestryChestHandler<T: Config> {
19 /// Defines the error type returned by the handler methods.
20 type Error: Display + Debug;
21
22 fn new() -> Self;
23
24 /// Saves a tapestry fragment.
25 ///
26 /// Tapestry fragments are stored incrementally, also refered to as "instances". Each instance
27 /// is identified by an integer, starting at 1 and incrementing by 1 for each new instance.
28 ///
29 /// This method is executed primarily by the [`crate::Loom::weave`] function.
30 ///
31 /// # Parameters
32 ///
33 /// - `tapestry_id`: Identifies the tapestry.
34 /// - `tapestry_fragment`: An instance of `TapestryFragment` to be stored.
35 /// - `increment`:
36 /// - A boolean flag indicating whether the tapestry instance should be incremented.
37 /// - This should typically be `true` when saving a new instance of [`TapestryFragment`],
38 /// and `false` when updating an existing one.
39 async fn save_tapestry_fragment<TID: TapestryId>(
40 &self,
41 tapestry_id: &TID,
42 tapestry_fragment: TapestryFragment<T>,
43 increment: bool,
44 ) -> crate::Result<u64>;
45 /// Save tapestry metadata.
46 ///
47 /// Based on application use cases, you can add aditional data for a given [`TapestryId`]
48 async fn save_tapestry_metadata<TID: TapestryId, M: Serialize + Debug + Clone + Send + Sync>(
49 &self,
50 tapestry_id: TID,
51 metadata: M,
52 ) -> crate::Result<()>;
53 /// Retrieves the index of a tapestry.
54 ///
55 /// Returns None if the tapestry does not exist.
56 async fn get_instance_index<TID: TapestryId>(
57 &self,
58 tapestry_id: TID,
59 ) -> crate::Result<Option<u16>>;
60 /// Retrieves the last tapestry fragment, or a fragment at a specified instance.
61 ///
62 /// # Parameters
63 ///
64 /// - `tapestry_id`: Identifies the tapestry.
65 /// - `instance`: The instance of the fragment to retrieve. If `None`, the method should
66 /// retrieve the last fragment.
67 ///
68 /// # Returns
69 ///
70 /// On successful retrieval, it returns `Ok(Some(TapestryFragment))` or `Ok(None)` if no
71 /// fragment was found.
72 async fn get_tapestry_fragment<TID: TapestryId>(
73 &self,
74 tapestry_id: TID,
75 instance: Option<u64>,
76 ) -> crate::Result<Option<TapestryFragment<T>>>;
77 /// Retrieves the last tapestry metadata, or a metadata at a specified instance.
78 async fn get_tapestry_metadata<TID: TapestryId, M: DeserializeOwned + Send + Sync>(
79 &self,
80 tapestry_id: TID,
81 ) -> crate::Result<Option<M>>;
82 /// Deletes a tapestry and all its instances.
83 async fn delete_tapestry<TID: TapestryId>(&self, tapestry_id: TID) -> crate::Result<()>;
84 /// Deletes a tapestry fragment.
85 async fn delete_tapestry_fragment<TID: TapestryId>(
86 &self,
87 tapestry_id: TID,
88 instance: Option<u64>,
89 ) -> crate::Result<()>;
90}