Skip to content

Commit

Permalink
Define scope when storing deltas
Browse files Browse the repository at this point in the history
  • Loading branch information
aleics committed Jan 16, 2025
1 parent e1d4fc0 commit 82e41bd
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 52 deletions.
10 changes: 7 additions & 3 deletions benches/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use delta_search::fixtures::{
Sport,
};
use delta_search::query::{
CompositeFilter, OptionsQueryExecution, Pagination, QueryExecution, QueryScope, Sort,
CompositeFilter, DeltaScope, OptionsQueryExecution, Pagination, QueryExecution, Sort,
SortDirection,
};
use delta_search::Engine;
Expand Down Expand Up @@ -108,14 +108,18 @@ fn bench_apply_deltas(b: &mut Bencher) {
deltas.extend(decrease_score_deltas(&PLAYERS, COUNT));
deltas.extend(switch_sports_deltas(&PLAYERS, COUNT));

let scope = DeltaScope::date(*DATE);

tokio_test::block_on(async {
ENGINE.store_deltas(&NAME, *DATE, &deltas).await.unwrap();
ENGINE.store_deltas(&NAME, &scope, &deltas).await.unwrap();
});

let scope = DeltaScope::date(DATE.next_day().unwrap());

b.iter(move || {
tokio_test::block_on(async {
let query = QueryExecution::new()
.with_scope(QueryScope::date(DATE.next_day().unwrap()))
.with_scope(scope)
.with_pagination(*PAGINATION);

ENGINE.query(&NAME, query).await.unwrap();
Expand Down
8 changes: 4 additions & 4 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use delta_search::fixtures::{
SwitchSportsDelta,
};
use delta_search::query::{
CompositeFilter, OptionsQueryExecution, QueryExecution, QueryScope, Sort, SortDirection,
CompositeFilter, DeltaScope, OptionsQueryExecution, QueryExecution, Sort, SortDirection,
};
use delta_search::Engine;
use time::{Date, Month};
Expand Down Expand Up @@ -72,10 +72,10 @@ async fn main() {
SwitchSportsDelta::create(lionel_messi_id, Sport::Football, Sport::Basketball),
];

let date = Date::from_calendar_date(2023, Month::January, 1).unwrap();
let delta_scope = DeltaScope::date(Date::from_calendar_date(2023, Month::January, 1).unwrap());

engine
.store_deltas(name, date, &switch_sports)
.store_deltas(name, &delta_scope, &switch_sports)
.await
.unwrap();

Expand All @@ -85,7 +85,7 @@ async fn main() {
FieldValue::String(Sport::Basketball.as_string()),
))
.with_sort(Sort::new("score").with_direction(SortDirection::DESC))
.with_scope(QueryScope::date(
.with_scope(DeltaScope::date(
Date::from_calendar_date(2024, Month::January, 1).unwrap(),
));

Expand Down
138 changes: 111 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use std::slice;
use std::sync::Arc;

use thiserror::Error;
use time::Date;
use tokio::sync::RwLock;

use query::QueryError;
use storage::{StorageError, StoredDeltaScope};
use query::{DeltaScope, QueryError};
use storage::StorageError;

use crate::data::{DataItem, DataItemId};
use crate::query::{DeltaChange, FilterOption, OptionsQueryExecution, QueryExecution};
Expand Down Expand Up @@ -109,11 +108,9 @@ impl Engine {
pub async fn store_deltas(
&self,
name: &str,
date: Date,
scope: &DeltaScope,
deltas: &[DeltaChange],
) -> Result<(), EngineError> {
let scope = StoredDeltaScope::date(date);

if let Some(entry) = self.entities.get(name) {
let entity = entry.read().await;
entity.add_deltas(scope, deltas)?;
Expand Down Expand Up @@ -170,8 +167,8 @@ mod tests {
michael_jordan, roger, DecreaseScoreDelta, Player, Sport, SwitchSportsDelta, TestRunners,
};
use crate::query::{
CompositeFilter, FilterOption, OptionsQueryExecution, Pagination, QueryExecution,
QueryScope, Sort, SortDirection,
CompositeFilter, DeltaScope, FilterOption, OptionsQueryExecution, Pagination,
QueryExecution, Sort, SortDirection,
};

lazy_static! {
Expand Down Expand Up @@ -493,23 +490,23 @@ mod tests {
])
.await;

let delta_scope =
DeltaScope::date(Date::from_calendar_date(2023, Month::January, 1).unwrap());
let deltas = [
DecreaseScoreDelta::create(MICHAEL_JORDAN.id, 10.0),
DecreaseScoreDelta::create(LIONEL_MESSI.id, 9.0),
];

runner
.engine
.store_deltas(
&runner.name,
Date::from_calendar_date(2023, Month::January, 1).unwrap(),
&[
DecreaseScoreDelta::create(MICHAEL_JORDAN.id, 10.0),
DecreaseScoreDelta::create(LIONEL_MESSI.id, 9.0),
],
)
.store_deltas(&runner.name, &delta_scope, &deltas)
.await
.unwrap();

// when
let execution = QueryExecution::new()
.with_filter(CompositeFilter::eq("sport", FieldValue::str("Football")))
.with_scope(QueryScope::date(
.with_scope(DeltaScope::date(
Date::from_calendar_date(2024, Month::January, 1).unwrap(),
));

Expand Down Expand Up @@ -546,24 +543,24 @@ mod tests {
])
.await;

let delta_scope =
DeltaScope::date(Date::from_calendar_date(2023, Month::January, 1).unwrap());
let deltas = [SwitchSportsDelta::create(
MICHAEL_JORDAN.id,
Sport::Basketball,
Sport::Football,
)];

runner
.engine
.store_deltas(
&runner.name,
Date::from_calendar_date(2023, Month::January, 1).unwrap(),
&[SwitchSportsDelta::create(
0,
Sport::Basketball,
Sport::Football,
)],
)
.store_deltas(&runner.name, &delta_scope, &deltas)
.await
.unwrap();

// when
let execution = QueryExecution::new()
.with_filter(CompositeFilter::eq("sport", FieldValue::str("Football")))
.with_scope(QueryScope::date(
.with_scope(DeltaScope::date(
Date::from_calendar_date(2024, Month::January, 1).unwrap(),
));

Expand All @@ -590,6 +587,93 @@ mod tests {
);
}

#[tokio::test]
async fn query_with_delta_context() {
// given
let runner = STORAGES
.start_runner(vec![
MICHAEL_JORDAN.clone(),
LIONEL_MESSI.clone(),
CRISTIANO_RONALDO.clone(),
])
.await;

let context = 0;
let delta_scope = DeltaScope::context(
context,
Date::from_calendar_date(2023, Month::January, 1).unwrap(),
);

let deltas = [SwitchSportsDelta::create(
MICHAEL_JORDAN.id,
Sport::Basketball,
Sport::Football,
)];

runner
.engine
.store_deltas(&runner.name, &delta_scope, &deltas)
.await
.unwrap();

// when
let filter = CompositeFilter::eq("sport", FieldValue::str("Football"));

let execution_without_context = QueryExecution::new()
.with_filter(filter.clone())
.with_scope(DeltaScope::date(
Date::from_calendar_date(2024, Month::January, 1).unwrap(),
));

let mut matches_without_context = runner
.engine
.query(&runner.name, execution_without_context)
.await
.unwrap();

// then
matches_without_context.sort_by(|a, b| a.id.cmp(&b.id));

assert_eq!(
matches_without_context,
vec![LIONEL_MESSI.clone(), CRISTIANO_RONALDO.clone(),]
);

// when
let execution_with_context = QueryExecution::new()
.with_filter(filter.clone())
.with_scope(DeltaScope::context(
context,
Date::from_calendar_date(2024, Month::January, 1).unwrap(),
));

let mut matches_with_context = runner
.engine
.query(&runner.name, execution_with_context)
.await
.unwrap();

// then
matches_with_context.sort_by(|a, b| a.id.cmp(&b.id));

assert_eq!(
matches_with_context,
vec![
Player {
id: 0,
name: "Michael Jordan".to_string(),
score: Some(10.0),
sport: Sport::Football,
birth_date: "1963-02-17".to_string(),
active: false,
}
.as_item(),
LIONEL_MESSI.clone(),
CRISTIANO_RONALDO.clone(),
]
);
}

#[tokio::test]
async fn query_pagination() {
// given
Expand Down
22 changes: 11 additions & 11 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ impl FilterOption {
}

#[derive(Debug, PartialEq, Deserialize)]
pub struct QueryScope {
pub struct DeltaScope {
pub(crate) context: Option<u64>,
pub(crate) date: Date,
}

impl QueryScope {
impl DeltaScope {
pub fn date(date: Date) -> Self {
QueryScope {
DeltaScope {
context: None,
date,
}
}

pub fn context(context: u64, date: Date) -> Self {
QueryScope {
DeltaScope {
context: Some(context),
date,
}
Expand Down Expand Up @@ -117,7 +117,7 @@ impl QueryIndices {
#[derive(Default)]
pub struct OptionsQueryExecution {
filter: Option<CompositeFilter>,
scope: Option<QueryScope>,
scope: Option<DeltaScope>,
ref_fields: Option<Vec<String>>,
}

Expand All @@ -135,7 +135,7 @@ impl OptionsQueryExecution {
self
}

pub fn with_scope(mut self, scope: QueryScope) -> Self {
pub fn with_scope(mut self, scope: DeltaScope) -> Self {
self.scope = Some(scope);
self
}
Expand Down Expand Up @@ -166,7 +166,7 @@ impl OptionsQueryExecution {
pub struct QueryExecution {
filter: Option<CompositeFilter>,
sort: Option<Sort>,
scope: Option<QueryScope>,
scope: Option<DeltaScope>,
pagination: Option<Pagination>,
ref_fields: Vec<String>,
}
Expand All @@ -193,7 +193,7 @@ impl QueryExecution {
self
}

pub fn with_scope(mut self, scope: QueryScope) -> Self {
pub fn with_scope(mut self, scope: DeltaScope) -> Self {
self.scope = Some(scope);
self
}
Expand Down Expand Up @@ -266,7 +266,7 @@ impl QueryExecution {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub enum CompositeFilter {
And(Vec<CompositeFilter>),
Or(Vec<CompositeFilter>),
Expand Down Expand Up @@ -410,13 +410,13 @@ impl Sort {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub struct Filter {
name: String,
operation: FilterOperation,
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub enum FilterOperation {
Eq(FieldValue),
Between(FieldValue, FieldValue),
Expand Down
Loading

0 comments on commit 82e41bd

Please sign in to comment.