Skip to content

Commit ed1472c

Browse files
committed
graph, store: Simplify adding modifications for immutable entities
1 parent 8eb46ac commit ed1472c

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

graph/src/components/store/write.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Data structures and helpers for writing subgraph changes to the store
2-
use std::collections::HashSet;
2+
use std::{collections::HashSet, sync::Arc};
33

44
use crate::{
55
blockchain::{block_stream::FirehoseCursor, BlockPtr},
@@ -9,6 +9,7 @@ use crate::{
99
data::{subgraph::schema::SubgraphError, value::Word},
1010
data_source::CausalityRegion,
1111
prelude::DeploymentHash,
12+
schema::InputSchema,
1213
util::cache_weight::CacheWeight,
1314
};
1415

@@ -144,13 +145,6 @@ impl EntityModification {
144145
}
145146
}
146147

147-
fn key(&self) -> &EntityKey {
148-
use EntityModification::*;
149-
match self {
150-
Insert { key, .. } | Overwrite { key, .. } | Remove { key, .. } => key,
151-
}
152-
}
153-
154148
fn entity_count_change(&self) -> i32 {
155149
match self {
156150
EntityModification::Insert { end: None, .. } => 1,
@@ -299,13 +293,16 @@ pub struct RowGroup {
299293
/// then `rows[i].block() <= rows[j].block()`. Several methods on this
300294
/// struct rely on the fact that this ordering is observed.
301295
rows: Vec<EntityModification>,
296+
297+
immutable: bool,
302298
}
303299

304300
impl RowGroup {
305-
pub fn new(entity_type: EntityType) -> Self {
301+
pub fn new(entity_type: EntityType, immutable: bool) -> Self {
306302
Self {
307303
entity_type,
308304
rows: Vec::new(),
305+
immutable,
309306
}
310307
}
311308

@@ -394,6 +391,22 @@ impl RowGroup {
394391
/// Append `row` to `self.rows` by combining it with a previously
395392
/// existing row, if that is possible
396393
fn append_row(&mut self, row: EntityModification) -> Result<(), StoreError> {
394+
if self.immutable {
395+
match row {
396+
EntityModification::Insert { .. } => {
397+
self.rows.push(row);
398+
}
399+
EntityModification::Overwrite { .. } | EntityModification::Remove { .. } => {
400+
return Err(constraint_violation!(
401+
"immutable entity type {} only allows inserts, not {:?}",
402+
self.entity_type,
403+
row
404+
));
405+
}
406+
}
407+
return Ok(());
408+
}
409+
397410
if let Some(prev_row) = self.prev_row_mut(row.id()) {
398411
use EntityModification::*;
399412

@@ -510,12 +523,16 @@ impl<'a> Iterator for ClampsByBlockIterator<'a> {
510523
/// A list of entity changes with one group per entity type
511524
#[derive(Debug)]
512525
pub struct RowGroups {
526+
schema: Arc<InputSchema>,
513527
pub groups: Vec<RowGroup>,
514528
}
515529

516530
impl RowGroups {
517-
fn new() -> Self {
518-
Self { groups: Vec::new() }
531+
fn new(schema: Arc<InputSchema>) -> Self {
532+
Self {
533+
schema,
534+
groups: Vec::new(),
535+
}
519536
}
520537

521538
fn group(&self, entity_type: &EntityType) -> Option<&RowGroup> {
@@ -534,7 +551,9 @@ impl RowGroups {
534551
match pos {
535552
Some(pos) => &mut self.groups[pos],
536553
None => {
537-
self.groups.push(RowGroup::new(entity_type.clone()));
554+
let immutable = self.schema.is_immutable(entity_type);
555+
self.groups
556+
.push(RowGroup::new(entity_type.clone(), immutable));
538557
// unwrap: we just pushed an entry
539558
self.groups.last_mut().unwrap()
540559
}
@@ -612,6 +631,7 @@ pub struct Batch {
612631

613632
impl Batch {
614633
pub fn new(
634+
schema: Arc<InputSchema>,
615635
block_ptr: BlockPtr,
616636
firehose_cursor: FirehoseCursor,
617637
mut raw_mods: Vec<EntityModification>,
@@ -631,7 +651,7 @@ impl Batch {
631651
EntityModification::Remove { .. } => 0,
632652
});
633653

634-
let mut mods = RowGroups::new();
654+
let mut mods = RowGroups::new(schema);
635655

636656
for m in raw_mods {
637657
mods.group_entry(&m.key().entity_type).push(m, block)?;
@@ -905,6 +925,7 @@ mod test {
905925
let group = RowGroup {
906926
entity_type: EntityType::new("Entry".to_string()),
907927
rows,
928+
immutable: false,
908929
};
909930
let act = group
910931
.clamps_by_block()
@@ -1003,7 +1024,7 @@ mod test {
10031024
impl Group {
10041025
fn new() -> Self {
10051026
Self {
1006-
group: RowGroup::new(ENTITY_TYPE.clone()),
1027+
group: RowGroup::new(ENTITY_TYPE.clone(), false),
10071028
}
10081029
}
10091030

store/postgres/src/writable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,7 @@ impl WritableStoreTrait for WritableStore {
14971497
processed_data_sources: Vec<StoredDynamicDataSource>,
14981498
) -> Result<(), StoreError> {
14991499
let batch = Batch::new(
1500+
self.store.input_schema.cheap_clone(),
15001501
block_ptr_to.clone(),
15011502
firehose_cursor.clone(),
15021503
mods,

store/test-store/tests/postgres/relational_bytes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn row_group_update(
8484
block: BlockNumber,
8585
data: impl IntoIterator<Item = (EntityKey, Entity)>,
8686
) -> RowGroup {
87-
let mut group = RowGroup::new(entity_type.clone());
87+
let mut group = RowGroup::new(entity_type.clone(), false);
8888
for (key, data) in data {
8989
group
9090
.push(EntityModification::overwrite(key, data, block), block)
@@ -98,7 +98,7 @@ pub fn row_group_insert(
9898
block: BlockNumber,
9999
data: impl IntoIterator<Item = (EntityKey, Entity)>,
100100
) -> RowGroup {
101-
let mut group = RowGroup::new(entity_type.clone());
101+
let mut group = RowGroup::new(entity_type.clone(), false);
102102
for (key, data) in data {
103103
group
104104
.push(EntityModification::insert(key, data, block), block)
@@ -112,7 +112,7 @@ pub fn row_group_delete(
112112
block: BlockNumber,
113113
data: impl IntoIterator<Item = EntityKey>,
114114
) -> RowGroup {
115-
let mut group = RowGroup::new(entity_type.clone());
115+
let mut group = RowGroup::new(entity_type.clone(), false);
116116
for key in data {
117117
group
118118
.push(EntityModification::remove(key, block), block)

0 commit comments

Comments
 (0)