Skip to content

Commit 841bf7d

Browse files
author
Zoran Cvetkov
committed
comments and minor changes
1 parent a213d3a commit 841bf7d

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

store/postgres/src/block_range.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,12 @@ impl EntityBlockRange {
202202

203203
pub fn compare_column(&self, out: &mut AstPass<Pg>) {
204204
match self {
205-
EntityBlockRange::Mutable((_, bound_side)) => match bound_side {
206-
BoundSide::Lower => out.push_sql(" lower(block_range) "),
207-
BoundSide::Upper => out.push_sql(" upper(block_range) "),
208-
},
205+
EntityBlockRange::Mutable((_, BoundSide::Lower)) => {
206+
out.push_sql(" lower(block_range) ")
207+
}
208+
EntityBlockRange::Mutable((_, BoundSide::Upper)) => {
209+
out.push_sql(" upper(block_range) ")
210+
}
209211
EntityBlockRange::Immutable(_) => out.push_sql(" block$ "),
210212
}
211213
}

store/postgres/src/relational.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,10 @@ impl Layout {
529529
}
530530
let mut entities: BTreeMap<BlockNumber, Vec<EntityWithType>> = BTreeMap::new();
531531

532-
// collect all entities that have their 'lower(block_range)' attribute in the
532+
// Collect all entities that have their 'lower(block_range)' attribute in the
533533
// interval of blocks defined by the variable block_range. For the immutable
534534
// entities the respective attribute is 'block$'.
535+
// Here are all entities that are created or modified in the block_range.
535536
let lower_vec = FindRangeQuery::new(
536537
&tables,
537538
causality_region,
@@ -541,9 +542,13 @@ impl Layout {
541542
.get_results::<EntityDataExt>(conn)
542543
.optional()?
543544
.unwrap_or_default();
544-
// collect all entities that have their 'upper(block_range)' attribute in the
545+
// Collect all entities that have their 'upper(block_range)' attribute in the
545546
// interval of blocks defined by the variable block_range. For the immutable
546547
// entities no entries are returned.
548+
// Here are all entities that are modified or deleted in the block_range,
549+
// but will have the previous versions, i.e. in the case of an update, it's
550+
// the version before the update, and lower_vec will have a corresponding
551+
// entry with the new version.
547552
let upper_vec =
548553
FindRangeQuery::new(&tables, causality_region, BoundSide::Upper, block_range)
549554
.get_results::<EntityDataExt>(conn)
@@ -553,13 +558,11 @@ impl Layout {
553558
let mut upper_iter = upper_vec.iter().fuse().peekable();
554559
let mut lower_now = lower_iter.next();
555560
let mut upper_now = upper_iter.next();
556-
let mut lower = lower_now.unwrap_or(&EntityDataExt::default()).clone();
557-
let mut upper = upper_now.unwrap_or(&EntityDataExt::default()).clone();
558561
// A closure to convert the entity data from the database into entity operation.
559-
let transform = |ede: EntityDataExt,
562+
let transform = |ede: &EntityDataExt,
560563
entity_op: EntitySubgraphOperation|
561564
-> Result<(EntityWithType, BlockNumber), StoreError> {
562-
let e = EntityData::new(ede.entity, ede.data);
565+
let e = EntityData::new(ede.entity.clone(), ede.data.clone());
563566
let block = ede.block_number;
564567
let entity_type = e.entity_type(&self.input_schema);
565568
let entity = e.deserialize_with_layout::<Entity>(self, None)?;
@@ -573,62 +576,57 @@ impl Layout {
573576
Ok((ewt, block))
574577
};
575578

576-
// The algorithm advances simultaneously entities from both lower_vec and upper_vec and tries
577-
// to match entities that have entries in both vectors for a particular block. The match is
578-
// successfull if an entry in one array has same values for the number of the block, entity
579-
// name and the entity id. The comparison operation over the EntityDataExt fullfils that check.
580-
// In addition to that, it also helps to order the elements so the algorith can detect if one
581-
// side of the range exists and the other is missing. That way a creation and deletion are
582-
// deduced. For immutable entities the entries in upper_vec are missing hence they are considered
579+
// The algorithm is a similar to merge sort algorithm and it relays on the fact that both vectors
580+
// are ordered by (block_number, entity_type, entity_id). It advances simultaneously entities from
581+
// both lower_vec and upper_vec and tries to match entities that have entries in both vectors for
582+
// a particular block. The match is successful if an entry in one array has the same values in the
583+
// other one for the number of the block, entity type and the entity id. The comparison operation
584+
// over the EntityDataExt implements that check. If there is a match it’s a modification operation,
585+
// since both sides of a range are present for that block, entity type and id. If one side of the
586+
// range exists and the other is missing it is a creation or deletion depending on which side is
587+
// present. For immutable entities the entries in upper_vec are missing, hence they are considered
583588
// having a lower bound at particular block and upper bound at infinity.
584589
while lower_now.is_some() || upper_now.is_some() {
585-
let (ewt, block) = match (lower_now.is_some(), upper_now.is_some()) {
586-
(true, true) => {
590+
let (ewt, block) = match (lower_now, upper_now) {
591+
(Some(lower), Some(upper)) => {
587592
match lower.cmp(&upper) {
588593
std::cmp::Ordering::Greater => {
589594
// we have upper bound at this block, but no lower bounds at the same block so it's deletion
590595
let (ewt, block) = transform(upper, EntitySubgraphOperation::Delete)?;
591596
// advance upper_vec pointer
592597
upper_now = upper_iter.next();
593-
upper = upper_now.unwrap_or(&EntityDataExt::default()).clone();
594598
(ewt, block)
595599
}
596600
std::cmp::Ordering::Less => {
597601
// we have lower bound at this block but no upper bound at the same block so its creation
598602
let (ewt, block) = transform(lower, EntitySubgraphOperation::Create)?;
599603
// advance lower_vec pointer
600604
lower_now = lower_iter.next();
601-
lower = lower_now.unwrap_or(&EntityDataExt::default()).clone();
602605
(ewt, block)
603606
}
604607
std::cmp::Ordering::Equal => {
605608
let (ewt, block) = transform(lower, EntitySubgraphOperation::Modify)?;
606609
// advance both lower_vec and upper_vec pointers
607610
lower_now = lower_iter.next();
608-
lower = lower_now.unwrap_or(&EntityDataExt::default()).clone();
609611
upper_now = upper_iter.next();
610-
upper = upper_now.unwrap_or(&EntityDataExt::default()).clone();
611612
(ewt, block)
612613
}
613614
}
614615
}
615-
(true, false) => {
616+
(Some(lower), None) => {
616617
// we have lower bound at this block but no upper bound at the same block so its creation
617618
let (ewt, block) = transform(lower, EntitySubgraphOperation::Create)?;
618619
// advance lower_vec pointer
619620
lower_now = lower_iter.next();
620-
lower = lower_now.unwrap_or(&EntityDataExt::default()).clone();
621621
(ewt, block)
622622
}
623-
(false, have_upper) => {
624-
// we have upper bound at this block, but no lower bounds at all so it's deletion
625-
assert!(have_upper);
623+
(None, Some(upper)) => {
626624
let (ewt, block) = transform(upper, EntitySubgraphOperation::Delete)?;
627625
// advance upper_vec pointer
628626
upper_now = upper_iter.next();
629-
upper = upper_now.unwrap_or(&EntityDataExt::default()).clone();
630627
(ewt, block)
631628
}
629+
_ => panic!("Imposible case to happen"),
632630
};
633631

634632
match entities.get_mut(&block) {

0 commit comments

Comments
 (0)