Skip to content

Commit 4ee88d4

Browse files
author
Zoran Cvetkov
committed
wrap entity with vids
1 parent b27502c commit 4ee88d4

File tree

6 files changed

+63
-32
lines changed

6 files changed

+63
-32
lines changed

graph/src/components/store/entity_cache.rs

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::Arc;
77
use crate::cheap_clone::CheapClone;
88
use crate::components::store::write::EntityModification;
99
use crate::components::store::{self as s, Entity, EntityOperation};
10-
use crate::data::store::{EntityValidationError, Id, IdType, IntoEntityIterator};
10+
use crate::data::store::{EntityV, EntityValidationError, Id, IdType, IntoEntityIterator};
1111
use crate::prelude::{CacheWeight, ENV_VARS};
1212
use crate::schema::{EntityKey, InputSchema};
1313
use crate::util::intern::Error as InternError;
@@ -33,8 +33,8 @@ pub enum GetScope {
3333
#[derive(Debug, Clone)]
3434
enum EntityOp {
3535
Remove,
36-
Update(Entity),
37-
Overwrite(Entity),
36+
Update(EntityV),
37+
Overwrite(EntityV),
3838
}
3939

4040
impl EntityOp {
@@ -45,10 +45,10 @@ impl EntityOp {
4545
use EntityOp::*;
4646
match (self, entity) {
4747
(Remove, _) => Ok(None),
48-
(Overwrite(new), _) | (Update(new), None) => Ok(Some(new)),
48+
(Overwrite(new), _) | (Update(new), None) => Ok(Some(new.e)),
4949
(Update(updates), Some(entity)) => {
5050
let mut e = entity.borrow().clone();
51-
e.merge_remove_null_fields(updates)?;
51+
e.merge_remove_null_fields(updates.e)?;
5252
Ok(Some(e))
5353
}
5454
}
@@ -69,7 +69,7 @@ impl EntityOp {
6969
match self {
7070
// This is how `Overwrite` is constructed, by accumulating `Update` onto `Remove`.
7171
Remove => *self = Overwrite(update),
72-
Update(current) | Overwrite(current) => current.merge(update),
72+
Update(current) | Overwrite(current) => current.e.merge(update.e),
7373
}
7474
}
7575
}
@@ -304,9 +304,9 @@ impl EntityCache {
304304
) -> Result<Option<Entity>, anyhow::Error> {
305305
match op {
306306
EntityOp::Update(entity) | EntityOp::Overwrite(entity)
307-
if query.matches(key, entity) =>
307+
if query.matches(key, &entity.e) =>
308308
{
309-
Ok(Some(entity.clone()))
309+
Ok(Some(entity.e.clone()))
310310
}
311311
EntityOp::Remove => Ok(None),
312312
_ => Ok(None),
@@ -400,19 +400,19 @@ impl EntityCache {
400400
// The next VID is based on a block number and a sequence within the block
401401
let vid = ((block as i64) << 32) + self.vid_seq as i64;
402402
self.vid_seq += 1;
403-
let mut entity = entity;
404-
let old_vid = entity.set_vid(vid).expect("the vid should be set");
405-
// Make sure that there was no VID previously set for this entity.
406-
if let Some(ovid) = old_vid {
407-
bail!(
408-
"VID: {} of entity: {} with ID: {} was already present when set in EntityCache",
409-
ovid,
410-
key.entity_type,
411-
entity.id()
412-
);
413-
}
414-
415-
self.entity_op(key.clone(), EntityOp::Update(entity));
403+
// let mut entity = entity;
404+
// let old_vid = entity.set_vid(vid).expect("the vid should be set");
405+
// // Make sure that there was no VID previously set for this entity.
406+
// if let Some(ovid) = old_vid {
407+
// bail!(
408+
// "VID: {} of entity: {} with ID: {} was already present when set in EntityCache",
409+
// ovid,
410+
// key.entity_type,
411+
// entity.id()
412+
// );
413+
// }
414+
415+
self.entity_op(key.clone(), EntityOp::Update(EntityV::new(entity, vid)));
416416

417417
// The updates we were given are not valid by themselves; force a
418418
// lookup in the database and check again with an entity that merges
@@ -517,20 +517,23 @@ impl EntityCache {
517517
// Entity was created
518518
(None, EntityOp::Update(mut updates))
519519
| (None, EntityOp::Overwrite(mut updates)) => {
520-
updates.remove_null_fields();
521-
let data = Arc::new(updates);
520+
let vid = updates.vid;
521+
updates.e.remove_null_fields();
522+
let data = Arc::new(updates.e.clone());
522523
self.current.insert(key.clone(), Some(data.cheap_clone()));
523524
Some(Insert {
524525
key,
525526
data,
526527
block,
527528
end: None,
529+
vid,
528530
})
529531
}
530532
// Entity may have been changed
531533
(Some(current), EntityOp::Update(updates)) => {
534+
let vid = updates.vid;
532535
let mut data = current.as_ref().clone();
533-
data.merge_remove_null_fields(updates)
536+
data.merge_remove_null_fields(updates.e)
534537
.map_err(|e| key.unknown_attribute(e))?;
535538
let data = Arc::new(data);
536539
self.current.insert(key.clone(), Some(data.cheap_clone()));
@@ -540,21 +543,24 @@ impl EntityCache {
540543
data,
541544
block,
542545
end: None,
546+
vid,
543547
})
544548
} else {
545549
None
546550
}
547551
}
548552
// Entity was removed and then updated, so it will be overwritten
549553
(Some(current), EntityOp::Overwrite(data)) => {
550-
let data = Arc::new(data);
554+
let vid = data.vid;
555+
let data = Arc::new(data.e.clone());
551556
self.current.insert(key.clone(), Some(data.cheap_clone()));
552557
if current != data {
553558
Some(Overwrite {
554559
key,
555560
data,
556561
block,
557562
end: None,
563+
vid,
558564
})
559565
} else {
560566
None

graph/src/components/store/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::cheap_clone::CheapClone;
3030
use crate::components::store::write::EntityModification;
3131
use crate::constraint_violation;
3232
use crate::data::store::scalar::Bytes;
33-
use crate::data::store::{Id, IdList, Value};
33+
use crate::data::store::{EntityV, Id, IdList, Value};
3434
use crate::data::value::Word;
3535
use crate::data_source::CausalityRegion;
3636
use crate::derive::CheapClone;
@@ -829,7 +829,7 @@ where
829829
pub enum EntityOperation {
830830
/// Locates the entity specified by `key` and sets its attributes according to the contents of
831831
/// `data`. If no entity exists with this key, creates a new entity.
832-
Set { key: EntityKey, data: Entity },
832+
Set { key: EntityKey, data: EntityV },
833833

834834
/// Removes an entity with the specified key, if one exists.
835835
Remove { key: EntityKey },

graph/src/components/store/write.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ pub enum EntityModification {
4545
data: Arc<Entity>,
4646
block: BlockNumber,
4747
end: Option<BlockNumber>,
48+
vid: i64,
4849
},
4950
/// Update the entity by overwriting it
5051
Overwrite {
5152
key: EntityKey,
5253
data: Arc<Entity>,
5354
block: BlockNumber,
5455
end: Option<BlockNumber>,
56+
vid: i64,
5557
},
5658
/// Remove the entity
5759
Remove { key: EntityKey, block: BlockNumber },
@@ -67,6 +69,7 @@ pub struct EntityWrite<'a> {
6769
// The end of the block range for which this write is valid. The value
6870
// of `end` itself is not included in the range
6971
pub end: Option<BlockNumber>,
72+
vid: i64,
7073
}
7174

7275
impl std::fmt::Display for EntityWrite<'_> {
@@ -89,24 +92,28 @@ impl<'a> TryFrom<&'a EntityModification> for EntityWrite<'a> {
8992
data,
9093
block,
9194
end,
95+
vid,
9296
} => Ok(EntityWrite {
9397
id: &key.entity_id,
9498
entity: data,
9599
causality_region: key.causality_region,
96100
block: *block,
97101
end: *end,
102+
vid: *vid,
98103
}),
99104
EntityModification::Overwrite {
100105
key,
101106
data,
102107
block,
103108
end,
109+
vid,
104110
} => Ok(EntityWrite {
105111
id: &key.entity_id,
106112
entity: &data,
107113
causality_region: key.causality_region,
108114
block: *block,
109115
end: *end,
116+
vid: *vid,
110117
}),
111118

112119
EntityModification::Remove { .. } => Err(()),
@@ -213,11 +220,13 @@ impl EntityModification {
213220
data,
214221
block,
215222
end,
223+
vid,
216224
} => Ok(Insert {
217225
key,
218226
data,
219227
block,
220228
end,
229+
vid,
221230
}),
222231
Remove { key, .. } => {
223232
return Err(constraint_violation!(
@@ -271,21 +280,23 @@ impl EntityModification {
271280
}
272281

273282
impl EntityModification {
274-
pub fn insert(key: EntityKey, data: Entity, block: BlockNumber) -> Self {
283+
pub fn insert(key: EntityKey, data: Entity, block: BlockNumber, vid: i64) -> Self {
275284
EntityModification::Insert {
276285
key,
277286
data: Arc::new(data),
278287
block,
279288
end: None,
289+
vid,
280290
}
281291
}
282292

283-
pub fn overwrite(key: EntityKey, data: Entity, block: BlockNumber) -> Self {
293+
pub fn overwrite(key: EntityKey, data: Entity, block: BlockNumber, vid: i64) -> Self {
284294
EntityModification::Overwrite {
285295
key,
286296
data: Arc::new(data),
287297
block,
288298
end: None,
299+
vid,
289300
}
290301
}
291302

graph/src/data/store/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,19 @@ impl std::fmt::Debug for Entity {
11051105
}
11061106
}
11071107

1108+
/// An entity wrapper that has VID too.
1109+
#[derive(Debug, Clone, CacheWeight, PartialEq, Eq, Serialize)]
1110+
pub struct EntityV {
1111+
pub e: Entity,
1112+
pub vid: i64,
1113+
}
1114+
1115+
impl EntityV {
1116+
pub fn new(e: Entity, vid: i64) -> Self {
1117+
Self { e, vid }
1118+
}
1119+
}
1120+
11081121
/// An object that is returned from a query. It's a an `r::Value` which
11091122
/// carries the attributes of the object (`__typename`, `id` etc.) and
11101123
/// possibly a pointer to its parent if the query that constructed it is one

server/index-node/src/resolver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ fn entity_changes_to_graphql(entity_changes: Vec<EntityOperation>) -> r::Value {
749749
.push(key.entity_id);
750750
}
751751
EntityOperation::Set { key, data } => {
752-
updates.entry(key.entity_type).or_default().push(data);
752+
updates.entry(key.entity_type).or_default().push(data.e);
753753
}
754754
}
755755
}

store/postgres/src/relational.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use crate::{
6969
},
7070
};
7171
use graph::components::store::{AttributeNames, DerivedEntityQuery};
72-
use graph::data::store::{Id, IdList, IdType, BYTES_SCALAR};
72+
use graph::data::store::{EntityV, Id, IdList, IdType, BYTES_SCALAR};
7373
use graph::data::subgraph::schema::POI_TABLE;
7474
use graph::prelude::{
7575
anyhow, info, BlockNumber, DeploymentHash, Entity, EntityChange, EntityOperation, Logger,
@@ -731,9 +731,10 @@ impl Layout {
731731
let entity_id = data.id();
732732
processed_entities.insert((entity_type.clone(), entity_id.clone()));
733733

734+
let vid = data.vid();
734735
changes.push(EntityOperation::Set {
735736
key: entity_type.key_in(entity_id, CausalityRegion::from_entity(&data)),
736-
data,
737+
data: EntityV::new(data, vid),
737738
});
738739
}
739740

0 commit comments

Comments
 (0)