Skip to content

Commit 9b184f9

Browse files
author
Zoran Cvetkov
committed
request all entity types in a single SQL query
1 parent b4ad24f commit 9b184f9

File tree

10 files changed

+126
-126
lines changed

10 files changed

+126
-126
lines changed

graph/src/blockchain/block_stream.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -437,33 +437,39 @@ async fn get_entities_for_range(
437437
schema: &InputSchema,
438438
from: BlockNumber,
439439
to: BlockNumber,
440-
) -> Result<BTreeMap<BlockNumber, Vec<EntityWithType>>, Error> {
441-
let mut entities_by_block = BTreeMap::new();
442-
440+
/*
441+
) -> Result<BTreeMap<BlockNumber, Vec<EntityWithType>>, Error> {
442+
let mut entities_by_block = BTreeMap::new();
443+
444+
for entity_name in &filter.entities {
445+
let entity_type = schema.entity_type(entity_name)?;
446+
447+
let entity_ranges = store.get_range(&entity_type, from..to)?;
448+
449+
for (block_number, entity_vec) in entity_ranges {
450+
let mut entity_vec = entity_vec
451+
.into_iter()
452+
.map(|e| EntityWithType {
453+
entity_type: entity_type.clone(),
454+
entity: e,
455+
})
456+
.collect();
457+
458+
entities_by_block
459+
.entry(block_number)
460+
.and_modify(|existing_vec: &mut Vec<EntityWithType>| {
461+
existing_vec.append(&mut entity_vec);
462+
})
463+
.or_insert(entity_vec);
464+
}
465+
*/
466+
) -> Result<BTreeMap<BlockNumber, Vec<Entity>>, Error> {
467+
let mut entity_types = vec![];
443468
for entity_name in &filter.entities {
444469
let entity_type = schema.entity_type(entity_name)?;
445-
446-
let entity_ranges = store.get_range(&entity_type, from..to)?;
447-
448-
for (block_number, entity_vec) in entity_ranges {
449-
let mut entity_vec = entity_vec
450-
.into_iter()
451-
.map(|e| EntityWithType {
452-
entity_type: entity_type.clone(),
453-
entity: e,
454-
})
455-
.collect();
456-
457-
entities_by_block
458-
.entry(block_number)
459-
.and_modify(|existing_vec: &mut Vec<EntityWithType>| {
460-
existing_vec.append(&mut entity_vec);
461-
})
462-
.or_insert(entity_vec);
463-
}
470+
entity_types.push(entity_type);
464471
}
465-
466-
Ok(entities_by_block)
472+
Ok(store.get_range(entity_types, from..to)?)
467473
}
468474

469475
impl<C: Blockchain> TriggersAdapterWrapper<C> {

graph/src/components/store/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ impl ReadStore for EmptyStore {
10411041

10421042
fn get_range(
10431043
&self,
1044-
_entity_type: &EntityType,
1044+
_entity_types: Vec<EntityType>,
10451045
_block_range: Range<BlockNumber>,
10461046
) -> Result<BTreeMap<BlockNumber, Vec<Entity>>, StoreError> {
10471047
Ok(BTreeMap::new())

graph/src/components/store/traits.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ pub trait ReadStore: Send + Sync + 'static {
231231
/// Looks up entities using the given store key for a range of blocks.
232232
fn get_range(
233233
&self,
234-
entity_type: &EntityType,
234+
entity_types: Vec<EntityType>,
235235
block_range: Range<BlockNumber>,
236236
) -> Result<BTreeMap<BlockNumber, Vec<Entity>>, StoreError>;
237237

@@ -259,10 +259,10 @@ impl<T: ?Sized + ReadStore> ReadStore for Arc<T> {
259259

260260
fn get_range(
261261
&self,
262-
entity_type: &EntityType,
262+
entity_types: Vec<EntityType>,
263263
block_range: Range<BlockNumber>,
264264
) -> Result<BTreeMap<BlockNumber, Vec<Entity>>, StoreError> {
265-
(**self).get_range(entity_type, block_range)
265+
(**self).get_range(entity_types, block_range)
266266
}
267267

268268
fn get_derived(

store/postgres/src/block_range.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl EntityBlockRange {
161161
let BlockRange(start, finish) = block_range;
162162

163163
self.compare_column(out);
164-
out.push_sql(" >= ");
164+
out.push_sql(">= ");
165165
match start {
166166
Bound::Included(block) => out.push_bind_param::<Integer, _>(block)?,
167167
Bound::Excluded(block) => {
@@ -170,9 +170,9 @@ impl EntityBlockRange {
170170
}
171171
Bound::Unbounded => unimplemented!(),
172172
};
173-
out.push_sql(" AND ");
173+
out.push_sql(" and");
174174
self.compare_column(out);
175-
out.push_sql(" <= ");
175+
out.push_sql("<= ");
176176
match finish {
177177
Bound::Included(block) => {
178178
out.push_bind_param::<Integer, _>(block)?;

store/postgres/src/deployment_store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,12 +1059,12 @@ impl DeploymentStore {
10591059
pub(crate) fn get_range(
10601060
&self,
10611061
site: Arc<Site>,
1062-
entity_type: &EntityType,
1062+
entity_types: Vec<EntityType>,
10631063
block_range: Range<BlockNumber>,
10641064
) -> Result<BTreeMap<BlockNumber, Vec<Entity>>, StoreError> {
10651065
let mut conn = self.get_conn()?;
10661066
let layout = self.layout(&mut conn, site)?;
1067-
layout.find_range(&mut conn, entity_type, block_range)
1067+
layout.find_range(&mut conn, entity_types, block_range)
10681068
}
10691069

10701070
pub(crate) fn get_derived(

store/postgres/src/relational.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,15 @@ impl Layout {
518518
pub fn find_range(
519519
&self,
520520
conn: &mut PgConnection,
521-
entity_type: &EntityType,
521+
entity_types: Vec<EntityType>,
522522
block_range: Range<BlockNumber>,
523523
) -> Result<BTreeMap<BlockNumber, Vec<Entity>>, StoreError> {
524-
let table = self.table_for_entity(entity_type)?;
524+
let mut tables = vec![];
525+
for et in entity_types {
526+
tables.push(self.table_for_entity(&et)?.as_ref())
527+
}
525528
let mut entities: BTreeMap<BlockNumber, Vec<Entity>> = BTreeMap::new();
526-
if let Some(vec) = FindRangeQuery::new(table.as_ref(), block_range)
529+
if let Some(vec) = FindRangeQuery::new(&tables, block_range)
527530
.get_results::<EntityData>(conn)
528531
.optional()?
529532
{

store/postgres/src/relational_queries.rs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,37 +2008,64 @@ impl<'a, Conn> RunQueryDsl<Conn> for FindQuery<'a> {}
20082008

20092009
#[derive(Debug, Clone)]
20102010
pub struct FindRangeQuery<'a> {
2011-
table: &'a Table,
2012-
eb_range: EntityBlockRange,
2011+
tables: &'a Vec<&'a Table>,
2012+
imm_range: EntityBlockRange,
2013+
mut_range: EntityBlockRange,
20132014
}
20142015

20152016
impl<'a> FindRangeQuery<'a> {
2016-
pub fn new(table: &'a Table, block_range: Range<BlockNumber>) -> Self {
2017-
let eb_range = EntityBlockRange::new(table.immutable, block_range);
2018-
Self { table, eb_range }
2017+
pub fn new(tables: &'a Vec<&Table>, block_range: Range<BlockNumber>) -> Self {
2018+
let imm_range = EntityBlockRange::new(true, block_range.clone());
2019+
let mut_range = EntityBlockRange::new(false, block_range);
2020+
Self {
2021+
tables,
2022+
imm_range,
2023+
mut_range,
2024+
}
20192025
}
20202026
}
20212027

20222028
impl<'a> QueryFragment<Pg> for FindRangeQuery<'a> {
20232029
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
20242030
out.unsafe_to_cache_prepared();
20252031

2026-
// Generate
2027-
// select '..' as entity, to_jsonb(e.*) as data
2028-
// from schema.table e where id = $1
2029-
out.push_sql("select ");
2030-
out.push_bind_param::<Text, _>(self.table.object.as_str())?;
2031-
out.push_sql(" as entity, to_jsonb(e.*) as data\n");
2032-
out.push_sql(" from ");
2033-
out.push_sql(self.table.qualified_name.as_str());
2034-
out.push_sql(" e\n where ");
2035-
// TODO: do we need to care about it?
2036-
// if self.table.has_causality_region {
2037-
// out.push_sql("causality_region = ");
2038-
// out.push_bind_param::<Integer, _>(&self.key.causality_region)?;
2039-
// out.push_sql(" and ");
2040-
// }
2041-
self.eb_range.contains(&mut out)
2032+
let mut iter = self.tables.iter().peekable();
2033+
while let Some(table) = iter.next() {
2034+
// Generate
2035+
// select '..' as entity, to_jsonb(e.*) as data, block$ as block_number
2036+
// from schema.table e where id = $1
2037+
out.push_sql("select ");
2038+
out.push_bind_param::<Text, _>(table.object.as_str())?;
2039+
out.push_sql(" as entity, to_jsonb(e.*) as data,");
2040+
if table.immutable {
2041+
self.imm_range.compare_column(&mut out)
2042+
} else {
2043+
self.mut_range.compare_column(&mut out)
2044+
}
2045+
out.push_sql("as block_number\n");
2046+
out.push_sql(" from ");
2047+
out.push_sql(table.qualified_name.as_str());
2048+
out.push_sql(" e\n where");
2049+
// TODO: do we need to care about it?
2050+
// if self.table.has_causality_region {
2051+
// out.push_sql("causality_region = ");
2052+
// out.push_bind_param::<Integer, _>(&self.key.causality_region)?;
2053+
// out.push_sql(" and ");
2054+
// }
2055+
if table.immutable {
2056+
self.imm_range.contains(&mut out)?;
2057+
} else {
2058+
self.mut_range.contains(&mut out)?;
2059+
}
2060+
// more elements left?
2061+
if iter.peek().is_some() {
2062+
out.push_sql("\nunion all\n"); // with the next
2063+
} else {
2064+
out.push_sql("\norder by block_number"); // on the last
2065+
}
2066+
}
2067+
2068+
Ok(())
20422069
}
20432070
}
20442071

store/postgres/src/writable.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,15 @@ impl SyncStore {
354354

355355
fn get_range(
356356
&self,
357-
entity_type: &EntityType,
357+
entity_types: Vec<EntityType>,
358358
block_range: Range<BlockNumber>,
359359
) -> Result<BTreeMap<BlockNumber, Vec<Entity>>, StoreError> {
360360
retry::forever(&self.logger, "get_range", || {
361-
self.writable
362-
.get_range(self.site.cheap_clone(), entity_type, block_range.clone())
361+
self.writable.get_range(
362+
self.site.cheap_clone(),
363+
entity_types.clone(),
364+
block_range.clone(),
365+
)
363366
})
364367
}
365368

@@ -1230,10 +1233,10 @@ impl Queue {
12301233

12311234
fn get_range(
12321235
&self,
1233-
entity_type: &EntityType,
1236+
entity_types: Vec<EntityType>,
12341237
block_range: Range<BlockNumber>,
12351238
) -> Result<BTreeMap<BlockNumber, Vec<Entity>>, StoreError> {
1236-
self.store.get_range(entity_type, block_range)
1239+
self.store.get_range(entity_types, block_range)
12371240
}
12381241

12391242
fn get_derived(
@@ -1450,12 +1453,12 @@ impl Writer {
14501453

14511454
fn get_range(
14521455
&self,
1453-
entity_type: &EntityType,
1456+
entity_types: Vec<EntityType>,
14541457
block_range: Range<BlockNumber>,
14551458
) -> Result<BTreeMap<BlockNumber, Vec<Entity>>, StoreError> {
14561459
match self {
1457-
Writer::Sync(store) => store.get_range(entity_type, block_range),
1458-
Writer::Async { queue, .. } => queue.get_range(entity_type, block_range),
1460+
Writer::Sync(store) => store.get_range(entity_types, block_range),
1461+
Writer::Async { queue, .. } => queue.get_range(entity_types, block_range),
14591462
}
14601463
}
14611464

@@ -1589,10 +1592,10 @@ impl ReadStore for WritableStore {
15891592

15901593
fn get_range(
15911594
&self,
1592-
entity_type: &EntityType,
1595+
entity_types: Vec<EntityType>,
15931596
block_range: Range<BlockNumber>,
15941597
) -> Result<BTreeMap<BlockNumber, Vec<Entity>>, StoreError> {
1595-
self.writer.get_range(entity_type, block_range)
1598+
self.writer.get_range(entity_types, block_range)
15961599
}
15971600

15981601
fn get_derived(

store/test-store/tests/graph/entity_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl ReadStore for MockStore {
6969

7070
fn get_range(
7171
&self,
72-
_entity_type: &EntityType,
72+
_entity_types: Vec<EntityType>,
7373
_block_range: Range<BlockNumber>,
7474
) -> Result<BTreeMap<BlockNumber, Vec<Entity>>, StoreError> {
7575
todo!()

0 commit comments

Comments
 (0)