Skip to content

Commit 5910c02

Browse files
author
Zoran Cvetkov
committed
comments and minor changes
1 parent a213d3a commit 5910c02

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
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: 16 additions & 9 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)
@@ -573,13 +578,15 @@ impl Layout {
573578
Ok((ewt, block))
574579
};
575580

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
581+
// The algorithm is a similar to merge sort algorithm and it relays on the fact that both vectors
582+
// are ordered by (block_number, entity_type, entity_id). It advances simultaneously entities from
583+
// both lower_vec and upper_vec and tries to match entities that have entries in both vectors for
584+
// a particular block. The match is successful if an entry in one array has the same values in the
585+
// other one for the number of the block, entity type and the entity id. The comparison operation
586+
// over the EntityDataExt implements that check. If there is a match it’s a modification operation,
587+
// since both sides of a range are present for that block, entity type and id. If one side of the
588+
// range exists and the other is missing it is a creation or deletion depending on which side is
589+
// present. For immutable entities the entries in upper_vec are missing, hence they are considered
583590
// having a lower bound at particular block and upper bound at infinity.
584591
while lower_now.is_some() || upper_now.is_some() {
585592
let (ewt, block) = match (lower_now.is_some(), upper_now.is_some()) {

0 commit comments

Comments
 (0)