Expand index trait to also include key type
Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
parent
276cf3c8d8
commit
1343e61e0f
@ -77,11 +77,10 @@ fn main() {
|
||||
|
||||
// Load all active users using the IsActive field index
|
||||
// TODO: expand Index type so it defines the type of the key
|
||||
let key = true.to_string();
|
||||
let active_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<IsActive>(&key)
|
||||
.get::<IsActive>(&true)
|
||||
.expect("can load stored users");
|
||||
// We should have 2 active users
|
||||
assert_eq!(active_users.len(), 2);
|
||||
@ -96,15 +95,14 @@ fn main() {
|
||||
let active_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<IsActive>(&key)
|
||||
.get::<IsActive>(&true)
|
||||
.expect("can load stored users");
|
||||
assert_eq!(active_users.len(), 1);
|
||||
// And verify we still have 2 inactive users
|
||||
let key = false.to_string();
|
||||
let inactive_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<IsActive>(&key)
|
||||
.get::<IsActive>(&false)
|
||||
.expect("can load stored users");
|
||||
assert_eq!(inactive_users.len(), 2);
|
||||
|
||||
|
@ -22,7 +22,7 @@ where
|
||||
type Error: std::fmt::Debug;
|
||||
|
||||
/// Get all items where the given index field is equal to key.
|
||||
fn get<I>(&self, key: K) -> Result<Vec<V>, Error<Self::Error>>
|
||||
fn get<I>(&self, key: &I::Key) -> Result<Vec<V>, Error<Self::Error>>
|
||||
where
|
||||
I: Index<Model = V>;
|
||||
|
||||
@ -33,7 +33,7 @@ where
|
||||
fn set(&self, value: &V) -> Result<(), Error<Self::Error>>;
|
||||
|
||||
/// Delete all items from the db with a given index.
|
||||
fn delete<I>(&self, key: K) -> Result<(), Error<Self::Error>>
|
||||
fn delete<I>(&self, key: &I::Key) -> Result<(), Error<Self::Error>>
|
||||
where
|
||||
I: Index<Model = V>;
|
||||
|
||||
@ -66,4 +66,3 @@ impl<E> From<bincode::error::EncodeError> for Error<E> {
|
||||
Error::Encode(value)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,12 +42,12 @@ where
|
||||
{
|
||||
type Error = tst::Error;
|
||||
|
||||
fn get<I>(&self, key: &str) -> Result<Vec<M>, super::Error<Self::Error>>
|
||||
fn get<I>(&self, key: &I::Key) -> Result<Vec<M>, super::Error<Self::Error>>
|
||||
where
|
||||
I: Index<Model = M>,
|
||||
{
|
||||
let mut index_db = self.index.lock().expect("can lock index DB");
|
||||
let index_key = Self::index_key(M::db_prefix(), I::key(), key);
|
||||
let index_key = Self::index_key(M::db_prefix(), I::key(), &key.to_string());
|
||||
|
||||
let Some(object_ids) = Self::get_tst_value::<HashSet<u32>>(&mut index_db, &index_key)?
|
||||
else {
|
||||
@ -140,12 +140,12 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete<I>(&self, key: &str) -> Result<(), super::Error<Self::Error>>
|
||||
fn delete<I>(&self, key: &I::Key) -> Result<(), super::Error<Self::Error>>
|
||||
where
|
||||
I: Index<Model = M>,
|
||||
{
|
||||
let mut index_db = self.index.lock().expect("can lock index db");
|
||||
let key = Self::index_key(M::db_prefix(), I::key(), key);
|
||||
let key = Self::index_key(M::db_prefix(), I::key(), &key.to_string());
|
||||
let raw_obj_ids = index_db.get(&key)?;
|
||||
let (obj_ids, _): (HashSet<u32>, _) =
|
||||
bincode::serde::decode_from_slice(&raw_obj_ids, BINCODE_CONFIG)?;
|
||||
|
@ -42,9 +42,13 @@ impl IndexKeyBuilder {
|
||||
}
|
||||
|
||||
/// Unified trait for all models
|
||||
pub trait Model: Debug + Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static {
|
||||
pub trait Model:
|
||||
Debug + Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static
|
||||
{
|
||||
/// Get the database prefix for this model type
|
||||
fn db_prefix() -> &'static str where Self: Sized;
|
||||
fn db_prefix() -> &'static str
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
/// Returns a list of index keys for this model instance
|
||||
/// These keys will be used to create additional indexes in the TST
|
||||
@ -61,13 +65,19 @@ pub trait Model: Debug + Clone + Serialize + for<'de> Deserialize<'de> + Send +
|
||||
fn base_data_mut(&mut self) -> &mut BaseModelData;
|
||||
|
||||
/// Set the ID for this model
|
||||
fn id(mut self, id: u32) -> Self where Self: Sized {
|
||||
fn id(mut self, id: u32) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.base_data_mut().id = id;
|
||||
self
|
||||
}
|
||||
|
||||
/// Build the model, updating the modified timestamp
|
||||
fn build(mut self) -> Self where Self: Sized {
|
||||
fn build(mut self) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.base_data_mut().update_modified();
|
||||
self
|
||||
}
|
||||
@ -78,6 +88,8 @@ pub trait Index {
|
||||
/// The model for which this is an index in the database
|
||||
type Model: Model;
|
||||
|
||||
type Key: ToString + ?Sized;
|
||||
|
||||
/// The key of this index
|
||||
fn key() -> &'static str;
|
||||
}
|
||||
@ -208,3 +220,4 @@ macro_rules! impl_model {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::models::core::model::{BaseModelData, Index, IndexKey, Model};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::models::core::model::{Model, BaseModelData, IndexKey, Index};
|
||||
|
||||
/// Represents a user in the system
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@ -119,6 +119,8 @@ pub struct IsActive;
|
||||
impl Index for UserName {
|
||||
type Model = User;
|
||||
|
||||
type Key = str;
|
||||
|
||||
fn key() -> &'static str {
|
||||
"username"
|
||||
}
|
||||
@ -127,6 +129,8 @@ impl Index for UserName {
|
||||
impl Index for Email {
|
||||
type Model = User;
|
||||
|
||||
type Key = str;
|
||||
|
||||
fn key() -> &'static str {
|
||||
"email"
|
||||
}
|
||||
@ -135,7 +139,10 @@ impl Index for Email {
|
||||
impl Index for IsActive {
|
||||
type Model = User;
|
||||
|
||||
type Key = bool;
|
||||
|
||||
fn key() -> &'static str {
|
||||
"is_active"
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user