From c25b85669ba0c3a54abd4d3fa09be0bffaaf5255 Mon Sep 17 00:00:00 2001 From: Raphael Darley Date: Wed, 26 Jul 2023 12:54:48 +0100 Subject: [PATCH] feature: Add config for query/transaction timeout and notifications (#2340) --- lib/src/api/engine/any/mod.rs | 156 +++++++++++++++++++++++++-- lib/src/api/engine/local/native.rs | 10 +- lib/src/api/engine/local/wasm.rs | 10 +- lib/src/api/method/tests/protocol.rs | 2 +- lib/src/api/opt/config.rs | 79 ++++++++++++++ lib/src/api/opt/endpoint/fdb.rs | 35 +++++- lib/src/api/opt/endpoint/http.rs | 12 +-- lib/src/api/opt/endpoint/indxdb.rs | 32 +++++- lib/src/api/opt/endpoint/mem.rs | 28 ++++- lib/src/api/opt/endpoint/mod.rs | 4 +- lib/src/api/opt/endpoint/rocksdb.rs | 69 ++++++++++-- lib/src/api/opt/endpoint/speedb.rs | 35 +++++- lib/src/api/opt/endpoint/tikv.rs | 38 ++++++- lib/src/api/opt/endpoint/ws.rs | 12 +-- lib/src/api/opt/mod.rs | 2 + 15 files changed, 478 insertions(+), 46 deletions(-) create mode 100644 lib/src/api/opt/config.rs diff --git a/lib/src/api/engine/any/mod.rs b/lib/src/api/engine/any/mod.rs index d23702bf..3bf7aa42 100644 --- a/lib/src/api/engine/any/mod.rs +++ b/lib/src/api/engine/any/mod.rs @@ -98,6 +98,15 @@ use crate::api::err::Error; feature = "kv-indxdb", ))] use crate::api::opt::auth::Root; +#[cfg(any( + feature = "kv-mem", + feature = "kv-tikv", + feature = "kv-rocksdb", + feature = "kv-speedb", + feature = "kv-fdb", + feature = "kv-indxdb", +))] +use crate::api::opt::Config; use crate::api::opt::Endpoint; #[cfg(any( feature = "kv-mem", @@ -131,7 +140,7 @@ impl IntoEndpoint for &str { }; Ok(Endpoint { endpoint: Url::parse(url).map_err(|_| Error::InvalidUrl(self.to_owned()))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -205,8 +214,40 @@ where T: Into, { fn into_endpoint(self) -> Result { - let mut endpoint = IntoEndpoint::into_endpoint(self.0.into())?; - endpoint.strict = true; + let (address, _) = self; + let mut endpoint = IntoEndpoint::into_endpoint(address.into())?; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +#[cfg(any( + feature = "kv-mem", + feature = "kv-tikv", + feature = "kv-rocksdb", + feature = "kv-speedb", + feature = "kv-fdb", + feature = "kv-indxdb", +))] +#[cfg_attr( + docsrs, + doc(cfg(any( + feature = "kv-mem", + feature = "kv-tikv", + feature = "kv-rocksdb", + feature = "kv-speedb", + feature = "kv-fdb", + feature = "kv-indxdb", + ))) +)] +impl IntoEndpoint for (T, Config) +where + T: Into, +{ + fn into_endpoint(self) -> Result { + let (address, config) = self; + let mut endpoint = IntoEndpoint::into_endpoint(address.into())?; + endpoint.config = config; Ok(endpoint) } } @@ -266,7 +307,36 @@ where fn into_endpoint(self) -> Result { let (address, _, root) = self; let mut endpoint = IntoEndpoint::into_endpoint((address, root))?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +#[cfg(any( + feature = "kv-mem", + feature = "kv-tikv", + feature = "kv-rocksdb", + feature = "kv-fdb", + feature = "kv-indxdb", +))] +#[cfg_attr( + docsrs, + doc(cfg(any( + feature = "kv-mem", + feature = "kv-tikv", + feature = "kv-rocksdb", + feature = "kv-fdb", + feature = "kv-indxdb", + ))) +)] +impl IntoEndpoint for (T, Config, Root<'_>) +where + T: Into, +{ + fn into_endpoint(self) -> Result { + let (address, config, root) = self; + let mut endpoint = IntoEndpoint::into_endpoint((address, root))?; + endpoint.config = config; Ok(endpoint) } } @@ -302,7 +372,43 @@ where let (address, _, config) = self; let mut endpoint = address.into().into_endpoint()?; endpoint.tls_config = Some(Tls::Native(config)); - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +#[cfg(all( + any( + feature = "kv-mem", + feature = "kv-tikv", + feature = "kv-rocksdb", + feature = "kv-fdb", + feature = "kv-indxdb", + ), + feature = "native-tls", +))] +#[cfg_attr( + docsrs, + doc(cfg(all( + any( + feature = "kv-mem", + feature = "kv-tikv", + feature = "kv-rocksdb", + feature = "kv-fdb", + feature = "kv-indxdb", + ), + feature = "native-tls", + ))) +)] +impl IntoEndpoint for (T, Config, native_tls::TlsConnector) +where + T: Into, +{ + fn into_endpoint(self) -> Result { + let (address, opt_config, config) = self; + let mut endpoint = address.into().into_endpoint()?; + endpoint.tls_config = Some(Tls::Native(config)); + endpoint.config = opt_config; Ok(endpoint) } } @@ -375,7 +481,45 @@ where let (address, _, config) = self; let mut endpoint = address.into().into_endpoint()?; endpoint.tls_config = Some(Tls::Rust(config)); - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +#[cfg(all( + any( + feature = "kv-mem", + feature = "kv-tikv", + feature = "kv-rocksdb", + feature = "kv-speedb", + feature = "kv-fdb", + feature = "kv-indxdb", + ), + feature = "rustls", +))] +#[cfg_attr( + docsrs, + doc(cfg(all( + any( + feature = "kv-mem", + feature = "kv-tikv", + feature = "kv-rocksdb", + feature = "kv-speedb", + feature = "kv-fdb", + feature = "kv-indxdb", + ), + feature = "rustls", + ))) +)] +impl IntoEndpoint for (T, Config, rustls::ClientConfig) +where + T: Into, +{ + fn into_endpoint(self) -> Result { + let (address, opt_config, config) = self; + let mut endpoint = address.into().into_endpoint()?; + endpoint.tls_config = Some(Tls::Rust(config)); + endpoint.config = opt_config; Ok(endpoint) } } diff --git a/lib/src/api/engine/local/native.rs b/lib/src/api/engine/local/native.rs index 1ef8b518..d96adba3 100644 --- a/lib/src/api/engine/local/native.rs +++ b/lib/src/api/engine/local/native.rs @@ -116,7 +116,15 @@ pub(crate) fn router( } }; - let kvs = kvs.with_strict_mode(address.strict); + let kvs = kvs + .with_strict_mode(address.config.strict) + .with_query_timeout(address.config.query_timeout) + .with_transaction_timeout(address.config.transaction_timeout); + + let kvs = match address.config.notifications { + true => kvs.with_notifications(), + false => kvs, + }; let mut vars = BTreeMap::new(); let mut stream = route_rx.into_stream(); diff --git a/lib/src/api/engine/local/wasm.rs b/lib/src/api/engine/local/wasm.rs index b9e14ef1..e9434104 100644 --- a/lib/src/api/engine/local/wasm.rs +++ b/lib/src/api/engine/local/wasm.rs @@ -102,7 +102,15 @@ pub(crate) fn router( } }; - let kvs = kvs.with_strict_mode(address.strict); + let kvs = kvs + .with_strict_mode(address.config.strict) + .with_query_timeout(address.config.query_timeout) + .with_transaction_timeout(address.config.transaction_timeout); + + let kvs = match address.config.notifications { + true => kvs.with_notifications(), + false => kvs, + }; let mut vars = BTreeMap::new(); let mut stream = route_rx.into_stream(); diff --git a/lib/src/api/method/tests/protocol.rs b/lib/src/api/method/tests/protocol.rs index ca4dea7f..2f32991c 100644 --- a/lib/src/api/method/tests/protocol.rs +++ b/lib/src/api/method/tests/protocol.rs @@ -31,7 +31,7 @@ impl IntoEndpoint for () { fn into_endpoint(self) -> Result { Ok(Endpoint { endpoint: Url::parse("test://")?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, diff --git a/lib/src/api/opt/config.rs b/lib/src/api/opt/config.rs new file mode 100644 index 00000000..a976594b --- /dev/null +++ b/lib/src/api/opt/config.rs @@ -0,0 +1,79 @@ +use std::time::Duration; + +/// Configuration for server connection, including: strictness, notifications, query_timeout, transaction_timeout +#[cfg(any( + feature = "kv-mem", + feature = "kv-tikv", + feature = "kv-rocksdb", + feature = "kv-speedb", + feature = "kv-fdb", + feature = "kv-indxdb", +))] +#[derive(Debug, Default)] +pub struct Config { + pub(crate) strict: bool, + pub(crate) notifications: bool, + pub(crate) query_timeout: Option, + pub(crate) transaction_timeout: Option, +} +#[cfg(not(any( + feature = "kv-mem", + feature = "kv-tikv", + feature = "kv-rocksdb", + feature = "kv-speedb", + feature = "kv-fdb", + feature = "kv-indxdb", +)))] +#[derive(Debug, Default)] +pub struct Config(); + +#[cfg(any( + feature = "kv-mem", + feature = "kv-tikv", + feature = "kv-rocksdb", + feature = "kv-speedb", + feature = "kv-fdb", + feature = "kv-indxdb", +))] +impl Config { + ///Create a default config that can be modified to configure a connection + pub fn new() -> Self { + Default::default() + } + + ///Set the strict value of the config to the supplied value + pub fn set_strict(mut self, strict: bool) -> Self { + self.strict = strict; + self + } + + ///Set the config to use strict mode + pub fn strict(mut self) -> Self { + self.strict = true; + self + } + + ///Set the notifications value of the config to the supplied value + pub fn set_notifications(mut self, notifications: bool) -> Self { + self.notifications = notifications; + self + } + + ///Set the config to use notifications + pub fn notifications(mut self) -> Self { + self.notifications = true; + self + } + + ///Set the query timeout of the config + pub fn query_timeout(mut self, timeout: impl Into>) -> Self { + self.query_timeout = timeout.into(); + self + } + + ///Set the transaction timeout of the config + pub fn transaction_timeout(mut self, timeout: impl Into>) -> Self { + self.transaction_timeout = timeout.into(); + self + } +} diff --git a/lib/src/api/opt/endpoint/fdb.rs b/lib/src/api/opt/endpoint/fdb.rs index 4562e88a..39a0960e 100644 --- a/lib/src/api/opt/endpoint/fdb.rs +++ b/lib/src/api/opt/endpoint/fdb.rs @@ -2,6 +2,7 @@ use crate::api::engine::local::Db; use crate::api::engine::local::FDb; use crate::api::err::Error; use crate::api::opt::auth::Root; +use crate::api::opt::Config; use crate::api::opt::Endpoint; use crate::api::opt::IntoEndpoint; use crate::api::opt::Strict; @@ -17,7 +18,7 @@ impl IntoEndpoint for &str { let url = format!("fdb://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -45,7 +46,21 @@ where fn into_endpoint(self) -> Result { let (path, _) = self; let mut endpoint = IntoEndpoint::::into_endpoint(path.as_ref())?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for (T, Config) +where + T: AsRef, +{ + type Client = Db; + + fn into_endpoint(self) -> Result { + let (path, config) = self; + let mut endpoint = IntoEndpoint::::into_endpoint(path.as_ref())?; + endpoint.config = config; Ok(endpoint) } } @@ -75,7 +90,21 @@ where fn into_endpoint(self) -> Result { let (path, _, root) = self; let mut endpoint = IntoEndpoint::::into_endpoint((path, root))?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for (T, Config, Root<'_>) +where + T: AsRef, +{ + type Client = Db; + + fn into_endpoint(self) -> Result { + let (path, config, root) = self; + let mut endpoint = IntoEndpoint::::into_endpoint((path, root))?; + endpoint.config = config; Ok(endpoint) } } diff --git a/lib/src/api/opt/endpoint/http.rs b/lib/src/api/opt/endpoint/http.rs index 936ac0a5..f9301235 100644 --- a/lib/src/api/opt/endpoint/http.rs +++ b/lib/src/api/opt/endpoint/http.rs @@ -18,7 +18,7 @@ impl IntoEndpoint for &str { let url = format!("http://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -35,7 +35,7 @@ impl IntoEndpoint for SocketAddr { let url = format!("http://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -52,7 +52,7 @@ impl IntoEndpoint for String { let url = format!("http://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -69,7 +69,7 @@ impl IntoEndpoint for &str { let url = format!("https://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -86,7 +86,7 @@ impl IntoEndpoint for SocketAddr { let url = format!("https://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -103,7 +103,7 @@ impl IntoEndpoint for String { let url = format!("https://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, diff --git a/lib/src/api/opt/endpoint/indxdb.rs b/lib/src/api/opt/endpoint/indxdb.rs index 901ba319..cd41f45e 100644 --- a/lib/src/api/opt/endpoint/indxdb.rs +++ b/lib/src/api/opt/endpoint/indxdb.rs @@ -2,6 +2,7 @@ use crate::api::engine::local::Db; use crate::api::engine::local::IndxDb; use crate::api::err::Error; use crate::api::opt::auth::Root; +use crate::api::opt::Config; use crate::api::opt::Endpoint; use crate::api::opt::IntoEndpoint; use crate::api::opt::Strict; @@ -16,7 +17,7 @@ impl IntoEndpoint for &str { let url = format!("indxdb://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -30,8 +31,20 @@ impl IntoEndpoint for (&str, Strict) { type Client = Db; fn into_endpoint(self) -> Result { - let mut endpoint = IntoEndpoint::::into_endpoint(self.0)?; - endpoint.strict = true; + let (address, _) = self; + let mut endpoint = IntoEndpoint::::into_endpoint(address)?; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for (&str, Config) { + type Client = Db; + + fn into_endpoint(self) -> Result { + let (address, config) = self; + let mut endpoint = IntoEndpoint::::into_endpoint(address)?; + endpoint.config = config; Ok(endpoint) } } @@ -55,7 +68,18 @@ impl IntoEndpoint for (&str, Strict, Root<'_>) { fn into_endpoint(self) -> Result { let (name, _, root) = self; let mut endpoint = IntoEndpoint::::into_endpoint((name, root))?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for (&str, Config, Root<'_>) { + type Client = Db; + + fn into_endpoint(self) -> Result { + let (name, config, root) = self; + let mut endpoint = IntoEndpoint::::into_endpoint((name, root))?; + endpoint.config = config; Ok(endpoint) } } diff --git a/lib/src/api/opt/endpoint/mem.rs b/lib/src/api/opt/endpoint/mem.rs index afff170d..e6081d97 100644 --- a/lib/src/api/opt/endpoint/mem.rs +++ b/lib/src/api/opt/endpoint/mem.rs @@ -6,6 +6,7 @@ use crate::api::opt::IntoEndpoint; use crate::api::opt::Strict; use crate::api::Result; use crate::dbs::Level; +use crate::opt::Config; use url::Url; impl IntoEndpoint for () { @@ -14,7 +15,7 @@ impl IntoEndpoint for () { fn into_endpoint(self) -> Result { Ok(Endpoint { endpoint: Url::parse("mem://").unwrap(), - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -29,7 +30,17 @@ impl IntoEndpoint for Strict { fn into_endpoint(self) -> Result { let mut endpoint = IntoEndpoint::::into_endpoint(())?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for Config { + type Client = Db; + + fn into_endpoint(self) -> Result { + let mut endpoint = IntoEndpoint::::into_endpoint(())?; + endpoint.config = self; Ok(endpoint) } } @@ -52,7 +63,18 @@ impl IntoEndpoint for (Strict, Root<'_>) { fn into_endpoint(self) -> Result { let (_, root) = self; let mut endpoint = IntoEndpoint::::into_endpoint(root)?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for (Config, Root<'_>) { + type Client = Db; + + fn into_endpoint(self) -> Result { + let (config, root) = self; + let mut endpoint = IntoEndpoint::::into_endpoint(root)?; + endpoint.config = config; Ok(endpoint) } } diff --git a/lib/src/api/opt/endpoint/mod.rs b/lib/src/api/opt/endpoint/mod.rs index a7ba53ab..b7e20b22 100644 --- a/lib/src/api/opt/endpoint/mod.rs +++ b/lib/src/api/opt/endpoint/mod.rs @@ -21,13 +21,15 @@ use crate::api::Result; use crate::dbs::Level; use url::Url; +use super::Config; + /// A server address used to connect to the server #[derive(Debug)] #[allow(dead_code)] // used by the embedded and remote connections pub struct Endpoint { pub(crate) endpoint: Url, #[allow(dead_code)] // used by the embedded database - pub(crate) strict: bool, + pub(crate) config: Config, #[cfg(any(feature = "native-tls", feature = "rustls"))] pub(crate) tls_config: Option, // Only used by the local engines diff --git a/lib/src/api/opt/endpoint/rocksdb.rs b/lib/src/api/opt/endpoint/rocksdb.rs index 588392e2..23ed0c17 100644 --- a/lib/src/api/opt/endpoint/rocksdb.rs +++ b/lib/src/api/opt/endpoint/rocksdb.rs @@ -3,6 +3,7 @@ use crate::api::engine::local::File; use crate::api::engine::local::RocksDb; use crate::api::err::Error; use crate::api::opt::auth::Root; +use crate::api::opt::Config; use crate::api::opt::Endpoint; use crate::api::opt::IntoEndpoint; use crate::api::opt::Strict; @@ -18,7 +19,7 @@ impl IntoEndpoint for &str { let url = format!("rocksdb://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -46,7 +47,21 @@ where fn into_endpoint(self) -> Result { let (path, _) = self; let mut endpoint = IntoEndpoint::::into_endpoint(path.as_ref())?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for (T, Config) +where + T: AsRef, +{ + type Client = Db; + + fn into_endpoint(self) -> Result { + let (path, config) = self; + let mut endpoint = IntoEndpoint::::into_endpoint(path.as_ref())?; + endpoint.config = config; Ok(endpoint) } } @@ -76,7 +91,21 @@ where fn into_endpoint(self) -> Result { let (path, _, root) = self; let mut endpoint = IntoEndpoint::::into_endpoint((path.as_ref(), root))?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for (T, Config, Root<'_>) +where + T: AsRef, +{ + type Client = Db; + + fn into_endpoint(self) -> Result { + let (path, config, root) = self; + let mut endpoint = IntoEndpoint::::into_endpoint((path.as_ref(), root))?; + endpoint.config = config; Ok(endpoint) } } @@ -88,7 +117,7 @@ impl IntoEndpoint for &str { let url = format!("file://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -116,7 +145,21 @@ where fn into_endpoint(self) -> Result { let (path, _) = self; let mut endpoint = IntoEndpoint::::into_endpoint(path.as_ref())?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for (T, Config) +where + T: AsRef, +{ + type Client = Db; + + fn into_endpoint(self) -> Result { + let (path, config) = self; + let mut endpoint = IntoEndpoint::::into_endpoint(path.as_ref())?; + endpoint.config = config; Ok(endpoint) } } @@ -146,7 +189,21 @@ where fn into_endpoint(self) -> Result { let (path, _, root) = self; let mut endpoint = IntoEndpoint::::into_endpoint((path.as_ref(), root))?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for (T, Config, Root<'_>) +where + T: AsRef, +{ + type Client = Db; + + fn into_endpoint(self) -> Result { + let (path, config, root) = self; + let mut endpoint = IntoEndpoint::::into_endpoint((path.as_ref(), root))?; + endpoint.config = config; Ok(endpoint) } } diff --git a/lib/src/api/opt/endpoint/speedb.rs b/lib/src/api/opt/endpoint/speedb.rs index 810d3c01..a1bb3fd7 100644 --- a/lib/src/api/opt/endpoint/speedb.rs +++ b/lib/src/api/opt/endpoint/speedb.rs @@ -1,6 +1,7 @@ use crate::api::engine::local::Db; use crate::api::engine::local::SpeeDb; use crate::api::err::Error; +use crate::api::opt::Config; use crate::api::opt::Endpoint; use crate::api::opt::IntoEndpoint; use crate::api::opt::Strict; @@ -17,7 +18,7 @@ impl IntoEndpoint for &str { let url = format!("speedb://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -45,7 +46,21 @@ where fn into_endpoint(self) -> Result { let (path, _) = self; let mut endpoint = IntoEndpoint::::into_endpoint(path.as_ref())?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for (T, Config) +where + T: AsRef, +{ + type Client = Db; + + fn into_endpoint(self) -> Result { + let (path, config) = self; + let mut endpoint = IntoEndpoint::::into_endpoint(path.as_ref())?; + endpoint.config = config; Ok(endpoint) } } @@ -75,7 +90,21 @@ where fn into_endpoint(self) -> Result { let (path, _, root) = self; let mut endpoint = IntoEndpoint::::into_endpoint((path.as_ref(), root))?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for (T, Config, Root<'_>) +where + T: AsRef, +{ + type Client = Db; + + fn into_endpoint(self) -> Result { + let (path, config, root) = self; + let mut endpoint = IntoEndpoint::::into_endpoint((path.as_ref(), root))?; + endpoint.config = config; Ok(endpoint) } } diff --git a/lib/src/api/opt/endpoint/tikv.rs b/lib/src/api/opt/endpoint/tikv.rs index 79eb6558..3a31e935 100644 --- a/lib/src/api/opt/endpoint/tikv.rs +++ b/lib/src/api/opt/endpoint/tikv.rs @@ -2,6 +2,7 @@ use crate::api::engine::local::Db; use crate::api::engine::local::TiKv; use crate::api::err::Error; use crate::api::opt::auth::Root; +use crate::api::opt::Config; use crate::api::opt::Endpoint; use crate::api::opt::IntoEndpoint; use crate::api::opt::Strict; @@ -18,7 +19,7 @@ impl IntoEndpoint for &str { let url = format!("tikv://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -35,7 +36,7 @@ impl IntoEndpoint for SocketAddr { let url = format!("tikv://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -52,7 +53,7 @@ impl IntoEndpoint for String { let url = format!("tikv://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -71,7 +72,20 @@ where fn into_endpoint(self) -> Result { let (address, _) = self; let mut endpoint = address.into_endpoint()?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} +impl IntoEndpoint for (T, Config) +where + T: IntoEndpoint + Display, +{ + type Client = Db; + + fn into_endpoint(self) -> Result { + let (address, config) = self; + let mut endpoint = address.into_endpoint()?; + endpoint.config = config; Ok(endpoint) } } @@ -101,7 +115,21 @@ where fn into_endpoint(self) -> Result { let (address, _, root) = self; let mut endpoint = (address, root).into_endpoint()?; - endpoint.strict = true; + endpoint.config.strict = true; + Ok(endpoint) + } +} + +impl IntoEndpoint for (T, Config, Root<'_>) +where + T: IntoEndpoint + Display, +{ + type Client = Db; + + fn into_endpoint(self) -> Result { + let (address, config, root) = self; + let mut endpoint = (address, root).into_endpoint()?; + endpoint.config = config; Ok(endpoint) } } diff --git a/lib/src/api/opt/endpoint/ws.rs b/lib/src/api/opt/endpoint/ws.rs index 3d19e576..4a34e898 100644 --- a/lib/src/api/opt/endpoint/ws.rs +++ b/lib/src/api/opt/endpoint/ws.rs @@ -18,7 +18,7 @@ impl IntoEndpoint for &str { let url = format!("ws://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -35,7 +35,7 @@ impl IntoEndpoint for SocketAddr { let url = format!("ws://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -52,7 +52,7 @@ impl IntoEndpoint for String { let url = format!("ws://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -69,7 +69,7 @@ impl IntoEndpoint for &str { let url = format!("wss://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -86,7 +86,7 @@ impl IntoEndpoint for SocketAddr { let url = format!("wss://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, @@ -103,7 +103,7 @@ impl IntoEndpoint for String { let url = format!("wss://{self}"); Ok(Endpoint { endpoint: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?, - strict: false, + config: Default::default(), #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, auth: Level::No, diff --git a/lib/src/api/opt/mod.rs b/lib/src/api/opt/mod.rs index fa77dfd5..06a2c4d2 100644 --- a/lib/src/api/opt/mod.rs +++ b/lib/src/api/opt/mod.rs @@ -2,6 +2,7 @@ pub mod auth; +mod config; mod endpoint; mod query; mod resource; @@ -20,6 +21,7 @@ use serde_json::json; use serde_json::Map; use serde_json::Value as JsonValue; +pub use config::*; pub use endpoint::*; pub use query::*; pub use resource::*;