1
1
//! Data structures and helpers for writing subgraph changes to the store
2
- use std:: collections:: HashSet ;
2
+ use std:: { collections:: HashSet , sync :: Arc } ;
3
3
4
4
use crate :: {
5
5
blockchain:: { block_stream:: FirehoseCursor , BlockPtr } ,
@@ -9,6 +9,7 @@ use crate::{
9
9
data:: { subgraph:: schema:: SubgraphError , value:: Word } ,
10
10
data_source:: CausalityRegion ,
11
11
prelude:: DeploymentHash ,
12
+ schema:: InputSchema ,
12
13
util:: cache_weight:: CacheWeight ,
13
14
} ;
14
15
@@ -144,13 +145,6 @@ impl EntityModification {
144
145
}
145
146
}
146
147
147
- fn key ( & self ) -> & EntityKey {
148
- use EntityModification :: * ;
149
- match self {
150
- Insert { key, .. } | Overwrite { key, .. } | Remove { key, .. } => key,
151
- }
152
- }
153
-
154
148
fn entity_count_change ( & self ) -> i32 {
155
149
match self {
156
150
EntityModification :: Insert { end : None , .. } => 1 ,
@@ -299,13 +293,16 @@ pub struct RowGroup {
299
293
/// then `rows[i].block() <= rows[j].block()`. Several methods on this
300
294
/// struct rely on the fact that this ordering is observed.
301
295
rows : Vec < EntityModification > ,
296
+
297
+ immutable : bool ,
302
298
}
303
299
304
300
impl RowGroup {
305
- pub fn new ( entity_type : EntityType ) -> Self {
301
+ pub fn new ( entity_type : EntityType , immutable : bool ) -> Self {
306
302
Self {
307
303
entity_type,
308
304
rows : Vec :: new ( ) ,
305
+ immutable,
309
306
}
310
307
}
311
308
@@ -394,6 +391,22 @@ impl RowGroup {
394
391
/// Append `row` to `self.rows` by combining it with a previously
395
392
/// existing row, if that is possible
396
393
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
+
397
410
if let Some ( prev_row) = self . prev_row_mut ( row. id ( ) ) {
398
411
use EntityModification :: * ;
399
412
@@ -510,12 +523,16 @@ impl<'a> Iterator for ClampsByBlockIterator<'a> {
510
523
/// A list of entity changes with one group per entity type
511
524
#[ derive( Debug ) ]
512
525
pub struct RowGroups {
526
+ schema : Arc < InputSchema > ,
513
527
pub groups : Vec < RowGroup > ,
514
528
}
515
529
516
530
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
+ }
519
536
}
520
537
521
538
fn group ( & self , entity_type : & EntityType ) -> Option < & RowGroup > {
@@ -534,7 +551,9 @@ impl RowGroups {
534
551
match pos {
535
552
Some ( pos) => & mut self . groups [ pos] ,
536
553
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) ) ;
538
557
// unwrap: we just pushed an entry
539
558
self . groups . last_mut ( ) . unwrap ( )
540
559
}
@@ -612,6 +631,7 @@ pub struct Batch {
612
631
613
632
impl Batch {
614
633
pub fn new (
634
+ schema : Arc < InputSchema > ,
615
635
block_ptr : BlockPtr ,
616
636
firehose_cursor : FirehoseCursor ,
617
637
mut raw_mods : Vec < EntityModification > ,
@@ -631,7 +651,7 @@ impl Batch {
631
651
EntityModification :: Remove { .. } => 0 ,
632
652
} ) ;
633
653
634
- let mut mods = RowGroups :: new ( ) ;
654
+ let mut mods = RowGroups :: new ( schema ) ;
635
655
636
656
for m in raw_mods {
637
657
mods. group_entry ( & m. key ( ) . entity_type ) . push ( m, block) ?;
@@ -905,6 +925,7 @@ mod test {
905
925
let group = RowGroup {
906
926
entity_type : EntityType :: new ( "Entry" . to_string ( ) ) ,
907
927
rows,
928
+ immutable : false ,
908
929
} ;
909
930
let act = group
910
931
. clamps_by_block ( )
@@ -1003,7 +1024,7 @@ mod test {
1003
1024
impl Group {
1004
1025
fn new ( ) -> Self {
1005
1026
Self {
1006
- group : RowGroup :: new ( ENTITY_TYPE . clone ( ) ) ,
1027
+ group : RowGroup :: new ( ENTITY_TYPE . clone ( ) , false ) ,
1007
1028
}
1008
1029
}
1009
1030
0 commit comments