Skip to content

Commit 37f8a5f

Browse files
author
Zoran Cvetkov
committed
first attempt at reading entities for mutable ones
1 parent 0530ce1 commit 37f8a5f

File tree

9 files changed

+305
-10
lines changed

9 files changed

+305
-10
lines changed

graph/src/components/store/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::collections::btree_map::Entry;
2121
use std::collections::{BTreeMap, BTreeSet, HashSet};
2222
use std::fmt;
2323
use std::fmt::Display;
24+
use std::ops::Range;
2425
use std::sync::atomic::{AtomicUsize, Ordering};
2526
use std::sync::{Arc, RwLock};
2627
use std::time::Duration;
@@ -1038,6 +1039,14 @@ impl ReadStore for EmptyStore {
10381039
Ok(BTreeMap::new())
10391040
}
10401041

1042+
fn get_range(
1043+
&self,
1044+
_key: &EntityKey,
1045+
_block_range: Range<u32>,
1046+
) -> Result<BTreeMap<BlockNumber, Entity>, StoreError> {
1047+
Ok(BTreeMap::new())
1048+
}
1049+
10411050
fn get_derived(
10421051
&self,
10431052
_query: &DerivedEntityQuery,

graph/src/components/store/traits.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::HashMap;
2+
use std::ops::Range;
23

34
use anyhow::Error;
45
use async_trait::async_trait;
@@ -227,6 +228,13 @@ pub trait ReadStore: Send + Sync + 'static {
227228
keys: BTreeSet<EntityKey>,
228229
) -> Result<BTreeMap<EntityKey, Entity>, StoreError>;
229230

231+
/// Looks up entities using the given store key for a range of blocks.
232+
fn get_range(
233+
&self,
234+
key: &EntityKey,
235+
block_range: Range<u32>,
236+
) -> Result<BTreeMap<BlockNumber, Entity>, StoreError>;
237+
230238
/// Reverse lookup
231239
fn get_derived(
232240
&self,
@@ -249,6 +257,14 @@ impl<T: ?Sized + ReadStore> ReadStore for Arc<T> {
249257
(**self).get_many(keys)
250258
}
251259

260+
fn get_range(
261+
&self,
262+
key: &EntityKey,
263+
block_range: Range<u32>,
264+
) -> Result<BTreeMap<BlockNumber, Entity>, StoreError> {
265+
(**self).get_range(key, block_range)
266+
}
267+
252268
fn get_derived(
253269
&self,
254270
entity_derived: &DerivedEntityQuery,

store/postgres/src/block_range.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ lazy_static! {
5050

5151
/// The range of blocks for which an entity is valid. We need this struct
5252
/// to bind ranges into Diesel queries.
53-
#[derive(Clone, Debug)]
53+
#[derive(Clone, Debug, Copy)]
5454
pub struct BlockRange(Bound<BlockNumber>, Bound<BlockNumber>);
5555

5656
pub(crate) fn first_block_in_range(
@@ -147,6 +147,16 @@ pub enum BlockRangeColumn<'a> {
147147
table_prefix: &'a str,
148148
block: BlockNumber,
149149
},
150+
MutableRange {
151+
table: &'a Table,
152+
table_prefix: &'a str,
153+
block_range: BlockRange, // TODO: check if this is a proper type here (maybe Range<BlockNumber>?)
154+
},
155+
ImmutableRange {
156+
table: &'a Table,
157+
table_prefix: &'a str,
158+
block_range: BlockRange,
159+
},
150160
}
151161

152162
impl<'a> BlockRangeColumn<'a> {
@@ -166,10 +176,35 @@ impl<'a> BlockRangeColumn<'a> {
166176
}
167177
}
168178

179+
// TODO: refactor new and new2 into one. use enum of both BlockNumber and range
180+
pub fn new2(
181+
table: &'a Table,
182+
table_prefix: &'a str,
183+
block_range: std::ops::Range<u32>,
184+
) -> Self {
185+
let st: Bound<BlockNumber> = Bound::Included(block_range.start.try_into().unwrap());
186+
let en: Bound<BlockNumber> = Bound::Excluded(block_range.end.try_into().unwrap());
187+
let block_range: BlockRange = BlockRange(st, en);
188+
if table.immutable {
189+
Self::ImmutableRange {
190+
table,
191+
table_prefix,
192+
block_range,
193+
}
194+
} else {
195+
Self::MutableRange {
196+
table,
197+
table_prefix,
198+
block_range,
199+
}
200+
}
201+
}
202+
169203
pub fn block(&self) -> BlockNumber {
170204
match self {
171205
BlockRangeColumn::Mutable { block, .. } => *block,
172206
BlockRangeColumn::Immutable { block, .. } => *block,
207+
_ => todo!(),
173208
}
174209
}
175210
}
@@ -224,13 +259,48 @@ impl<'a> BlockRangeColumn<'a> {
224259
out.push_bind_param::<Integer, _>(block)
225260
}
226261
}
262+
BlockRangeColumn::MutableRange {
263+
table: _,
264+
table_prefix: _,
265+
block_range: BlockRange(start, finish),
266+
} => {
267+
out.push_sql("block_range && int4range(");
268+
match start {
269+
Bound::Included(block) => out.push_bind_param::<Integer, _>(block)?,
270+
Bound::Excluded(block) => {
271+
out.push_bind_param::<Integer, _>(block)?;
272+
out.push_sql("+1");
273+
}
274+
Bound::Unbounded => todo!(),
275+
};
276+
out.push_sql(",");
277+
match finish {
278+
Bound::Included(block) => {
279+
out.push_bind_param::<Integer, _>(block)?;
280+
out.push_sql("+1");
281+
}
282+
Bound::Excluded(block) => out.push_bind_param::<Integer, _>(block)?,
283+
Bound::Unbounded => todo!(),
284+
};
285+
out.push_sql(")");
286+
Ok(())
287+
}
288+
BlockRangeColumn::ImmutableRange {
289+
table: _,
290+
table_prefix: _,
291+
block_range: _,
292+
} => {
293+
println!("ImmutableRange conatins()");
294+
todo!()
295+
}
227296
}
228297
}
229298

230299
pub fn column_name(&self) -> &str {
231300
match self {
232301
BlockRangeColumn::Mutable { .. } => BLOCK_RANGE_COLUMN,
233302
BlockRangeColumn::Immutable { .. } => BLOCK_COLUMN,
303+
_ => todo!(),
234304
}
235305
}
236306

@@ -245,6 +315,7 @@ impl<'a> BlockRangeColumn<'a> {
245315
out.push_sql(table_prefix);
246316
out.push_sql(BLOCK_COLUMN);
247317
}
318+
_ => todo!(),
248319
}
249320
}
250321

@@ -254,6 +325,7 @@ impl<'a> BlockRangeColumn<'a> {
254325
match self {
255326
BlockRangeColumn::Mutable { .. } => out.push_sql(BLOCK_RANGE_CURRENT),
256327
BlockRangeColumn::Immutable { .. } => out.push_sql("true"),
328+
_ => todo!(),
257329
}
258330
}
259331

@@ -277,6 +349,7 @@ impl<'a> BlockRangeColumn<'a> {
277349
BlockRangeColumn::Immutable { .. } => {
278350
unreachable!("immutable entities can not be updated or deleted")
279351
}
352+
_ => todo!(),
280353
}
281354
}
282355

@@ -285,6 +358,7 @@ impl<'a> BlockRangeColumn<'a> {
285358
match self {
286359
BlockRangeColumn::Mutable { .. } => out.push_sql(BLOCK_RANGE_COLUMN),
287360
BlockRangeColumn::Immutable { .. } => out.push_sql(BLOCK_COLUMN),
361+
_ => todo!(),
288362
}
289363
}
290364

@@ -303,6 +377,7 @@ impl<'a> BlockRangeColumn<'a> {
303377
out.push_sql(" >= ");
304378
out.push_bind_param::<Integer, _>(block)
305379
}
380+
_ => todo!(),
306381
}
307382
}
308383
}

store/postgres/src/deployment_store.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use lru_time_cache::LruCache;
2929
use rand::{seq::SliceRandom, thread_rng};
3030
use std::collections::{BTreeMap, HashMap};
3131
use std::convert::Into;
32-
use std::ops::Deref;
3332
use std::ops::{Bound, DerefMut};
33+
use std::ops::{Deref, Range};
3434
use std::str::FromStr;
3535
use std::sync::{atomic::AtomicUsize, Arc, Mutex};
3636
use std::time::{Duration, Instant};
@@ -1056,6 +1056,17 @@ impl DeploymentStore {
10561056
layout.find_many(&mut conn, ids_for_type, block)
10571057
}
10581058

1059+
pub(crate) fn get_range(
1060+
&self,
1061+
site: Arc<Site>,
1062+
key: &EntityKey,
1063+
block_range: Range<u32>,
1064+
) -> Result<BTreeMap<BlockNumber, Entity>, StoreError> {
1065+
let mut conn = self.get_conn()?;
1066+
let layout = self.layout(&mut conn, site)?;
1067+
layout.find_range(&mut conn, key, block_range)
1068+
}
1069+
10591070
pub(crate) fn get_derived(
10601071
&self,
10611072
site: Arc<Site>,

store/postgres/src/relational.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use std::borrow::Borrow;
4646
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
4747
use std::convert::{From, TryFrom};
4848
use std::fmt::{self, Write};
49+
use std::ops::Range;
4950
use std::str::FromStr;
5051
use std::sync::{Arc, Mutex};
5152
use std::time::{Duration, Instant};
@@ -58,7 +59,7 @@ use crate::{
5859
primary::{Namespace, Site},
5960
relational_queries::{
6061
ClampRangeQuery, EntityData, EntityDeletion, FilterCollection, FilterQuery, FindManyQuery,
61-
FindQuery, InsertQuery, RevertClampQuery, RevertRemoveQuery,
62+
FindQuery, FindRangeQuery, InsertQuery, RevertClampQuery, RevertRemoveQuery,
6263
},
6364
};
6465
use graph::components::store::DerivedEntityQuery;
@@ -514,6 +515,27 @@ impl Layout {
514515
Ok(entities)
515516
}
516517

518+
pub fn find_range(
519+
&self,
520+
conn: &mut PgConnection,
521+
key: &EntityKey,
522+
block_range: Range<u32>,
523+
) -> Result<BTreeMap<BlockNumber, Entity>, StoreError> {
524+
let table = self.table_for_entity(&key.entity_type)?;
525+
let mut entities: BTreeMap<BlockNumber, Entity> = BTreeMap::new();
526+
if let Some(vec) = FindRangeQuery::new(table.as_ref(), key, block_range)
527+
.get_results::<EntityData>(conn)
528+
.optional()?
529+
{
530+
for e in vec {
531+
let block = e.clone().deserialize_block_number::<Entity>()?;
532+
let en = e.deserialize_with_layout::<Entity>(self, None)?;
533+
entities.insert(block, en);
534+
}
535+
}
536+
Ok(entities)
537+
}
538+
517539
pub fn find_derived(
518540
&self,
519541
conn: &mut PgConnection,

0 commit comments

Comments
 (0)