Skip to content

Commit 8eb46ac

Browse files
committed
graph: Move EntityOp to EntityCache
That's the only placed where it is needed
1 parent b76b170 commit 8eb46ac

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

graph/src/components/store/entity_cache.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ use std::sync::Arc;
66

77
use crate::cheap_clone::CheapClone;
88
use crate::components::store::write::EntityModification;
9-
use crate::components::store::{self as s, Entity, EntityKey, EntityOp, EntityOperation};
9+
use crate::components::store::{self as s, Entity, EntityKey, EntityOperation};
1010
use crate::data::store::IntoEntityIterator;
1111
use crate::prelude::ENV_VARS;
1212
use crate::schema::InputSchema;
13+
use crate::util::intern::Error as InternError;
1314
use crate::util::lfu_cache::{EvictStats, LfuCache};
1415

1516
use super::{BlockNumber, DerivedEntityQuery, EntityType, LoadRelatedRequest, StoreError};
@@ -22,6 +23,45 @@ pub enum GetScope {
2223
InBlock,
2324
}
2425

26+
/// A representation of entity operations that can be accumulated.
27+
#[derive(Debug, Clone)]
28+
enum EntityOp {
29+
Remove,
30+
Update(Entity),
31+
Overwrite(Entity),
32+
}
33+
34+
impl EntityOp {
35+
fn apply_to(self, entity: &mut Option<Cow<Entity>>) -> Result<(), InternError> {
36+
use EntityOp::*;
37+
match (self, entity) {
38+
(Remove, e @ _) => *e = None,
39+
(Overwrite(new), e @ _) | (Update(new), e @ None) => *e = Some(Cow::Owned(new)),
40+
(Update(updates), Some(entity)) => entity.to_mut().merge_remove_null_fields(updates)?,
41+
}
42+
Ok(())
43+
}
44+
45+
fn accumulate(&mut self, next: EntityOp) {
46+
use EntityOp::*;
47+
let update = match next {
48+
// Remove and Overwrite ignore the current value.
49+
Remove | Overwrite(_) => {
50+
*self = next;
51+
return;
52+
}
53+
Update(update) => update,
54+
};
55+
56+
// We have an update, apply it.
57+
match self {
58+
// This is how `Overwrite` is constructed, by accumulating `Update` onto `Remove`.
59+
Remove => *self = Overwrite(update),
60+
Update(current) | Overwrite(current) => current.merge(update),
61+
}
62+
}
63+
}
64+
2565
/// A cache for entities from the store that provides the basic functionality
2666
/// needed for the store interactions in the host exports. This struct tracks
2767
/// how entities are modified, and caches all entities looked up from the

graph/src/components/store/mod.rs

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use futures::stream::poll_fn;
1616
use futures::{Async, Poll, Stream};
1717
use graphql_parser::schema as s;
1818
use serde::{Deserialize, Serialize};
19-
use std::borrow::{Borrow, Cow};
19+
use std::borrow::Borrow;
2020
use std::collections::btree_map::Entry;
2121
use std::collections::{BTreeMap, BTreeSet, HashSet};
2222
use std::fmt::Display;
@@ -32,7 +32,7 @@ use crate::data::store::*;
3232
use crate::data::value::Word;
3333
use crate::data_source::CausalityRegion;
3434
use crate::schema::InputSchema;
35-
use crate::util::intern::{self, Error as InternError};
35+
use crate::util::intern;
3636
use crate::{constraint_violation, prelude::*};
3737

3838
/// The type name of an entity. This is the string that is used in the
@@ -1003,45 +1003,6 @@ impl Display for DeploymentLocator {
10031003
// connection checkouts
10041004
pub type PoolWaitStats = Arc<RwLock<MovingStats>>;
10051005

1006-
/// A representation of entity operations that can be accumulated.
1007-
#[derive(Debug, Clone)]
1008-
enum EntityOp {
1009-
Remove,
1010-
Update(Entity),
1011-
Overwrite(Entity),
1012-
}
1013-
1014-
impl EntityOp {
1015-
fn apply_to(self, entity: &mut Option<Cow<Entity>>) -> Result<(), InternError> {
1016-
use EntityOp::*;
1017-
match (self, entity) {
1018-
(Remove, e @ _) => *e = None,
1019-
(Overwrite(new), e @ _) | (Update(new), e @ None) => *e = Some(Cow::Owned(new)),
1020-
(Update(updates), Some(entity)) => entity.to_mut().merge_remove_null_fields(updates)?,
1021-
}
1022-
Ok(())
1023-
}
1024-
1025-
fn accumulate(&mut self, next: EntityOp) {
1026-
use EntityOp::*;
1027-
let update = match next {
1028-
// Remove and Overwrite ignore the current value.
1029-
Remove | Overwrite(_) => {
1030-
*self = next;
1031-
return;
1032-
}
1033-
Update(update) => update,
1034-
};
1035-
1036-
// We have an update, apply it.
1037-
match self {
1038-
// This is how `Overwrite` is constructed, by accumulating `Update` onto `Remove`.
1039-
Remove => *self = Overwrite(update),
1040-
Update(current) | Overwrite(current) => current.merge(update),
1041-
}
1042-
}
1043-
}
1044-
10451006
/// Determines which columns should be selected in a table.
10461007
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
10471008
pub enum AttributeNames {

0 commit comments

Comments
 (0)