Skip to content

Commit 700c11b

Browse files
committed
Use a single key to store deltas
To improve the distribution of the stored deltas, we removed the deep key value maps and use a custom key to encode the `context` and `timestamp` of a stored delta. Thus, improving the amount of data that needs to be loaded for each operation. Added additional end-to-end tests and benchmarks to validate the behavior.
1 parent 4a69c33 commit 700c11b

File tree

7 files changed

+397
-96
lines changed

7 files changed

+397
-96
lines changed

benches/query.rs

+33
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,36 @@ fn bench_apply_deltas(b: &mut Bencher) {
126126
});
127127
});
128128
}
129+
130+
#[bench]
131+
fn bench_apply_deltas_with_multiple_dates(b: &mut Bencher) {
132+
let mut deltas = Vec::new();
133+
134+
deltas.extend(decrease_score_deltas(&PLAYERS, COUNT));
135+
deltas.extend(switch_sports_deltas(&PLAYERS, COUNT));
136+
137+
let mut date: Date = *DATE;
138+
139+
tokio_test::block_on(async {
140+
for delta_chunk in deltas.chunks(10) {
141+
ENGINE
142+
.store_deltas(&NAME, &DeltaScope::date(date), delta_chunk)
143+
.await
144+
.unwrap();
145+
146+
date = date.next_day().unwrap();
147+
}
148+
});
149+
150+
b.iter(move || {
151+
tokio_test::block_on(async {
152+
let scope = DeltaScope::date(date.next_day().unwrap());
153+
154+
let query = QueryExecution::new()
155+
.with_scope(scope)
156+
.with_pagination(*PAGINATION);
157+
158+
ENGINE.query(&NAME, query).await.unwrap();
159+
});
160+
});
161+
}

examples/simple.rs

+39-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use delta_search::data::FieldValue;
22
use delta_search::fixtures::{
3-
create_players_storage, cristiano_ronaldo, david, lionel_messi, michael_jordan, roger, Sport,
4-
SwitchSportsDelta,
3+
create_players_storage, cristiano_ronaldo, david, lionel_messi, michael_jordan, roger,
4+
DecreaseScoreDelta, Sport, SwitchSportsDelta,
55
};
66
use delta_search::query::{
77
CompositeFilter, DeltaScope, OptionsQueryExecution, QueryExecution, Sort, SortDirection,
@@ -50,7 +50,7 @@ async fn main() {
5050
.with_sort(Sort::new("score").with_direction(SortDirection::DESC));
5151
let players = engine.query(name, query).await;
5252

53-
println!("Basketball players sorted by score: {:?}", players);
53+
println!("Basketball players sorted by score:\n{:?}\n", players);
5454

5555
let players = engine
5656
.query(
@@ -65,7 +65,7 @@ async fn main() {
6565
)
6666
.await;
6767

68-
println!("Players born in the 80s: {:?}", players);
68+
println!("Players born in the 80s:\n{:?}\n", players);
6969

7070
let switch_sports = vec![
7171
SwitchSportsDelta::create(michael_jordan_id, Sport::Basketball, Sport::Football),
@@ -92,8 +92,37 @@ async fn main() {
9292
let players = engine.query(name, query).await;
9393

9494
println!(
95-
"Basketball players sorted by score after switching sports in 2023: {:?}",
96-
players
95+
"Basketball players sorted by score after switching sports in 2023:\n{:?}\n",
96+
players.unwrap()
97+
);
98+
99+
let lower_scores = vec![
100+
DecreaseScoreDelta::create(michael_jordan_id, 10.0),
101+
DecreaseScoreDelta::create(lionel_messi_id, 9.0),
102+
];
103+
104+
let delta_scope = DeltaScope::context(
105+
0,
106+
Date::from_calendar_date(2023, Month::January, 1).unwrap(),
107+
);
108+
109+
engine
110+
.store_deltas(name, &delta_scope, &lower_scores)
111+
.await
112+
.unwrap();
113+
114+
let query = QueryExecution::new()
115+
.with_sort(Sort::new("score").with_direction(SortDirection::DESC))
116+
.with_scope(DeltaScope::context(
117+
0,
118+
Date::from_calendar_date(2024, Month::January, 1).unwrap(),
119+
));
120+
121+
let players = engine.query(name, query).await;
122+
123+
println!(
124+
"Players sorted by score after decreasing their score by 1:\n{:?}\n",
125+
players.unwrap()
97126
);
98127

99128
engine.remove(name, &david_id).await.unwrap();
@@ -108,5 +137,8 @@ async fn main() {
108137
)
109138
.await;
110139

111-
println!("Players playing basketball after deletion: {:?}", players);
140+
println!(
141+
"Players playing basketball after deletion:\n{:?}\n",
142+
players.unwrap()
143+
);
112144
}

src/fixtures.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl Player {
270270
pub struct DecreaseScoreDelta;
271271

272272
impl DecreaseScoreDelta {
273-
pub(crate) fn create(id: DataItemId, score: f64) -> DeltaChange {
273+
pub fn create(id: DataItemId, score: f64) -> DeltaChange {
274274
DeltaChange::new(
275275
id,
276276
"score".to_string(),

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ fn parse_date(string: &str) -> Result<Date, time::error::Parse> {
330330
#[derive(Deserialize)]
331331
#[serde(rename_all = "camelCase")]
332332
struct DeltaScopeInput {
333-
context: Option<u64>,
333+
context: Option<u32>,
334334
date: String,
335335
}
336336

src/query.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ impl FilterOption {
2525

2626
#[derive(Debug, PartialEq, Deserialize)]
2727
pub struct DeltaScope {
28-
pub(crate) context: Option<u64>,
28+
pub(crate) context: Option<u32>,
2929
pub(crate) date: Date,
3030
}
3131

3232
impl DeltaScope {
33-
pub fn new(context: Option<u64>, date: Date) -> Self {
33+
pub fn new(context: Option<u32>, date: Date) -> Self {
3434
Self { context, date }
3535
}
3636

@@ -41,7 +41,7 @@ impl DeltaScope {
4141
}
4242
}
4343

44-
pub fn context(context: u64, date: Date) -> Self {
44+
pub fn context(context: u32, date: Date) -> Self {
4545
DeltaScope {
4646
context: Some(context),
4747
date,

0 commit comments

Comments
 (0)