2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-08-22 01:56:00 -04:00
|
|
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use crate::remote::RemoteDbHandlerPermissions;
|
|
|
|
use crate::sqlite::SqliteDbHandler;
|
|
|
|
use crate::sqlite::SqliteDbHandlerPermissions;
|
|
|
|
use crate::AtomicWrite;
|
|
|
|
use crate::Database;
|
|
|
|
use crate::DatabaseHandler;
|
|
|
|
use crate::QueueMessageHandle;
|
|
|
|
use crate::ReadRange;
|
|
|
|
use crate::SnapshotReadOptions;
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::OpState;
|
2023-10-31 07:13:57 -04:00
|
|
|
use denokv_proto::CommitResult;
|
|
|
|
use denokv_proto::ReadRangeOutput;
|
2023-12-05 08:21:46 -05:00
|
|
|
use denokv_proto::WatchStream;
|
2023-08-22 01:56:00 -04:00
|
|
|
|
|
|
|
pub struct MultiBackendDbHandler {
|
|
|
|
backends: Vec<(&'static [&'static str], Box<dyn DynamicDbHandler>)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MultiBackendDbHandler {
|
|
|
|
pub fn new(
|
|
|
|
backends: Vec<(&'static [&'static str], Box<dyn DynamicDbHandler>)>,
|
|
|
|
) -> Self {
|
|
|
|
Self { backends }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remote_or_sqlite<
|
|
|
|
P: SqliteDbHandlerPermissions + RemoteDbHandlerPermissions + 'static,
|
|
|
|
>(
|
|
|
|
default_storage_dir: Option<std::path::PathBuf>,
|
2023-10-31 07:13:57 -04:00
|
|
|
versionstamp_rng_seed: Option<u64>,
|
|
|
|
http_options: crate::remote::HttpOptions,
|
2023-08-22 01:56:00 -04:00
|
|
|
) -> Self {
|
|
|
|
Self::new(vec![
|
|
|
|
(
|
|
|
|
&["https://", "http://"],
|
2023-10-31 07:13:57 -04:00
|
|
|
Box::new(crate::remote::RemoteDbHandler::<P>::new(http_options)),
|
2023-08-22 01:56:00 -04:00
|
|
|
),
|
|
|
|
(
|
|
|
|
&[""],
|
2023-10-31 07:13:57 -04:00
|
|
|
Box::new(SqliteDbHandler::<P>::new(
|
|
|
|
default_storage_dir,
|
|
|
|
versionstamp_rng_seed,
|
|
|
|
)),
|
2023-08-22 01:56:00 -04:00
|
|
|
),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait(?Send)]
|
|
|
|
impl DatabaseHandler for MultiBackendDbHandler {
|
2023-12-05 08:21:46 -05:00
|
|
|
type DB = RcDynamicDb;
|
2023-08-22 01:56:00 -04:00
|
|
|
|
|
|
|
async fn open(
|
|
|
|
&self,
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
path: Option<String>,
|
|
|
|
) -> Result<Self::DB, AnyError> {
|
|
|
|
for (prefixes, handler) in &self.backends {
|
|
|
|
for &prefix in *prefixes {
|
|
|
|
if prefix.is_empty() {
|
|
|
|
return handler.dyn_open(state.clone(), path.clone()).await;
|
|
|
|
}
|
|
|
|
let Some(path) = &path else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
if path.starts_with(prefix) {
|
|
|
|
return handler.dyn_open(state.clone(), Some(path.clone())).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(type_error(format!(
|
|
|
|
"No backend supports the given path: {:?}",
|
|
|
|
path
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait(?Send)]
|
|
|
|
pub trait DynamicDbHandler {
|
|
|
|
async fn dyn_open(
|
|
|
|
&self,
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
path: Option<String>,
|
2023-12-05 08:21:46 -05:00
|
|
|
) -> Result<RcDynamicDb, AnyError>;
|
2023-08-22 01:56:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait(?Send)]
|
|
|
|
impl DatabaseHandler for Box<dyn DynamicDbHandler> {
|
2023-12-05 08:21:46 -05:00
|
|
|
type DB = RcDynamicDb;
|
2023-08-22 01:56:00 -04:00
|
|
|
|
|
|
|
async fn open(
|
|
|
|
&self,
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
path: Option<String>,
|
|
|
|
) -> Result<Self::DB, AnyError> {
|
|
|
|
(**self).dyn_open(state, path).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait(?Send)]
|
|
|
|
impl<T, DB> DynamicDbHandler for T
|
|
|
|
where
|
|
|
|
T: DatabaseHandler<DB = DB>,
|
|
|
|
DB: Database + 'static,
|
|
|
|
{
|
|
|
|
async fn dyn_open(
|
|
|
|
&self,
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
path: Option<String>,
|
2023-12-05 08:21:46 -05:00
|
|
|
) -> Result<RcDynamicDb, AnyError> {
|
|
|
|
Ok(RcDynamicDb(Rc::new(self.open(state, path).await?)))
|
2023-08-22 01:56:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait(?Send)]
|
|
|
|
pub trait DynamicDb {
|
|
|
|
async fn dyn_snapshot_read(
|
|
|
|
&self,
|
|
|
|
requests: Vec<ReadRange>,
|
|
|
|
options: SnapshotReadOptions,
|
|
|
|
) -> Result<Vec<ReadRangeOutput>, AnyError>;
|
|
|
|
|
|
|
|
async fn dyn_atomic_write(
|
|
|
|
&self,
|
|
|
|
write: AtomicWrite,
|
|
|
|
) -> Result<Option<CommitResult>, AnyError>;
|
|
|
|
|
|
|
|
async fn dyn_dequeue_next_message(
|
|
|
|
&self,
|
2023-09-26 23:06:57 -04:00
|
|
|
) -> Result<Option<Box<dyn QueueMessageHandle>>, AnyError>;
|
2023-08-22 01:56:00 -04:00
|
|
|
|
2023-12-05 08:21:46 -05:00
|
|
|
fn dyn_watch(&self, keys: Vec<Vec<u8>>) -> WatchStream;
|
|
|
|
|
2023-08-22 01:56:00 -04:00
|
|
|
fn dyn_close(&self);
|
|
|
|
}
|
|
|
|
|
2023-12-05 08:21:46 -05:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct RcDynamicDb(Rc<dyn DynamicDb>);
|
|
|
|
|
2023-08-22 01:56:00 -04:00
|
|
|
#[async_trait(?Send)]
|
2023-12-05 08:21:46 -05:00
|
|
|
impl Database for RcDynamicDb {
|
2023-08-22 01:56:00 -04:00
|
|
|
type QMH = Box<dyn QueueMessageHandle>;
|
|
|
|
|
|
|
|
async fn snapshot_read(
|
|
|
|
&self,
|
|
|
|
requests: Vec<ReadRange>,
|
|
|
|
options: SnapshotReadOptions,
|
|
|
|
) -> Result<Vec<ReadRangeOutput>, AnyError> {
|
2023-12-05 08:21:46 -05:00
|
|
|
(*self.0).dyn_snapshot_read(requests, options).await
|
2023-08-22 01:56:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn atomic_write(
|
|
|
|
&self,
|
|
|
|
write: AtomicWrite,
|
|
|
|
) -> Result<Option<CommitResult>, AnyError> {
|
2023-12-05 08:21:46 -05:00
|
|
|
(*self.0).dyn_atomic_write(write).await
|
2023-08-22 01:56:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn dequeue_next_message(
|
|
|
|
&self,
|
2023-09-26 23:06:57 -04:00
|
|
|
) -> Result<Option<Box<dyn QueueMessageHandle>>, AnyError> {
|
2023-12-05 08:21:46 -05:00
|
|
|
(*self.0).dyn_dequeue_next_message().await
|
|
|
|
}
|
|
|
|
|
|
|
|
fn watch(&self, keys: Vec<Vec<u8>>) -> WatchStream {
|
|
|
|
(*self.0).dyn_watch(keys)
|
2023-08-22 01:56:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn close(&self) {
|
2023-12-05 08:21:46 -05:00
|
|
|
(*self.0).dyn_close()
|
2023-08-22 01:56:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait(?Send)]
|
|
|
|
impl<T, QMH> DynamicDb for T
|
|
|
|
where
|
|
|
|
T: Database<QMH = QMH>,
|
|
|
|
QMH: QueueMessageHandle + 'static,
|
|
|
|
{
|
|
|
|
async fn dyn_snapshot_read(
|
|
|
|
&self,
|
|
|
|
requests: Vec<ReadRange>,
|
|
|
|
options: SnapshotReadOptions,
|
|
|
|
) -> Result<Vec<ReadRangeOutput>, AnyError> {
|
2023-10-31 07:13:57 -04:00
|
|
|
Ok(self.snapshot_read(requests, options).await?)
|
2023-08-22 01:56:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn dyn_atomic_write(
|
|
|
|
&self,
|
|
|
|
write: AtomicWrite,
|
|
|
|
) -> Result<Option<CommitResult>, AnyError> {
|
2023-10-31 07:13:57 -04:00
|
|
|
Ok(self.atomic_write(write).await?)
|
2023-08-22 01:56:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn dyn_dequeue_next_message(
|
|
|
|
&self,
|
2023-09-26 23:06:57 -04:00
|
|
|
) -> Result<Option<Box<dyn QueueMessageHandle>>, AnyError> {
|
|
|
|
Ok(
|
|
|
|
self
|
2023-10-31 07:13:57 -04:00
|
|
|
.dequeue_next_message()
|
2023-09-26 23:06:57 -04:00
|
|
|
.await?
|
|
|
|
.map(|x| Box::new(x) as Box<dyn QueueMessageHandle>),
|
|
|
|
)
|
2023-08-22 01:56:00 -04:00
|
|
|
}
|
|
|
|
|
2023-12-05 08:21:46 -05:00
|
|
|
fn dyn_watch(&self, keys: Vec<Vec<u8>>) -> WatchStream {
|
|
|
|
self.watch(keys)
|
|
|
|
}
|
|
|
|
|
2023-08-22 01:56:00 -04:00
|
|
|
fn dyn_close(&self) {
|
|
|
|
self.close()
|
|
|
|
}
|
|
|
|
}
|