Skip to content

Commit a119ed6

Browse files
committed
all: Rename EntityChange to AssignmentChange and clean up its API
1 parent 9c69088 commit a119ed6

File tree

6 files changed

+63
-73
lines changed

6 files changed

+63
-73
lines changed

core/graphman/src/commands/deployment/reassign.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use anyhow::anyhow;
44
use graph::components::store::DeploymentLocator;
55
use graph::components::store::StoreEvent;
6-
use graph::prelude::EntityChange;
6+
use graph::prelude::AssignmentChange;
77
use graph::prelude::NodeId;
88
use graph_store_postgres::command_support::catalog;
99
use graph_store_postgres::command_support::catalog::Site;
@@ -74,7 +74,7 @@ pub fn reassign_deployment(
7474
let primary_conn = primary_pool.get().map_err(GraphmanError::from)?;
7575
let mut catalog_conn = catalog::Connection::new(primary_conn);
7676

77-
let changes: Vec<EntityChange> = match catalog_conn
77+
let changes: Vec<AssignmentChange> = match catalog_conn
7878
.assigned_node(&deployment.site)
7979
.map_err(GraphmanError::from)?
8080
{

core/src/subgraph/registrar.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,8 @@ where
157157
.subscribe()
158158
.map_err(|()| anyhow!("Entity change stream failed"))
159159
.map(|event| {
160-
let assignments = event
161-
.changes
162-
.iter()
163-
.map(|change| match change {
164-
EntityChange::Assignment {
165-
deployment,
166-
operation,
167-
} => (deployment.clone(), operation.clone()),
168-
})
169-
.collect::<Vec<_>>();
170-
stream::iter_ok(assignments)
160+
let changes: Vec<_> = event.changes.iter().cloned().map(AssignmentChange::into_parts).collect();
161+
stream::iter_ok(changes)
171162
})
172163
.flatten()
173164
.and_then(
@@ -178,7 +169,7 @@ where
178169
);
179170

180171
match operation {
181-
EntityChangeOperation::Set => {
172+
AssignmentOperation::Set => {
182173
store
183174
.assignment_status(&deployment)
184175
.map_err(|e| {
@@ -215,7 +206,7 @@ where
215206
}
216207
})
217208
}
218-
EntityChangeOperation::Removed => {
209+
AssignmentOperation::Removed => {
219210
// Send remove event without checking node ID.
220211
// If node ID does not match, then this is a no-op when handled in
221212
// assignment provider.

graph/src/components/store/mod.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -537,32 +537,42 @@ impl EntityQuery {
537537
}
538538
}
539539

540-
/// Operation types that lead to entity changes.
541-
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
540+
/// Operation types that lead to changes in assignments
541+
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
542542
#[serde(rename_all = "lowercase")]
543-
pub enum EntityChangeOperation {
544-
/// An entity was added or updated
543+
pub enum AssignmentOperation {
544+
/// An assignment was added or updated
545545
Set,
546-
/// An existing entity was removed.
546+
/// An assignment was removed.
547547
Removed,
548548
}
549549

550-
/// Entity change events emitted by [Store](trait.Store.html) implementations.
550+
/// Assignment change events emitted by [Store](trait.Store.html) implementations.
551551
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
552-
pub enum EntityChange {
553-
Assignment {
554-
deployment: DeploymentLocator,
555-
operation: EntityChangeOperation,
556-
},
552+
pub struct AssignmentChange {
553+
deployment: DeploymentLocator,
554+
operation: AssignmentOperation,
557555
}
558556

559-
impl EntityChange {
560-
pub fn for_assignment(deployment: DeploymentLocator, operation: EntityChangeOperation) -> Self {
561-
Self::Assignment {
557+
impl AssignmentChange {
558+
fn new(deployment: DeploymentLocator, operation: AssignmentOperation) -> Self {
559+
Self {
562560
deployment,
563561
operation,
564562
}
565563
}
564+
565+
pub fn set(deployment: DeploymentLocator) -> Self {
566+
Self::new(deployment, AssignmentOperation::Set)
567+
}
568+
569+
pub fn removed(deployment: DeploymentLocator) -> Self {
570+
Self::new(deployment, AssignmentOperation::Removed)
571+
}
572+
573+
pub fn into_parts(self) -> (DeploymentLocator, AssignmentOperation) {
574+
(self.deployment, self.operation)
575+
}
566576
}
567577

568578
#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -578,16 +588,16 @@ pub struct StoreEvent {
578588
// The tag is only there to make it easier to track StoreEvents in the
579589
// logs as they flow through the system
580590
pub tag: usize,
581-
pub changes: HashSet<EntityChange>,
591+
pub changes: HashSet<AssignmentChange>,
582592
}
583593

584594
impl StoreEvent {
585-
pub fn new(changes: Vec<EntityChange>) -> StoreEvent {
595+
pub fn new(changes: Vec<AssignmentChange>) -> StoreEvent {
586596
let changes = changes.into_iter().collect();
587597
StoreEvent::from_set(changes)
588598
}
589599

590-
fn from_set(changes: HashSet<EntityChange>) -> StoreEvent {
600+
fn from_set(changes: HashSet<AssignmentChange>) -> StoreEvent {
591601
static NEXT_TAG: AtomicUsize = AtomicUsize::new(0);
592602

593603
let tag = NEXT_TAG.fetch_add(1, Ordering::Relaxed);

graph/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ pub mod prelude {
127127
};
128128
pub use crate::components::server::subscription::SubscriptionServer;
129129
pub use crate::components::store::{
130-
write::EntityModification, AttributeNames, BlockNumber, CachedEthereumCall, ChainStore,
131-
Child, ChildMultiplicity, EntityCache, EntityChange, EntityChangeOperation,
130+
write::EntityModification, AssignmentChange, AssignmentOperation, AttributeNames,
131+
BlockNumber, CachedEthereumCall, ChainStore, Child, ChildMultiplicity, EntityCache,
132132
EntityCollection, EntityFilter, EntityLink, EntityOperation, EntityOrder,
133133
EntityOrderByChild, EntityOrderByChildInfo, EntityQuery, EntityRange, EntityWindow,
134134
EthereumCallCache, ParentLink, PartialBlockPtr, PoolWaitStats, QueryStore,

store/postgres/src/primary.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ use graph::{
3939
prelude::{
4040
anyhow,
4141
chrono::{DateTime, Utc},
42-
serde_json, DeploymentHash, EntityChange, EntityChangeOperation, NodeId, StoreError,
43-
SubgraphName, SubgraphVersionSwitchingMode,
42+
serde_json, AssignmentChange, DeploymentHash, NodeId, StoreError, SubgraphName,
43+
SubgraphVersionSwitchingMode,
4444
},
4545
};
4646
use graph::{
@@ -790,7 +790,7 @@ impl<'a> Connection<'a> {
790790

791791
/// Delete all assignments for deployments that are neither the current nor the
792792
/// pending version of a subgraph and return the deployment id's
793-
fn remove_unused_assignments(&mut self) -> Result<Vec<EntityChange>, StoreError> {
793+
fn remove_unused_assignments(&mut self) -> Result<Vec<AssignmentChange>, StoreError> {
794794
use deployment_schemas as ds;
795795
use subgraph as s;
796796
use subgraph_deployment_assignment as a;
@@ -827,12 +827,7 @@ impl<'a> Connection<'a> {
827827
.into_iter()
828828
.map(|(id, hash)| {
829829
DeploymentHash::new(hash)
830-
.map(|hash| {
831-
EntityChange::for_assignment(
832-
DeploymentLocator::new(id.into(), hash),
833-
EntityChangeOperation::Removed,
834-
)
835-
})
830+
.map(|hash| AssignmentChange::removed(DeploymentLocator::new(id.into(), hash)))
836831
.map_err(|id| {
837832
StoreError::ConstraintViolation(format!(
838833
"invalid id `{}` for deployment assignment",
@@ -851,7 +846,7 @@ impl<'a> Connection<'a> {
851846
pub fn promote_deployment(
852847
&mut self,
853848
id: &DeploymentHash,
854-
) -> Result<Vec<EntityChange>, StoreError> {
849+
) -> Result<Vec<AssignmentChange>, StoreError> {
855850
use subgraph as s;
856851
use subgraph_version as v;
857852

@@ -926,7 +921,7 @@ impl<'a> Connection<'a> {
926921
node_id: NodeId,
927922
mode: SubgraphVersionSwitchingMode,
928923
exists_and_synced: F,
929-
) -> Result<Vec<EntityChange>, StoreError>
924+
) -> Result<Vec<AssignmentChange>, StoreError>
930925
where
931926
F: Fn(&DeploymentHash) -> Result<bool, StoreError>,
932927
{
@@ -1034,13 +1029,16 @@ impl<'a> Connection<'a> {
10341029
// Clean up any assignments we might have displaced
10351030
let mut changes = self.remove_unused_assignments()?;
10361031
if new_assignment {
1037-
let change = EntityChange::for_assignment(site.into(), EntityChangeOperation::Set);
1032+
let change = AssignmentChange::set(site.into());
10381033
changes.push(change);
10391034
}
10401035
Ok(changes)
10411036
}
10421037

1043-
pub fn remove_subgraph(&mut self, name: SubgraphName) -> Result<Vec<EntityChange>, StoreError> {
1038+
pub fn remove_subgraph(
1039+
&mut self,
1040+
name: SubgraphName,
1041+
) -> Result<Vec<AssignmentChange>, StoreError> {
10441042
use subgraph as s;
10451043
use subgraph_version as v;
10461044

@@ -1062,7 +1060,7 @@ impl<'a> Connection<'a> {
10621060
}
10631061
}
10641062

1065-
pub fn pause_subgraph(&mut self, site: &Site) -> Result<Vec<EntityChange>, StoreError> {
1063+
pub fn pause_subgraph(&mut self, site: &Site) -> Result<Vec<AssignmentChange>, StoreError> {
10661064
use subgraph_deployment_assignment as a;
10671065

10681066
let conn = self.conn.as_mut();
@@ -1073,8 +1071,7 @@ impl<'a> Connection<'a> {
10731071
match updates {
10741072
0 => Err(StoreError::DeploymentNotFound(site.deployment.to_string())),
10751073
1 => {
1076-
let change =
1077-
EntityChange::for_assignment(site.into(), EntityChangeOperation::Removed);
1074+
let change = AssignmentChange::removed(site.into());
10781075
Ok(vec![change])
10791076
}
10801077
_ => {
@@ -1085,7 +1082,7 @@ impl<'a> Connection<'a> {
10851082
}
10861083
}
10871084

1088-
pub fn resume_subgraph(&mut self, site: &Site) -> Result<Vec<EntityChange>, StoreError> {
1085+
pub fn resume_subgraph(&mut self, site: &Site) -> Result<Vec<AssignmentChange>, StoreError> {
10891086
use subgraph_deployment_assignment as a;
10901087

10911088
let conn = self.conn.as_mut();
@@ -1096,7 +1093,7 @@ impl<'a> Connection<'a> {
10961093
match updates {
10971094
0 => Err(StoreError::DeploymentNotFound(site.deployment.to_string())),
10981095
1 => {
1099-
let change = EntityChange::for_assignment(site.into(), EntityChangeOperation::Set);
1096+
let change = AssignmentChange::set(site.into());
11001097
Ok(vec![change])
11011098
}
11021099
_ => {
@@ -1111,7 +1108,7 @@ impl<'a> Connection<'a> {
11111108
&mut self,
11121109
site: &Site,
11131110
node: &NodeId,
1114-
) -> Result<Vec<EntityChange>, StoreError> {
1111+
) -> Result<Vec<AssignmentChange>, StoreError> {
11151112
use subgraph_deployment_assignment as a;
11161113

11171114
let conn = self.conn.as_mut();
@@ -1121,7 +1118,7 @@ impl<'a> Connection<'a> {
11211118
match updates {
11221119
0 => Err(StoreError::DeploymentNotFound(site.deployment.to_string())),
11231120
1 => {
1124-
let change = EntityChange::for_assignment(site.into(), EntityChangeOperation::Set);
1121+
let change = AssignmentChange::set(site.into());
11251122
Ok(vec![change])
11261123
}
11271124
_ => {
@@ -1248,19 +1245,19 @@ impl<'a> Connection<'a> {
12481245
&mut self,
12491246
site: &Site,
12501247
node: &NodeId,
1251-
) -> Result<Vec<EntityChange>, StoreError> {
1248+
) -> Result<Vec<AssignmentChange>, StoreError> {
12521249
use subgraph_deployment_assignment as a;
12531250

12541251
let conn = self.conn.as_mut();
12551252
insert_into(a::table)
12561253
.values((a::id.eq(site.id), a::node_id.eq(node.as_str())))
12571254
.execute(conn)?;
12581255

1259-
let change = EntityChange::for_assignment(site.into(), EntityChangeOperation::Set);
1256+
let change = AssignmentChange::set(site.into());
12601257
Ok(vec![change])
12611258
}
12621259

1263-
pub fn unassign_subgraph(&mut self, site: &Site) -> Result<Vec<EntityChange>, StoreError> {
1260+
pub fn unassign_subgraph(&mut self, site: &Site) -> Result<Vec<AssignmentChange>, StoreError> {
12641261
use subgraph_deployment_assignment as a;
12651262

12661263
let conn = self.conn.as_mut();
@@ -1271,8 +1268,7 @@ impl<'a> Connection<'a> {
12711268
match delete_count {
12721269
0 => Ok(vec![]),
12731270
1 => {
1274-
let change =
1275-
EntityChange::for_assignment(site.into(), EntityChangeOperation::Removed);
1271+
let change = AssignmentChange::removed(site.into());
12761272
Ok(vec![change])
12771273
}
12781274
_ => {

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ use graph::{
1010
schema::{DeploymentCreate, SubgraphError},
1111
DeploymentFeatures,
1212
},
13+
prelude::AssignmentChange,
1314
prelude::BlockPtr,
14-
prelude::EntityChange,
15-
prelude::EntityChangeOperation,
1615
prelude::QueryStoreManager,
1716
prelude::StoreEvent,
1817
prelude::SubgraphManifest,
@@ -59,18 +58,12 @@ const SUBGRAPH_FEATURES_GQL: &str = "
5958
}
6059
";
6160

62-
fn assigned(deployment: &DeploymentLocator) -> EntityChange {
63-
EntityChange::Assignment {
64-
deployment: deployment.clone(),
65-
operation: EntityChangeOperation::Set,
66-
}
61+
fn assigned(deployment: &DeploymentLocator) -> AssignmentChange {
62+
AssignmentChange::set(deployment.clone())
6763
}
6864

69-
fn unassigned(deployment: &DeploymentLocator) -> EntityChange {
70-
EntityChange::Assignment {
71-
deployment: deployment.clone(),
72-
operation: EntityChangeOperation::Removed,
73-
}
65+
fn unassigned(deployment: &DeploymentLocator) -> AssignmentChange {
66+
AssignmentChange::removed(deployment.clone())
7467
}
7568

7669
fn get_version_info(store: &Store, subgraph_name: &str) -> VersionInfo {
@@ -163,7 +156,7 @@ fn create_subgraph() {
163156
store: &SubgraphStore,
164157
id: &str,
165158
mode: SubgraphVersionSwitchingMode,
166-
) -> (DeploymentLocator, HashSet<EntityChange>) {
159+
) -> (DeploymentLocator, HashSet<AssignmentChange>) {
167160
let name = SubgraphName::new(SUBGRAPH_NAME.to_string()).unwrap();
168161
let id = DeploymentHash::new(id.to_string()).unwrap();
169162
let schema = InputSchema::parse_latest(SUBGRAPH_GQL, id.clone()).unwrap();
@@ -203,7 +196,7 @@ fn create_subgraph() {
203196
(deployment, events)
204197
}
205198

206-
fn deploy_event(deployment: &DeploymentLocator) -> HashSet<EntityChange> {
199+
fn deploy_event(deployment: &DeploymentLocator) -> HashSet<AssignmentChange> {
207200
let mut changes = HashSet::new();
208201
changes.insert(assigned(deployment));
209202
changes

0 commit comments

Comments
 (0)