pub trait BaseClient<T>{
Show 14 methods
// Required methods
fn new(config: ClientConfig) -> Self;
fn get(&mut self, key: &str) -> Result<Option<T>>;
fn set(&mut self, key: &str, value: T) -> Result<()>;
fn update(
&mut self,
key: &str,
value: T,
upsert: Option<bool>,
) -> Result<()>;
fn delete(&mut self, key: &str) -> Result<()>;
fn exists(&mut self, key: &str) -> Result<bool>;
fn keys(&mut self) -> Result<Option<Vec<String>>>;
fn values(&mut self) -> Result<Option<Vec<T>>>;
fn len(&mut self) -> Result<usize>;
fn purge(&mut self) -> Result<()>;
fn get_many(&mut self, keys: &[&str]) -> Result<Option<Vec<T>>>;
fn set_many(&mut self, keys: &[&str], values: &[T]) -> Result<()>;
fn delete_many(&mut self, keys: &[&str]) -> Result<()>;
fn update_many(
&mut self,
keys: &[&str],
values: &[T],
upsert: Option<bool>,
) -> Result<()>;
}Required Methods§
Sourcefn new(config: ClientConfig) -> Self
fn new(config: ClientConfig) -> Self
Creates a new instance of the client.
config is the configuration for the database. If None, then the default configuration will be used.
The client needs to know what type of data it will be storing, so it can properly serialize and deserialize it.
You need to specify the type of data when creating a new client using the client::<T>::new() method.
This type must implement the following traits:
SerializeDeserializeOwnedDebugEqPartialEqHashSendSyncClone
§Examples
With default configuration:
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));Sourcefn get(&mut self, key: &str) -> Result<Option<T>>
fn get(&mut self, key: &str) -> Result<Option<T>>
Get the value associated with a key.
Returns None if the key does not exist. This could be caused by
the key never being assigned a value, or the key expiring.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
}
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));
client.set("user_1", Schema { id: 10 }).unwrap();
let user = client.get("user_1").unwrap();
// do something with the userDo something with the result. After Consuming the result, you
must handle the Option<T> that is returned.
Sourcefn set(&mut self, key: &str, value: T) -> Result<()>
fn set(&mut self, key: &str, value: T) -> Result<()>
Set the value associated with a key.
If the key already exists, the database will attempt to overwrite the value.
key to set the value for.
value to set for the key.
ttl the time-to-live for the key. If None, then the the key will not expire,
unless default_ttl is set in the configuration. If default_ttl is set, then
the key will expire after the default ttl. If ttl is set here, it will override
the default ttl set in the configuration.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));
client.set("user_1", Schema { id: 10 }).unwrap();Sourcefn update(&mut self, key: &str, value: T, upsert: Option<bool>) -> Result<()>
fn update(&mut self, key: &str, value: T, upsert: Option<bool>) -> Result<()>
Update the value associated with a key.
By default update will fail if the key does not exist. If you want to upsert the value, then
you can set upsert to true using true.into() or Some(true).
key to update the value for.
value to update for the key.
upsert if the value should be upserted if the key does not exist.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));
client.update("user_1", Schema { id: 10 }, None).unwrap(); // fails
client
.update("user_1", Schema { id: 20 }, true.into())
.unwrap(); // succeedsSourcefn delete(&mut self, key: &str) -> Result<()>
fn delete(&mut self, key: &str) -> Result<()>
Delete the value associated with a key.
key to delete the value for.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));
client.delete("user_1").unwrap();Sourcefn exists(&mut self, key: &str) -> Result<bool>
fn exists(&mut self, key: &str) -> Result<bool>
Check if a key exists in the database.
key to check if it exists.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));
if client.exists("user_1").unwrap() {
// do something
}Sourcefn keys(&mut self) -> Result<Option<Vec<String>>>
fn keys(&mut self) -> Result<Option<Vec<String>>>
Get all keys in the database.
Returns None if there are no keys in the database or a Vec<String> keys.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));
let all_keys = client.keys().unwrap();Sourcefn values(&mut self) -> Result<Option<Vec<T>>>
fn values(&mut self) -> Result<Option<Vec<T>>>
Get all values in the database.
Returns None if there are no values in the database or a Vec<T> values.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));
let all_values = client.values().unwrap();Sourcefn len(&mut self) -> Result<usize>
fn len(&mut self) -> Result<usize>
Get the number of keys in the database.
Returns 0 if there are no keys in the database or the number of keys in the database.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));
let num_keys = client.len().unwrap();Sourcefn purge(&mut self) -> Result<()>
fn purge(&mut self) -> Result<()>
Clears all keys and values from the database.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));
client.purge().unwrap();Sourcefn get_many(&mut self, keys: &[&str]) -> Result<Option<Vec<T>>>
fn get_many(&mut self, keys: &[&str]) -> Result<Option<Vec<T>>>
Get multiple values associated with multiple keys.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));
let values = client.get_many(&["user_1", "user_2"]).unwrap();Sourcefn set_many(&mut self, keys: &[&str], values: &[T]) -> Result<()>
fn set_many(&mut self, keys: &[&str], values: &[T]) -> Result<()>
Set multiple values associated with multiple keys.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));
client
.set_many(
&["user_1", "user_2"],
&[Schema { id: 10 }, Schema { id: 20 }],
)
.unwrap();Sourcefn delete_many(&mut self, keys: &[&str]) -> Result<()>
fn delete_many(&mut self, keys: &[&str]) -> Result<()>
Delete multiple values associated with multiple keys.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema
{
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new(
"db.qkv".to_string(),
true.into(),
LevelFilter::Debug.into(),
));
client.delete_many(&["user_1", "user_2"]).unwrap();Sourcefn update_many(
&mut self,
keys: &[&str],
values: &[T],
upsert: Option<bool>,
) -> Result<()>
fn update_many( &mut self, keys: &[&str], values: &[T], upsert: Option<bool>, ) -> Result<()>
Update multiple values associated with multiple keys.
§Examples
use quick_kv::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Schema {
id: u64,
};
let mut client = QuickClient::<Schema>::new(ClientConfig::new("db.qkv".to_string(), true.into(), LevelFilter::Debug.into()));
client.update_many(&["user_1", "user_2"], &[Schema { id: 10 }, Schema { id: 20 }], true.into()).unwrap();Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.