Skip to content

Commit d2d926b

Browse files
committed
Subgraph Composition: Rename and rework data structures
1 parent 9235212 commit d2d926b

File tree

11 files changed

+58
-54
lines changed

11 files changed

+58
-54
lines changed

graph/src/blockchain/block_stream.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl<C: Blockchain> TriggersAdapterWrapper<C> {
357357

358358
fn create_subgraph_trigger_from_entities(
359359
filter: &SubgraphFilter,
360-
entities: Vec<EntityWithType>,
360+
entities: Vec<EntitySourceOperation>,
361361
) -> Vec<subgraph::TriggerData> {
362362
entities
363363
.into_iter()
@@ -372,7 +372,7 @@ async fn create_subgraph_triggers<C: Blockchain>(
372372
logger: Logger,
373373
blocks: Vec<C::Block>,
374374
filter: &SubgraphFilter,
375-
mut entities: BTreeMap<BlockNumber, Vec<EntityWithType>>,
375+
mut entities: BTreeMap<BlockNumber, Vec<EntitySourceOperation>>,
376376
) -> Result<Vec<BlockWithTriggers<C>>, Error> {
377377
let logger_clone = logger.cheap_clone();
378378

@@ -428,15 +428,15 @@ async fn scan_subgraph_triggers<C: Blockchain>(
428428
}
429429

430430
#[derive(Debug, Clone, Eq, PartialEq)]
431-
pub enum EntitySubgraphOperation {
431+
pub enum EntityOperationKind {
432432
Create,
433433
Modify,
434434
Delete,
435435
}
436436

437437
#[derive(Debug, Clone, Eq, PartialEq)]
438-
pub struct EntityWithType {
439-
pub entity_op: EntitySubgraphOperation,
438+
pub struct EntitySourceOperation {
439+
pub entity_op: EntityOperationKind,
440440
pub entity_type: EntityType,
441441
pub entity: Entity,
442442
pub vid: i64,
@@ -448,7 +448,7 @@ async fn get_entities_for_range(
448448
schema: &InputSchema,
449449
from: BlockNumber,
450450
to: BlockNumber,
451-
) -> Result<BTreeMap<BlockNumber, Vec<EntityWithType>>, Error> {
451+
) -> Result<BTreeMap<BlockNumber, Vec<EntitySourceOperation>>, Error> {
452452
let entity_types: Result<Vec<EntityType>> = filter
453453
.entities
454454
.iter()

graph/src/components/store/traits.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use async_trait::async_trait;
66
use web3::types::{Address, H256};
77

88
use super::*;
9-
use crate::blockchain::block_stream::{EntityWithType, FirehoseCursor};
9+
use crate::blockchain::block_stream::{EntitySourceOperation, FirehoseCursor};
1010
use crate::blockchain::{BlockTime, ChainIdentifier, ExtendedBlockPtr};
1111
use crate::components::metrics::stopwatch::StopwatchMetrics;
1212
use crate::components::server::index_node::VersionInfo;
@@ -302,7 +302,7 @@ pub trait SourceableStore: Sync + Send + 'static {
302302
entity_types: Vec<EntityType>,
303303
causality_region: CausalityRegion,
304304
block_range: Range<BlockNumber>,
305-
) -> Result<BTreeMap<BlockNumber, Vec<EntityWithType>>, StoreError>;
305+
) -> Result<BTreeMap<BlockNumber, Vec<EntitySourceOperation>>, StoreError>;
306306

307307
fn input_schema(&self) -> InputSchema;
308308

@@ -318,7 +318,7 @@ impl<T: ?Sized + SourceableStore> SourceableStore for Arc<T> {
318318
entity_types: Vec<EntityType>,
319319
causality_region: CausalityRegion,
320320
block_range: Range<BlockNumber>,
321-
) -> Result<BTreeMap<BlockNumber, Vec<EntityWithType>>, StoreError> {
321+
) -> Result<BTreeMap<BlockNumber, Vec<EntitySourceOperation>>, StoreError> {
322322
(**self).get_range(entity_types, causality_region, block_range)
323323
}
324324

graph/src/data_source/common.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::blockchain::block_stream::EntityWithType;
1+
use crate::blockchain::block_stream::EntitySourceOperation;
22
use crate::prelude::{BlockPtr, Value};
33
use crate::{components::link_resolver::LinkResolver, data::value::Word, prelude::Link};
44
use anyhow::{anyhow, Context, Error};
@@ -193,7 +193,10 @@ impl CallDecl {
193193
})
194194
}
195195

196-
pub fn address_for_entity_handler(&self, entity: &EntityWithType) -> Result<H160, Error> {
196+
pub fn address_for_entity_handler(
197+
&self,
198+
entity: &EntitySourceOperation,
199+
) -> Result<H160, Error> {
197200
match &self.expr.address {
198201
// Static hex address - just return it directly
199202
CallArg::HexAddress(address) => Ok(*address),
@@ -227,7 +230,7 @@ impl CallDecl {
227230
/// Returns an error if argument count mismatches or if conversion fails.
228231
pub fn args_for_entity_handler(
229232
&self,
230-
entity: &EntityWithType,
233+
entity: &EntitySourceOperation,
231234
param_types: Vec<ParamType>,
232235
) -> Result<Vec<Token>, Error> {
233236
self.validate_entity_handler_args(&param_types)?;
@@ -260,7 +263,7 @@ impl CallDecl {
260263
&self,
261264
arg: &CallArg,
262265
expected_type: &ParamType,
263-
entity: &EntityWithType,
266+
entity: &EntitySourceOperation,
264267
) -> Result<Token, Error> {
265268
match arg {
266269
CallArg::HexAddress(address) => self.process_hex_address(*address, expected_type),
@@ -292,7 +295,7 @@ impl CallDecl {
292295
&self,
293296
name: &str,
294297
expected_type: &ParamType,
295-
entity: &EntityWithType,
298+
entity: &EntitySourceOperation,
296299
) -> Result<Token, Error> {
297300
let value = entity
298301
.entity
@@ -549,7 +552,7 @@ impl DeclaredCall {
549552
pub fn from_entity_trigger(
550553
mapping: &dyn FindMappingABI,
551554
call_decls: &CallDecls,
552-
entity: &EntityWithType,
555+
entity: &EntitySourceOperation,
553556
) -> Result<Vec<DeclaredCall>, anyhow::Error> {
554557
Self::create_calls(mapping, call_decls, |decl, function| {
555558
let param_types = function

graph/src/data_source/subgraph.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
blockchain::{block_stream::EntityWithType, Block, Blockchain},
2+
blockchain::{block_stream::EntitySourceOperation, Block, Blockchain},
33
components::{link_resolver::LinkResolver, store::BlockNumber},
44
data::{
55
subgraph::{calls_host_fn, SPEC_VERSION_1_3_0},
@@ -353,11 +353,11 @@ pub struct MappingEntityTrigger {
353353
#[derive(Clone, PartialEq, Eq)]
354354
pub struct TriggerData {
355355
pub source: DeploymentHash,
356-
pub entity: EntityWithType,
356+
pub entity: EntitySourceOperation,
357357
}
358358

359359
impl TriggerData {
360-
pub fn new(source: DeploymentHash, entity: EntityWithType) -> Self {
360+
pub fn new(source: DeploymentHash, entity: EntitySourceOperation) -> Self {
361361
Self { source, entity }
362362
}
363363

runtime/wasm/src/to_from/external.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ethabi;
22

3-
use graph::blockchain::block_stream::{EntitySubgraphOperation, EntityWithType};
3+
use graph::blockchain::block_stream::{EntityOperationKind, EntitySourceOperation};
44
use graph::data::store::scalar::Timestamp;
55
use graph::data::value::Word;
66
use graph::prelude::{BigDecimal, BigInt};
@@ -482,16 +482,16 @@ pub struct AscEntityTrigger {
482482
pub vid: i64,
483483
}
484484

485-
impl ToAscObj<AscEntityTrigger> for EntityWithType {
485+
impl ToAscObj<AscEntityTrigger> for EntitySourceOperation {
486486
fn to_asc_obj<H: AscHeap + ?Sized>(
487487
&self,
488488
heap: &mut H,
489489
gas: &GasCounter,
490490
) -> Result<AscEntityTrigger, HostExportError> {
491491
let entity_op = match self.entity_op {
492-
EntitySubgraphOperation::Create => AscSubgraphEntityOp::Create,
493-
EntitySubgraphOperation::Modify => AscSubgraphEntityOp::Modify,
494-
EntitySubgraphOperation::Delete => AscSubgraphEntityOp::Delete,
492+
EntityOperationKind::Create => AscSubgraphEntityOp::Create,
493+
EntityOperationKind::Modify => AscSubgraphEntityOp::Modify,
494+
EntityOperationKind::Delete => AscSubgraphEntityOp::Delete,
495495
};
496496

497497
Ok(AscEntityTrigger {

store/postgres/src/deployment_store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use diesel::pg::PgConnection;
44
use diesel::r2d2::{ConnectionManager, PooledConnection};
55
use diesel::{prelude::*, sql_query};
66
use graph::anyhow::Context;
7-
use graph::blockchain::block_stream::{EntityWithType, FirehoseCursor};
7+
use graph::blockchain::block_stream::{EntitySourceOperation, FirehoseCursor};
88
use graph::blockchain::BlockTime;
99
use graph::components::store::write::RowGroup;
1010
use graph::components::store::{
@@ -1069,7 +1069,7 @@ impl DeploymentStore {
10691069
entity_types: Vec<EntityType>,
10701070
causality_region: CausalityRegion,
10711071
block_range: Range<BlockNumber>,
1072-
) -> Result<BTreeMap<BlockNumber, Vec<EntityWithType>>, StoreError> {
1072+
) -> Result<BTreeMap<BlockNumber, Vec<EntitySourceOperation>>, StoreError> {
10731073
let mut conn = self.get_conn()?;
10741074
let layout = self.layout(&mut conn, site)?;
10751075
layout.find_range(&mut conn, entity_types, causality_region, block_range)

store/postgres/src/relational.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use diesel::{connection::SimpleConnection, Connection};
2828
use diesel::{
2929
debug_query, sql_query, OptionalExtension, PgConnection, QueryDsl, QueryResult, RunQueryDsl,
3030
};
31-
use graph::blockchain::block_stream::{EntitySubgraphOperation, EntityWithType};
31+
use graph::blockchain::block_stream::{EntityOperationKind, EntitySourceOperation};
3232
use graph::blockchain::BlockTime;
3333
use graph::cheap_clone::CheapClone;
3434
use graph::components::store::write::{RowGroup, WriteChunk};
@@ -549,12 +549,12 @@ impl Layout {
549549
entity_types: Vec<EntityType>,
550550
causality_region: CausalityRegion,
551551
block_range: Range<BlockNumber>,
552-
) -> Result<BTreeMap<BlockNumber, Vec<EntityWithType>>, StoreError> {
552+
) -> Result<BTreeMap<BlockNumber, Vec<EntitySourceOperation>>, StoreError> {
553553
let mut tables = vec![];
554554
for et in entity_types {
555555
tables.push(self.table_for_entity(&et)?.as_ref());
556556
}
557-
let mut entities: BTreeMap<BlockNumber, Vec<EntityWithType>> = BTreeMap::new();
557+
let mut entities: BTreeMap<BlockNumber, Vec<EntitySourceOperation>> = BTreeMap::new();
558558

559559
// Collect all entities that have their 'lower(block_range)' attribute in the
560560
// interval of blocks defined by the variable block_range. For the immutable
@@ -587,14 +587,14 @@ impl Layout {
587587
let mut upper_now = upper_iter.next();
588588
// A closure to convert the entity data from the database into entity operation.
589589
let transform = |ede: &EntityDataExt,
590-
entity_op: EntitySubgraphOperation|
591-
-> Result<(EntityWithType, BlockNumber), StoreError> {
590+
entity_op: EntityOperationKind|
591+
-> Result<(EntitySourceOperation, BlockNumber), StoreError> {
592592
let e = EntityData::new(ede.entity.clone(), ede.data.clone());
593593
let block = ede.block_number;
594594
let entity_type = e.entity_type(&self.input_schema);
595595
let entity = e.deserialize_with_layout::<Entity>(self, None)?;
596596
let vid = ede.vid;
597-
let ewt = EntityWithType {
597+
let ewt = EntitySourceOperation {
598598
entity_op,
599599
entity_type,
600600
entity,
@@ -619,20 +619,20 @@ impl Layout {
619619
match lower.cmp(&upper) {
620620
std::cmp::Ordering::Greater => {
621621
// we have upper bound at this block, but no lower bounds at the same block so it's deletion
622-
let (ewt, block) = transform(upper, EntitySubgraphOperation::Delete)?;
622+
let (ewt, block) = transform(upper, EntityOperationKind::Delete)?;
623623
// advance upper_vec pointer
624624
upper_now = upper_iter.next();
625625
(ewt, block)
626626
}
627627
std::cmp::Ordering::Less => {
628628
// we have lower bound at this block but no upper bound at the same block so its creation
629-
let (ewt, block) = transform(lower, EntitySubgraphOperation::Create)?;
629+
let (ewt, block) = transform(lower, EntityOperationKind::Create)?;
630630
// advance lower_vec pointer
631631
lower_now = lower_iter.next();
632632
(ewt, block)
633633
}
634634
std::cmp::Ordering::Equal => {
635-
let (ewt, block) = transform(lower, EntitySubgraphOperation::Modify)?;
635+
let (ewt, block) = transform(lower, EntityOperationKind::Modify)?;
636636
// advance both lower_vec and upper_vec pointers
637637
lower_now = lower_iter.next();
638638
upper_now = upper_iter.next();
@@ -642,13 +642,14 @@ impl Layout {
642642
}
643643
(Some(lower), None) => {
644644
// we have lower bound at this block but no upper bound at the same block so its creation
645-
let (ewt, block) = transform(lower, EntitySubgraphOperation::Create)?;
645+
let (ewt, block) = transform(lower, EntityOperationKind::Create)?;
646646
// advance lower_vec pointer
647647
lower_now = lower_iter.next();
648648
(ewt, block)
649649
}
650650
(None, Some(upper)) => {
651-
let (ewt, block) = transform(upper, EntitySubgraphOperation::Delete)?;
651+
// we have upper bound at this block, but no lower bounds at all so it's deletion
652+
let (ewt, block) = transform(upper, EntityOperationKind::Delete)?;
652653
// advance upper_vec pointer
653654
upper_now = upper_iter.next();
654655
(ewt, block)

store/postgres/src/writable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::Instant;
66
use std::{collections::BTreeMap, sync::Arc};
77

88
use async_trait::async_trait;
9-
use graph::blockchain::block_stream::{EntityWithType, FirehoseCursor};
9+
use graph::blockchain::block_stream::{EntitySourceOperation, FirehoseCursor};
1010
use graph::blockchain::BlockTime;
1111
use graph::components::store::{Batch, DeploymentCursorTracker, DerivedEntityQuery, ReadStore};
1212
use graph::constraint_violation;
@@ -1595,7 +1595,7 @@ impl store::SourceableStore for SourceableStore {
15951595
entity_types: Vec<EntityType>,
15961596
causality_region: CausalityRegion,
15971597
block_range: Range<BlockNumber>,
1598-
) -> Result<BTreeMap<BlockNumber, Vec<EntityWithType>>, StoreError> {
1598+
) -> Result<BTreeMap<BlockNumber, Vec<EntitySourceOperation>>, StoreError> {
15991599
self.store.get_range(
16001600
self.site.clone(),
16011601
entity_types,

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use graph::blockchain::block_stream::{EntityWithType, FirehoseCursor};
1+
use graph::blockchain::block_stream::{EntitySourceOperation, FirehoseCursor};
22
use graph::data::subgraph::schema::DeploymentCreate;
33
use graph::data::value::Word;
44
use graph::data_source::CausalityRegion;
@@ -341,13 +341,13 @@ fn restart() {
341341
fn read_range_test() {
342342
run_test(|store, writable, sourceable, deployment| async move {
343343
let result_entities = vec![
344-
r#"(1, [EntityWithType { entity_op: Create, entity_type: EntityType(Counter), entity: Entity { count: Int(2), id: String("1") }, vid: 1 }, EntityWithType { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(2), id: String("1") }, vid: 1 }])"#,
345-
r#"(2, [EntityWithType { entity_op: Modify, entity_type: EntityType(Counter), entity: Entity { count: Int(4), id: String("1") }, vid: 2 }, EntityWithType { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(4), id: String("2") }, vid: 2 }])"#,
346-
r#"(3, [EntityWithType { entity_op: Delete, entity_type: EntityType(Counter), entity: Entity { count: Int(4), id: String("1") }, vid: 2 }, EntityWithType { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(6), id: String("3") }, vid: 3 }])"#,
347-
r#"(4, [EntityWithType { entity_op: Create, entity_type: EntityType(Counter), entity: Entity { count: Int(8), id: String("1") }, vid: 3 }, EntityWithType { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(8), id: String("4") }, vid: 4 }])"#,
348-
r#"(5, [EntityWithType { entity_op: Delete, entity_type: EntityType(Counter), entity: Entity { count: Int(8), id: String("1") }, vid: 3 }, EntityWithType { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(10), id: String("5") }, vid: 5 }])"#,
349-
r#"(6, [EntityWithType { entity_op: Create, entity_type: EntityType(Counter), entity: Entity { count: Int(12), id: String("1") }, vid: 4 }])"#,
350-
r#"(7, [EntityWithType { entity_op: Delete, entity_type: EntityType(Counter), entity: Entity { count: Int(12), id: String("1") }, vid: 4 }])"#,
344+
r#"(1, [EntitySourceOperation { entity_op: Create, entity_type: EntityType(Counter), entity: Entity { count: Int(2), id: String("1") }, vid: 1 }, EntitySourceOperation { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(2), id: String("1") }, vid: 1 }])"#,
345+
r#"(2, [EntitySourceOperation { entity_op: Modify, entity_type: EntityType(Counter), entity: Entity { count: Int(4), id: String("1") }, vid: 2 }, EntitySourceOperation { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(4), id: String("2") }, vid: 2 }])"#,
346+
r#"(3, [EntitySourceOperation { entity_op: Delete, entity_type: EntityType(Counter), entity: Entity { count: Int(4), id: String("1") }, vid: 2 }, EntitySourceOperation { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(6), id: String("3") }, vid: 3 }])"#,
347+
r#"(4, [EntitySourceOperation { entity_op: Create, entity_type: EntityType(Counter), entity: Entity { count: Int(8), id: String("1") }, vid: 3 }, EntitySourceOperation { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(8), id: String("4") }, vid: 4 }])"#,
348+
r#"(5, [EntitySourceOperation { entity_op: Delete, entity_type: EntityType(Counter), entity: Entity { count: Int(8), id: String("1") }, vid: 3 }, EntitySourceOperation { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(10), id: String("5") }, vid: 5 }])"#,
349+
r#"(6, [EntitySourceOperation { entity_op: Create, entity_type: EntityType(Counter), entity: Entity { count: Int(12), id: String("1") }, vid: 4 }])"#,
350+
r#"(7, [EntitySourceOperation { entity_op: Delete, entity_type: EntityType(Counter), entity: Entity { count: Int(12), id: String("1") }, vid: 4 }])"#,
351351
];
352352
let subgraph_store = store.subgraph_store();
353353
writable.deployment_synced(block_pointer(0)).unwrap();
@@ -360,7 +360,7 @@ fn read_range_test() {
360360

361361
let br: Range<BlockNumber> = 0..18;
362362
let entity_types = vec![COUNTER_TYPE.clone(), COUNTER2_TYPE.clone()];
363-
let e: BTreeMap<i32, Vec<EntityWithType>> = sourceable
363+
let e: BTreeMap<i32, Vec<EntitySourceOperation>> = sourceable
364364
.get_range(entity_types.clone(), CausalityRegion::ONCHAIN, br.clone())
365365
.unwrap();
366366
assert_eq!(e.len(), 5);
@@ -374,7 +374,7 @@ fn read_range_test() {
374374
}
375375
writable.flush().await.unwrap();
376376
writable.deployment_synced(block_pointer(0)).unwrap();
377-
let e: BTreeMap<i32, Vec<EntityWithType>> = sourceable
377+
let e: BTreeMap<i32, Vec<EntitySourceOperation>> = sourceable
378378
.get_range(entity_types, CausalityRegion::ONCHAIN, br)
379379
.unwrap();
380380
assert_eq!(e.len(), 7);
@@ -399,7 +399,7 @@ fn read_immutable_only_range_test() {
399399
writable.deployment_synced(block_pointer(0)).unwrap();
400400
let br: Range<BlockNumber> = 0..18;
401401
let entity_types = vec![COUNTER2_TYPE.clone()];
402-
let e: BTreeMap<i32, Vec<EntityWithType>> = sourceable
402+
let e: BTreeMap<i32, Vec<EntitySourceOperation>> = sourceable
403403
.get_range(entity_types.clone(), CausalityRegion::ONCHAIN, br.clone())
404404
.unwrap();
405405
assert_eq!(e.len(), 4);

tests/src/fixture/ethereum.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::{
66
test_ptr, CommonChainConfig, MutexBlockStreamBuilder, NoopAdapterSelector,
77
NoopRuntimeAdapterBuilder, StaticBlockRefetcher, StaticStreamBuilder, Stores, TestChain,
88
};
9-
use graph::blockchain::block_stream::{EntitySubgraphOperation, EntityWithType};
9+
use graph::blockchain::block_stream::{EntityOperationKind, EntitySourceOperation};
1010
use graph::blockchain::client::ChainClient;
1111
use graph::blockchain::{BlockPtr, Trigger, TriggersAdapterSelector};
1212
use graph::cheap_clone::CheapClone;
@@ -167,10 +167,10 @@ pub fn push_test_subgraph_trigger(
167167
source: DeploymentHash,
168168
entity: Entity,
169169
entity_type: EntityType,
170-
entity_op: EntitySubgraphOperation,
170+
entity_op: EntityOperationKind,
171171
vid: i64,
172172
) {
173-
let entity = EntityWithType {
173+
let entity = EntitySourceOperation {
174174
entity: entity,
175175
entity_type: entity_type,
176176
entity_op: entity_op,

0 commit comments

Comments
 (0)