Skip to content

Commit ff43b85

Browse files
committed
Extend query to allow to define delta scope
Definition of custom SQL keywords: * `BRANCH`: defines which branch should be used for the scope * `AS OF`: defines the date in which the query needs to be run.
1 parent d6d0855 commit ff43b85

File tree

7 files changed

+185
-113
lines changed

7 files changed

+185
-113
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ cargo run --release
4141

4242
- `POST /entities/{entity_name}`: define a new entity with a given name.
4343
- `PUT /data/{entity_name}`: store or update data in bulk in an entity entry.
44-
- `POST /deltas/{entity_name}`: store deltas with a given context in an entity entry.
44+
- `POST /deltas/{entity_name}`: store deltas with a given branch in an entity entry.
4545
- `PUT /indices/{entity_name}`: create a new index for a given property in an entity entry.
4646
- `POST /options`: list filter options given a search query.
4747
- `POST /search`: send a search query.

examples/simple.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ fn main() -> Result<(), Error> {
8686
DecreaseScoreDelta::create(lionel_messi_id, 9.0),
8787
];
8888

89-
let delta_scope = DeltaScope::context(0, Date::from_calendar_date(2023, Month::January, 1)?);
89+
let delta_scope = DeltaScope::branch(0, Date::from_calendar_date(2023, Month::January, 1)?);
9090

9191
engine.store_deltas(name, &delta_scope, lower_scores)?;
9292

9393
let query = QueryExecution::new()
9494
.for_entity(name.to_string())
9595
.with_sort(Sort::new("score").with_direction(SortDirection::DESC))
96-
.with_scope(DeltaScope::context(
96+
.with_scope(DeltaScope::branch(
9797
0,
9898
Date::from_calendar_date(2024, Month::January, 1)?,
9999
));

src/lib.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -575,17 +575,17 @@ mod tests {
575575
}
576576

577577
#[test]
578-
fn query_with_delta_context() {
578+
fn query_with_delta_branch() {
579579
// given
580580
let runner = STORAGES.start_runner(vec![
581581
MICHAEL_JORDAN.clone(),
582582
LIONEL_MESSI.clone(),
583583
CRISTIANO_RONALDO.clone(),
584584
]);
585585

586-
let context = 0;
587-
let delta_scope = DeltaScope::context(
588-
context,
586+
let branch = 0;
587+
let delta_scope = DeltaScope::branch(
588+
branch,
589589
Date::from_calendar_date(2023, Month::January, 1).unwrap(),
590590
);
591591

@@ -602,39 +602,39 @@ mod tests {
602602
// when
603603
let filter = CompositeFilter::eq("sport", FieldValue::str("Football"));
604604

605-
let execution_without_context = QueryExecution::new()
605+
let execution_without_branch = QueryExecution::new()
606606
.for_entity(runner.name.clone())
607607
.with_filter(filter.clone())
608608
.with_scope(DeltaScope::date(
609609
Date::from_calendar_date(2024, Month::January, 1).unwrap(),
610610
));
611611

612-
let mut matches_without_context = runner.engine.query(execution_without_context).unwrap();
612+
let mut matches_without_branch = runner.engine.query(execution_without_branch).unwrap();
613613

614614
// then
615-
matches_without_context.sort_by(|a, b| a.id.cmp(&b.id));
615+
matches_without_branch.sort_by(|a, b| a.id.cmp(&b.id));
616616

617617
assert_eq!(
618-
matches_without_context,
618+
matches_without_branch,
619619
vec![LIONEL_MESSI.clone(), CRISTIANO_RONALDO.clone(),]
620620
);
621621

622622
// when
623-
let execution_with_context = QueryExecution::new()
623+
let execution_with_branch = QueryExecution::new()
624624
.for_entity(runner.name.clone())
625625
.with_filter(filter.clone())
626-
.with_scope(DeltaScope::context(
627-
context,
626+
.with_scope(DeltaScope::branch(
627+
branch,
628628
Date::from_calendar_date(2024, Month::January, 1).unwrap(),
629629
));
630630

631-
let mut matches_with_context = runner.engine.query(execution_with_context).unwrap();
631+
let mut matches_with_branch = runner.engine.query(execution_with_branch).unwrap();
632632

633633
// then
634-
matches_with_context.sort_by(|a, b| a.id.cmp(&b.id));
634+
matches_with_branch.sort_by(|a, b| a.id.cmp(&b.id));
635635

636636
assert_eq!(
637-
matches_with_context,
637+
matches_with_branch,
638638
vec![
639639
Player {
640640
id: 0,

src/main.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,7 @@ impl App {
9999
}
100100

101101
fn build_query_execution(input: QueryInput) -> Result<QueryExecution, AppError> {
102-
let mut execution =
103-
QueryExecution::parse_query(&input.query).map_err(|_| AppError::InvalidFilterQuery)?;
104-
105-
if let Some(scope) = input.scope {
106-
execution = execution.with_scope(scope.map_delta_scope()?);
107-
}
108-
109-
Ok(execution)
102+
QueryExecution::parse_query(&input.query).map_err(|_| AppError::InvalidFilterQuery)
110103
}
111104

112105
fn options(&self, input: QueryOptionsInput) -> Result<Vec<FilterOption>, AppError> {
@@ -121,14 +114,7 @@ impl App {
121114
fn build_options_execution(
122115
input: QueryOptionsInput,
123116
) -> Result<OptionsQueryExecution, AppError> {
124-
let mut execution = OptionsQueryExecution::parse_query(&input.query)
125-
.map_err(|_| AppError::InvalidFilterQuery)?;
126-
127-
if let Some(scope) = input.scope {
128-
execution = execution.with_scope(scope.map_delta_scope()?);
129-
}
130-
131-
Ok(execution)
117+
OptionsQueryExecution::parse_query(&input.query).map_err(|_| AppError::InvalidFilterQuery)
132118
}
133119

134120
fn create_index(&self, name: &str, input: CreateIndexInput) -> Result<(), AppError> {
@@ -255,7 +241,7 @@ async fn bulk_upsert_entity(
255241

256242
#[derive(Deserialize)]
257243
#[serde(rename_all = "camelCase")]
258-
struct BulkStoreDeltas {
244+
struct BulkStoreDeltasInput {
259245
scope: DeltaScopeInput,
260246
deltas: Vec<DeltaChangeInput>,
261247
}
@@ -308,7 +294,7 @@ fn parse_date(string: &str) -> Result<Date, time::error::Parse> {
308294
#[derive(Debug, Deserialize)]
309295
#[serde(rename_all = "camelCase")]
310296
struct DeltaScopeInput {
311-
context: Option<u32>,
297+
branch: Option<u32>,
312298
date: String,
313299
}
314300

@@ -318,14 +304,14 @@ impl DeltaScopeInput {
318304
message: "Date format is invalid. Only ISO 8601 is supported.".to_string(),
319305
})?;
320306

321-
Ok(DeltaScope::new(self.context, date))
307+
Ok(DeltaScope::new(self.branch, date))
322308
}
323309
}
324310

325311
async fn bulk_add_deltas(
326312
State(search): State<App>,
327313
Path(name): Path<String>,
328-
Json(input): Json<BulkStoreDeltas>,
314+
Json(input): Json<BulkStoreDeltasInput>,
329315
) -> Result<Json<()>, AppError> {
330316
search.add_deltas(&name, input.scope, input.deltas)?;
331317
Ok(Json(()))
@@ -335,7 +321,6 @@ async fn bulk_add_deltas(
335321
#[serde(rename_all = "camelCase")]
336322
struct QueryOptionsInput {
337323
query: String,
338-
scope: Option<DeltaScopeInput>,
339324
}
340325

341326
async fn options(
@@ -376,7 +361,6 @@ async fn create_index(
376361
#[serde(rename_all = "camelCase")]
377362
struct QueryInput {
378363
query: String,
379-
scope: Option<DeltaScopeInput>,
380364
}
381365

382366
#[derive(Debug, Serialize)]

0 commit comments

Comments
 (0)