Skip to content

Commit bd95fff

Browse files
author
Zoran Cvetkov
committed
Revert "Make VID element internal to the Entity (#5857)"
This reverts commit 6bbd0d2.
1 parent 5cf0ad5 commit bd95fff

File tree

3 files changed

+26
-114
lines changed

3 files changed

+26
-114
lines changed

graph/src/components/store/entity_cache.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,18 @@ impl EntityCache {
213213
// always creates it in a new style.
214214
debug_assert!(match scope {
215215
GetScope::Store => {
216-
entity == self.store.get(key).unwrap().map(Arc::new)
216+
// Release build will never call this function and hence it's OK
217+
// when that implementation is not correct.
218+
fn remove_vid(entity: Option<Arc<Entity>>) -> Option<Entity> {
219+
entity.map(|e| {
220+
#[allow(unused_mut)]
221+
let mut entity = (*e).clone();
222+
#[cfg(debug_assertions)]
223+
entity.remove("vid");
224+
entity
225+
})
226+
}
227+
remove_vid(entity.clone()) == remove_vid(self.store.get(key).unwrap().map(Arc::new))
217228
}
218229
GetScope::InBlock => true,
219230
});

graph/src/data/store/mod.rs

Lines changed: 14 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -740,17 +740,16 @@ lazy_static! {
740740
}
741741

742742
/// An entity is represented as a map of attribute names to values.
743-
#[derive(Clone, CacheWeight, Eq, Serialize)]
743+
#[derive(Clone, CacheWeight, PartialEq, Eq, Serialize)]
744744
pub struct Entity(Object<Value>);
745745

746746
impl<'a> IntoIterator for &'a Entity {
747-
type Item = (&'a str, &'a Value);
747+
type Item = (Word, Value);
748748

749-
type IntoIter =
750-
std::iter::Filter<intern::ObjectIter<'a, Value>, fn(&(&'a str, &'a Value)) -> bool>;
749+
type IntoIter = intern::ObjectOwningIter<Value>;
751750

752751
fn into_iter(self) -> Self::IntoIter {
753-
(&self.0).into_iter().filter(|(k, _)| *k != VID_FIELD)
752+
self.0.clone().into_iter()
754753
}
755754
}
756755

@@ -875,18 +874,10 @@ impl Entity {
875874
}
876875

877876
pub fn get(&self, key: &str) -> Option<&Value> {
878-
// VID field is private and not visible outside
879-
if key == VID_FIELD {
880-
return None;
881-
}
882877
self.0.get(key)
883878
}
884879

885880
pub fn contains_key(&self, key: &str) -> bool {
886-
// VID field is private and not visible outside
887-
if key == VID_FIELD {
888-
return false;
889-
}
890881
self.0.contains_key(key)
891882
}
892883

@@ -924,8 +915,7 @@ impl Entity {
924915
/// Return the VID of this entity and if its missing or of a type different than
925916
/// i64 it panics.
926917
pub fn vid(&self) -> i64 {
927-
self.0
928-
.get(VID_FIELD)
918+
self.get(VID_FIELD)
929919
.expect("the vid must be set")
930920
.as_int8()
931921
.expect("the vid must be set to a valid value")
@@ -936,6 +926,15 @@ impl Entity {
936926
self.0.insert(VID_FIELD, value.into())
937927
}
938928

929+
/// Sets the VID if it's not already set. Should be used only for tests.
930+
#[cfg(debug_assertions)]
931+
pub fn set_vid_if_empty(&mut self) {
932+
let vid = self.get(VID_FIELD);
933+
if vid.is_none() {
934+
let _ = self.set_vid(100).expect("the vid should be set");
935+
}
936+
}
937+
939938
/// Merges an entity update `update` into this entity.
940939
///
941940
/// If a key exists in both entities, the value from `update` is chosen.
@@ -1059,13 +1058,6 @@ impl Entity {
10591058
}
10601059
}
10611060

1062-
/// Checks equality of two entities while ignoring the VID fields
1063-
impl PartialEq for Entity {
1064-
fn eq(&self, other: &Self) -> bool {
1065-
self.0.eq_ignore_key(&other.0, VID_FIELD)
1066-
}
1067-
}
1068-
10691061
/// Convenience methods to modify individual attributes for tests.
10701062
/// Production code should not use/need this.
10711063
#[cfg(debug_assertions)]
@@ -1085,14 +1077,6 @@ impl Entity {
10851077
) -> Result<Option<Value>, InternError> {
10861078
self.0.insert(name, value.into())
10871079
}
1088-
1089-
/// Sets the VID if it's not already set. Should be used only for tests.
1090-
pub fn set_vid_if_empty(&mut self) {
1091-
let vid = self.0.get(VID_FIELD);
1092-
if vid.is_none() {
1093-
let _ = self.set_vid(100).expect("the vid should be set");
1094-
}
1095-
}
10961080
}
10971081

10981082
impl<'a> From<&'a Entity> for Cow<'a, Entity> {
@@ -1284,47 +1268,3 @@ fn fmt_debug() {
12841268
let bi = Value::BigInt(scalar::BigInt::from(-17i32));
12851269
assert_eq!("BigInt(-17)", format!("{:?}", bi));
12861270
}
1287-
1288-
#[test]
1289-
fn entity_hidden_vid() {
1290-
use crate::schema::InputSchema;
1291-
let subgraph_id = "oneInterfaceOneEntity";
1292-
let document = "type Thing @entity {id: ID!, name: String!}";
1293-
let schema = InputSchema::raw(document, subgraph_id);
1294-
1295-
let entity = entity! { schema => id: "1", name: "test", vid: 3i64 };
1296-
let debug_str = format!("{:?}", entity);
1297-
let entity_str = "Entity { id: String(\"1\"), name: String(\"test\"), vid: Int8(3) }";
1298-
assert_eq!(debug_str, entity_str);
1299-
1300-
// get returns nothing...
1301-
assert_eq!(entity.get(VID_FIELD), None);
1302-
assert_eq!(entity.contains_key(VID_FIELD), false);
1303-
// ...while vid is present
1304-
assert_eq!(entity.vid(), 3i64);
1305-
1306-
// into_iter() misses it too
1307-
let mut it = entity.into_iter();
1308-
assert_eq!(Some(("id", &Value::String("1".to_string()))), it.next());
1309-
assert_eq!(
1310-
Some(("name", &Value::String("test".to_string()))),
1311-
it.next()
1312-
);
1313-
assert_eq!(None, it.next());
1314-
1315-
let mut entity2 = entity! { schema => id: "1", name: "test", vid: 5i64 };
1316-
assert_eq!(entity2.vid(), 5i64);
1317-
// equal with different vid
1318-
assert_eq!(entity, entity2);
1319-
1320-
entity2.remove(VID_FIELD);
1321-
// equal if one has no vid
1322-
assert_eq!(entity, entity2);
1323-
let debug_str2 = format!("{:?}", entity2);
1324-
let entity_str2 = "Entity { id: String(\"1\"), name: String(\"test\") }";
1325-
assert_eq!(debug_str2, entity_str2);
1326-
1327-
// set again
1328-
_ = entity2.set_vid(7i64);
1329-
assert_eq!(entity2.vid(), 7i64);
1330-
}

graph/src/util/intern.rs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -308,45 +308,6 @@ impl<V> Object<V> {
308308
}
309309
}
310310

311-
impl<V: PartialEq> Object<V> {
312-
fn len_ignore_atom(&self, atom: &Atom) -> usize {
313-
// Because of tombstones and the ignored atom, we can't just return `self.entries.len()`.
314-
self.entries
315-
.iter()
316-
.filter(|entry| entry.key != TOMBSTONE_KEY && entry.key != *atom)
317-
.count()
318-
}
319-
320-
/// Check for equality while ignoring one particular element
321-
pub fn eq_ignore_key(&self, other: &Self, ignore_key: &str) -> bool {
322-
let ignore = self.pool.lookup(ignore_key);
323-
let len1 = if let Some(to_ignore) = ignore {
324-
self.len_ignore_atom(&to_ignore)
325-
} else {
326-
self.len()
327-
};
328-
let len2 = if let Some(to_ignore) = other.pool.lookup(ignore_key) {
329-
other.len_ignore_atom(&to_ignore)
330-
} else {
331-
other.len()
332-
};
333-
if len1 != len2 {
334-
return false;
335-
}
336-
337-
if self.same_pool(other) {
338-
self.entries
339-
.iter()
340-
.filter(|e| e.key != TOMBSTONE_KEY && ignore.map_or(true, |ig| e.key != ig))
341-
.all(|Entry { key, value }| other.get_by_atom(key).map_or(false, |o| o == value))
342-
} else {
343-
self.iter()
344-
.filter(|(key, _)| *key != ignore_key)
345-
.all(|(key, value)| other.get(key).map_or(false, |o| o == value))
346-
}
347-
}
348-
}
349-
350311
impl<V: NullValue> Object<V> {
351312
/// Remove `key` from the object and return the value that was
352313
/// associated with the `key`. The entry is actually not removed for

0 commit comments

Comments
 (0)