db/model/
adapter.rs

1use async_trait::async_trait;
2
3use crate::{err::Error, util::get_absolute_path, SimpleTransaction};
4use std::{pin::Pin, sync::Arc};
5
6#[derive(Debug, Clone)]
7pub enum StorageVariant {
8	RelationalStore,
9	KeyValueStore,
10}
11
12#[derive(Debug, Clone)]
13pub enum StorageAdapterName {
14	RocksDB,
15	ReDB,
16	EchoDB,
17}
18
19#[derive(Debug, Clone)]
20pub struct StorageAdapter<T> {
21	pub name: StorageAdapterName,
22	pub path: String,
23	pub db_instance: Pin<Arc<T>>,
24	pub variant: StorageVariant,
25}
26
27impl<T> StorageAdapter<T> {
28	pub fn new(
29		name: StorageAdapterName,
30		path: String,
31		db_instance: T,
32		variant: StorageVariant,
33	) -> Result<Self, Error> {
34		Ok(StorageAdapter {
35			name,
36			path: get_absolute_path(&path),
37			db_instance: Arc::pin(db_instance),
38			variant,
39		})
40	}
41}
42
43#[async_trait]
44pub trait DatastoreAdapter {
45	type Transaction: SimpleTransaction;
46	// # Create new database transaction
47	// Set `rw` default to false means readable but not readable
48	async fn transaction(&self, rw: bool) -> Result<Self::Transaction, Error>;
49
50	fn default() -> Self
51	where
52		Self: Sized,
53	{
54		todo!();
55	}
56
57	fn spawn(&self) -> Self
58	where
59		Self: Sized,
60	{
61		todo!()
62	}
63
64	fn path(&self) -> &str;
65}