Skip to content

Commit

Permalink
address review
Browse files Browse the repository at this point in the history
  • Loading branch information
zorancv committed Dec 9, 2024
1 parent ebfe059 commit 70d511a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 32 deletions.
12 changes: 6 additions & 6 deletions graph/src/blockchain/block_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,12 @@ async fn get_entities_for_range(
from: BlockNumber,
to: BlockNumber,
) -> Result<BTreeMap<BlockNumber, Vec<EntityWithType>>, Error> {
let mut entity_types = vec![];
for entity_name in &filter.entities {
let entity_type = schema.entity_type(entity_name)?;
entity_types.push(entity_type);
}
Ok(store.get_range(entity_types, CausalityRegion::ONCHAIN, from..to)?)
let entity_types: Result<Vec<EntityType>> = filter
.entities
.iter()
.map(|name| schema.entity_type(name))
.collect();
Ok(store.get_range(entity_types?, CausalityRegion::ONCHAIN, from..to)?)
}

impl<C: Blockchain> TriggersAdapterWrapper<C> {
Expand Down
23 changes: 13 additions & 10 deletions store/postgres/src/block_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,32 @@ impl<'a> QueryFragment<Pg> for BlockRangeUpperBoundClause<'a> {
}
}

#[derive(Debug, Clone, Copy)]
pub enum BoundSide {
Lower,
Upper,
}

/// Helper for generating SQL fragments for selecting entities in a specific block range
#[derive(Debug, Clone, Copy)]
pub enum EntityBlockRange {
Mutable((BlockRange, bool)),
Mutable((BlockRange, BoundSide)),
Immutable(BlockRange),
}

impl EntityBlockRange {
pub fn new(
immutable: bool,
block_range: std::ops::Range<BlockNumber>,
is_uppper_range: bool,
bound_side: BoundSide,
) -> Self {
let start: Bound<BlockNumber> = Bound::Included(block_range.start);
let end: Bound<BlockNumber> = Bound::Excluded(block_range.end);
let block_range: BlockRange = BlockRange(start, end);
if immutable {
Self::Immutable(block_range)
} else {
Self::Mutable((block_range, is_uppper_range))
Self::Mutable((block_range, bound_side))
}
}

Expand Down Expand Up @@ -190,13 +196,10 @@ impl EntityBlockRange {

pub fn compare_column(&self, out: &mut AstPass<Pg>) {
match self {
EntityBlockRange::Mutable((_, is_upper_range)) => {
if *is_upper_range {
out.push_sql(" upper(block_range) ")
} else {
out.push_sql(" lower(block_range) ")
}
}
EntityBlockRange::Mutable((_, bound_side)) => match bound_side {
BoundSide::Lower => out.push_sql(" lower(block_range) "),
BoundSide::Upper => out.push_sql(" upper(block_range) "),
},
EntityBlockRange::Immutable(_) => out.push_sql(" block$ "),
}
}
Expand Down
24 changes: 15 additions & 9 deletions store/postgres/src/relational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use graph::prelude::{
QueryExecutionError, StoreError, StoreEvent, ValueType, BLOCK_NUMBER_MAX,
};

use crate::block_range::{BLOCK_COLUMN, BLOCK_RANGE_COLUMN};
use crate::block_range::{BoundSide, BLOCK_COLUMN, BLOCK_RANGE_COLUMN};
pub use crate::catalog::Catalog;
use crate::connection_pool::ForeignServer;
use crate::{catalog, deployment};
Expand Down Expand Up @@ -530,14 +530,20 @@ impl Layout {
et_map.insert(et.to_string(), Arc::new(et));
}
let mut entities: BTreeMap<BlockNumber, Vec<EntityWithType>> = BTreeMap::new();
let lower_vec = FindRangeQuery::new(&tables, causality_region, false, block_range.clone())
.get_results::<EntityDataExt>(conn)
.optional()?
.unwrap_or_default();
let upper_vec = FindRangeQuery::new(&tables, causality_region, true, block_range)
.get_results::<EntityDataExt>(conn)
.optional()?
.unwrap_or_default();
let lower_vec = FindRangeQuery::new(
&tables,
causality_region,
BoundSide::Lower,
block_range.clone(),
)
.get_results::<EntityDataExt>(conn)
.optional()?
.unwrap_or_default();
let upper_vec =
FindRangeQuery::new(&tables, causality_region, BoundSide::Upper, block_range)
.get_results::<EntityDataExt>(conn)
.optional()?
.unwrap_or_default();
let mut lower_iter = lower_vec.iter().fuse().peekable();
let mut upper_iter = upper_vec.iter().fuse().peekable();
let mut lower_now = lower_iter.next();
Expand Down
14 changes: 7 additions & 7 deletions store/postgres/src/relational_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use std::ops::Range;
use std::str::FromStr;
use std::string::ToString;

use crate::block_range::EntityBlockRange;
use crate::block_range::{BoundSide, EntityBlockRange};
use crate::relational::{
Column, ColumnType, Layout, SqlName, Table, BYTE_ARRAY_PREFIX_SIZE, PRIMARY_KEY_COLUMN,
STRING_PREFIX_SIZE,
Expand Down Expand Up @@ -2025,7 +2025,7 @@ impl<'a, Conn> RunQueryDsl<Conn> for FindQuery<'a> {}
pub struct FindRangeQuery<'a> {
tables: &'a Vec<&'a Table>,
causality_region: CausalityRegion,
is_upper_range: bool,
bound_side: BoundSide,
imm_range: EntityBlockRange,
mut_range: EntityBlockRange,
}
Expand All @@ -2034,15 +2034,15 @@ impl<'a> FindRangeQuery<'a> {
pub fn new(
tables: &'a Vec<&Table>,
causality_region: CausalityRegion,
is_upper_range: bool,
bound_side: BoundSide,
block_range: Range<BlockNumber>,
) -> Self {
let imm_range = EntityBlockRange::new(true, block_range.clone(), false);
let mut_range = EntityBlockRange::new(false, block_range, is_upper_range);
let imm_range = EntityBlockRange::new(true, block_range.clone(), bound_side);
let mut_range = EntityBlockRange::new(false, block_range, bound_side);
Self {
tables,
causality_region,
is_upper_range,
bound_side,
imm_range,
mut_range,
}
Expand All @@ -2056,7 +2056,7 @@ impl<'a> QueryFragment<Pg> for FindRangeQuery<'a> {

for table in self.tables.iter() {
// the immutable entities don't have upper range and also can't be modified or deleted
if !(self.is_upper_range && table.immutable) {
if matches!(self.bound_side, BoundSide::Lower) || !table.immutable {
if first {
first = false;
} else {
Expand Down

0 comments on commit 70d511a

Please sign in to comment.