cognee_storage/
storage_trait.rs1use async_trait::async_trait;
2use std::path::PathBuf;
3use thiserror::Error;
4use tokio::fs::File;
5use tokio::io::{AsyncRead, AsyncWriteExt};
6
7#[derive(Debug, Clone, Error)]
8pub enum StorageError {
9 #[error("Not found: {0}")]
10 NotFound(String),
11
12 #[error("IO error: {0}")]
13 IoError(String),
14
15 #[error("Permission denied: {0}")]
16 PermissionDenied(String),
17
18 #[error("Invalid path: {0}")]
19 InvalidPath(String),
20}
21
22pub struct StorageWriter {
25 file: File,
26 location: String,
27}
28
29impl StorageWriter {
30 pub(crate) fn new(file: File, location: String) -> Self {
31 Self { file, location }
32 }
33
34 pub async fn write_chunk(&mut self, chunk: &[u8]) -> Result<(), StorageError> {
36 self.file
37 .write_all(chunk)
38 .await
39 .map_err(|e| StorageError::IoError(format!("Failed to write chunk: {e}")))
40 }
41
42 pub async fn finish(mut self) -> Result<String, StorageError> {
44 self.file
45 .flush()
46 .await
47 .map_err(|e| StorageError::IoError(format!("Failed to flush file: {e}")))?;
48 Ok(self.location)
49 }
50}
51
52#[async_trait]
53pub trait StorageTrait: Send + Sync {
54 async fn store(&self, data: &[u8], file_name: &str) -> Result<String, StorageError>;
56
57 async fn store_stream_dyn(
60 &self,
61 reader: &mut (dyn AsyncRead + Unpin + Send),
62 file_name: &str,
63 ) -> Result<String, StorageError>;
64
65 async fn create_writer(&self, file_name: &str) -> Result<StorageWriter, StorageError>;
68
69 async fn retrieve(&self, location: &str) -> Result<Vec<u8>, StorageError>;
71
72 async fn exists(&self, location: &str) -> Result<bool, StorageError>;
74
75 async fn delete(&self, location: &str) -> Result<(), StorageError>;
77
78 fn get_full_path(&self, location: &str) -> PathBuf;
80
81 fn base_path(&self) -> &str;
85
86 async fn initialize(&self) -> Result<(), StorageError>;
88
89 async fn remove_all(&self) -> Result<(), StorageError>;
94}
95
96#[async_trait]
99pub trait StorageExt: StorageTrait {
100 async fn store_stream<R: AsyncRead + Unpin + Send>(
103 &self,
104 reader: &mut R,
105 file_name: &str,
106 ) -> Result<String, StorageError> {
107 self.store_stream_dyn(reader, file_name).await
108 }
109}
110
111impl<T: StorageTrait + ?Sized> StorageExt for T {}