Skip to content

Commit a50fcbc

Browse files
author
Zoran Cvetkov
committed
change entity structure
1 parent fda7d7c commit a50fcbc

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

graph/src/components/store/entity_cache.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,19 +402,19 @@ impl EntityCache {
402402

403403
let mut entity = entity;
404404
if self.strict_vid_order && is_object {
405-
// The next VID is based on a block number and a sequence within the block
406-
let vid = ((block as i64) << 32) + self.vid_seq as i64;
407-
self.vid_seq += 1;
408-
let old_vid = entity.set_vid(vid).expect("the vid should be set");
409405
// Make sure that there was no VID previously set for this entity.
410-
if let Some(ovid) = old_vid {
406+
if let Some(ovid) = entity.vid() {
411407
bail!(
412408
"VID: {} of entity: {} with ID: {} was already present when set in EntityCache",
413409
ovid,
414410
key.entity_type,
415411
entity.id()
416412
);
417413
}
414+
// The next VID is based on a block number and a sequence within the block
415+
let vid = ((block as i64) << 32) + self.vid_seq as i64;
416+
self.vid_seq += 1;
417+
entity.set_vid(vid);
418418
}
419419

420420
self.entity_op(key.clone(), EntityOp::Update(entity));

graph/src/data/store/mod.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -741,15 +741,18 @@ lazy_static! {
741741

742742
/// An entity is represented as a map of attribute names to values.
743743
#[derive(Clone, CacheWeight, PartialEq, Eq, Serialize)]
744-
pub struct Entity(Object<Value>);
744+
pub struct Entity {
745+
o: Object<Value>,
746+
vid: Option<i64>,
747+
}
745748

746749
impl<'a> IntoIterator for &'a Entity {
747750
type Item = (Word, Value);
748751

749752
type IntoIter = intern::ObjectOwningIter<Value>;
750753

751754
fn into_iter(self) -> Self::IntoIter {
752-
self.0.clone().into_iter()
755+
self.o.clone().into_iter()
753756
}
754757
}
755758

@@ -853,7 +856,7 @@ impl Entity {
853856
obj.insert(key, value)
854857
.map_err(|e| EntityValidationError::UnknownKey(e.not_interned()))?;
855858
}
856-
let entity = Entity(obj);
859+
let entity = Entity { o: obj, vid: None };
857860
entity.check_id()?;
858861
Ok(entity)
859862
}
@@ -868,23 +871,23 @@ impl Entity {
868871
obj.insert(key, value)
869872
.map_err(|e| anyhow!("unknown attribute {}", e.not_interned()))?;
870873
}
871-
let entity = Entity(obj);
874+
let entity = Entity { o: obj, vid: None };
872875
entity.check_id()?;
873876
Ok(entity)
874877
}
875878

876879
pub fn get(&self, key: &str) -> Option<&Value> {
877-
self.0.get(key)
880+
self.o.get(key)
878881
}
879882

880883
pub fn contains_key(&self, key: &str) -> bool {
881-
self.0.contains_key(key)
884+
self.o.contains_key(key)
882885
}
883886

884887
// This collects the entity into an ordered vector so that it can be iterated deterministically.
885888
pub fn sorted(self) -> Vec<(Word, Value)> {
886889
let mut v: Vec<_> = self
887-
.0
890+
.o
888891
.into_iter()
889892
.filter(|(k, _)| !k.eq(VID_FIELD))
890893
.collect();
@@ -893,15 +896,15 @@ impl Entity {
893896
}
894897

895898
pub fn sorted_ref(&self) -> Vec<(&str, &Value)> {
896-
let mut v: Vec<_> = self.0.iter().filter(|(k, _)| !k.eq(&VID_FIELD)).collect();
899+
let mut v: Vec<_> = self.o.iter().filter(|(k, _)| !k.eq(&VID_FIELD)).collect();
897900
v.sort_by(|(k1, _), (k2, _)| k1.cmp(k2));
898901
v
899902
}
900903

901904
fn check_id(&self) -> Result<(), EntityValidationError> {
902905
match self.get("id") {
903906
None => Err(EntityValidationError::MissingIDAttribute {
904-
entity: format!("{:?}", self.0),
907+
entity: format!("{:?}", self),
905908
}),
906909
Some(Value::String(_)) | Some(Value::Bytes(_)) | Some(Value::Int8(_)) => Ok(()),
907910
_ => Err(EntityValidationError::UnsupportedTypeForIDAttribute),
@@ -918,31 +921,29 @@ impl Entity {
918921

919922
/// Return the VID of this entity and if its missing or of a type different than
920923
/// i64 it panics.
921-
pub fn vid(&self) -> i64 {
922-
self.get(VID_FIELD)
923-
.expect("the vid must be set")
924-
.as_int8()
925-
.expect("the vid must be set to a valid value")
924+
pub fn vid(&self) -> Option<i64> {
925+
self.vid
926926
}
927927

928928
/// Sets the VID of the entity. The previous one is returned.
929-
pub fn set_vid(&mut self, value: i64) -> Result<Option<Value>, InternError> {
930-
self.0.insert(VID_FIELD, value.into())
929+
pub fn set_vid(&mut self, value: i64) -> Option<i64> {
930+
let old_val = self.vid;
931+
self.vid = Some(value);
932+
old_val
931933
}
932934

933935
/// Clone entity and remove the VID.
934936
pub fn clone_no_vid(&self) -> Entity {
935937
let mut c = self.clone();
936-
c.0.remove(VID_FIELD);
938+
c.vid = None;
937939
c
938940
}
939941

940942
/// Sets the VID if it's not already set. Should be used only for tests.
941943
#[cfg(debug_assertions)]
942944
pub fn set_vid_if_empty(&mut self) {
943-
let vid = self.get(VID_FIELD);
944-
if vid.is_none() {
945-
let _ = self.set_vid(100).expect("the vid should be set");
945+
if self.vid.is_none() {
946+
self.vid = Some(100)
946947
}
947948
}
948949

@@ -952,7 +953,7 @@ impl Entity {
952953
/// If a key only exists on one entity, the value from that entity is chosen.
953954
/// If a key is set to `Value::Null` in `update`, the key/value pair is set to `Value::Null`.
954955
pub fn merge(&mut self, update: Entity) {
955-
self.0.merge(update.0);
956+
self.o.merge(update.o);
956957
}
957958

958959
/// Merges an entity update `update` into this entity, removing `Value::Null` values.
@@ -961,18 +962,18 @@ impl Entity {
961962
/// If a key only exists on one entity, the value from that entity is chosen.
962963
/// If a key is set to `Value::Null` in `update`, the key/value pair is removed.
963964
pub fn merge_remove_null_fields(&mut self, update: Entity) -> Result<(), InternError> {
964-
for (key, value) in update.0.into_iter() {
965+
for (key, value) in update.o.into_iter() {
965966
match value {
966-
Value::Null => self.0.remove(&key),
967-
_ => self.0.insert(&key, value)?,
967+
Value::Null => self.o.remove(&key),
968+
_ => self.o.insert(&key, value)?,
968969
};
969970
}
970971
Ok(())
971972
}
972973

973974
/// Remove all entries with value `Value::Null` from `self`
974975
pub fn remove_null_fields(&mut self) {
975-
self.0.retain(|_, value| !value.is_null())
976+
self.o.retain(|_, value| !value.is_null())
976977
}
977978

978979
/// Add the key/value pairs from `iter` to this entity. This is the same
@@ -984,7 +985,7 @@ impl Entity {
984985
iter: impl IntoIterator<Item = (impl AsRef<str>, Value)>,
985986
) -> Result<(), InternError> {
986987
for (key, value) in iter {
987-
self.0.insert(key, value)?;
988+
self.o.insert(key, value)?;
988989
}
989990
Ok(())
990991
}
@@ -1074,19 +1075,19 @@ impl Entity {
10741075
#[cfg(debug_assertions)]
10751076
impl Entity {
10761077
pub fn insert(&mut self, key: &str, value: Value) -> Result<Option<Value>, InternError> {
1077-
self.0.insert(key, value)
1078+
self.o.insert(key, value)
10781079
}
10791080

10801081
pub fn remove(&mut self, key: &str) -> Option<Value> {
1081-
self.0.remove(key)
1082+
self.o.remove(key)
10821083
}
10831084

10841085
pub fn set(
10851086
&mut self,
10861087
name: &str,
10871088
value: impl Into<Value>,
10881089
) -> Result<Option<Value>, InternError> {
1089-
self.0.insert(name, value.into())
1090+
self.o.insert(name, value.into())
10901091
}
10911092
}
10921093

@@ -1098,16 +1099,17 @@ impl<'a> From<&'a Entity> for Cow<'a, Entity> {
10981099

10991100
impl GasSizeOf for Entity {
11001101
fn gas_size_of(&self) -> Gas {
1101-
self.0.gas_size_of()
1102+
self.o.gas_size_of()
11021103
}
11031104
}
11041105

11051106
impl std::fmt::Debug for Entity {
11061107
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11071108
let mut ds = f.debug_struct("Entity");
1108-
for (k, v) in &self.0 {
1109+
for (k, v) in &self.o {
11091110
ds.field(k, v);
11101111
}
1112+
ds.field("VID", &self.vid);
11111113
ds.finish()
11121114
}
11131115
}

store/postgres/src/relational_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2258,7 +2258,7 @@ impl<'a> InsertRow<'a> {
22582258
let br_value = BlockRangeValue::new(table, row.block, row.end);
22592259
let causality_region = row.causality_region;
22602260
let vid = if table.object.has_vid_seq() {
2261-
row.entity.vid()
2261+
row.entity.vid().unwrap_or_default()
22622262
} else {
22632263
0
22642264
};

0 commit comments

Comments
 (0)