Skip to content

Commit d15cea1

Browse files
author
Zoran Cvetkov
committed
add wrapping
1 parent 60dbb31 commit d15cea1

File tree

17 files changed

+209
-154
lines changed

17 files changed

+209
-154
lines changed

chain/substreams/src/trigger.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use graph::{
88
subgraph::{MappingError, ProofOfIndexingEvent, SharedProofOfIndexing},
99
trigger_processor::HostedTrigger,
1010
},
11+
data::store::EntityV,
1112
prelude::{
1213
anyhow, async_trait, BlockHash, BlockNumber, BlockState, CheapClone, RuntimeHostBuilder,
1314
},
@@ -225,7 +226,8 @@ where
225226
logger,
226227
);
227228

228-
state.entity_cache.set(key, entity)?;
229+
// TODO: check if 0 is correct VID
230+
state.entity_cache.set(key, EntityV::new(entity, 0))?;
229231
}
230232
ParsedChanges::Delete(entity_key) => {
231233
let entity_type = entity_key.entity_type.cheap_clone();

core/src/subgraph/runner.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use graph::components::{
1515
subgraph::{MappingError, PoICausalityRegion, ProofOfIndexing, SharedProofOfIndexing},
1616
};
1717
use graph::data::store::scalar::Bytes;
18+
use graph::data::store::EntityV;
1819
use graph::data::subgraph::{
1920
schema::{SubgraphError, SubgraphHealth},
2021
SubgraphFeature,
@@ -1584,7 +1585,8 @@ async fn update_proof_of_indexing(
15841585
data.push((entity_cache.schema.poi_block_time(), block_time));
15851586
}
15861587
let poi = entity_cache.make_entity(data)?;
1587-
entity_cache.set(key, poi)
1588+
// VOI is autogenerated for POI table and our input is ignored
1589+
entity_cache.set(key, EntityV::new(poi, 0))
15881590
}
15891591

15901592
let _section_guard = stopwatch.start_section("update_proof_of_indexing");

graph/src/components/store/entity_cache.rs

Lines changed: 21 additions & 21 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::ENV_VARS;
1212
use crate::schema::{EntityKey, InputSchema};
1313
use crate::util::intern::Error as InternError;
@@ -29,8 +29,8 @@ pub enum GetScope {
2929
#[derive(Debug, Clone)]
3030
enum EntityOp {
3131
Remove,
32-
Update(Entity),
33-
Overwrite(Entity),
32+
Update(EntityV),
33+
Overwrite(EntityV),
3434
}
3535

3636
impl EntityOp {
@@ -41,7 +41,7 @@ impl EntityOp {
4141
use EntityOp::*;
4242
match (self, entity) {
4343
(Remove, _) => Ok(None),
44-
(Overwrite(new), _) | (Update(new), None) => Ok(Some(new)),
44+
(Overwrite(new), _) | (Update(new), None) => Ok(Some(new.e)),
4545
(Update(updates), Some(entity)) => {
4646
let mut e = entity.borrow().clone();
4747
e.merge_remove_null_fields(updates)?;
@@ -65,7 +65,7 @@ impl EntityOp {
6565
match self {
6666
// This is how `Overwrite` is constructed, by accumulating `Update` onto `Remove`.
6767
Remove => *self = Overwrite(update),
68-
Update(current) | Overwrite(current) => current.merge(update),
68+
Update(current) | Overwrite(current) => current.e.merge(update.e),
6969
}
7070
}
7171
}
@@ -278,9 +278,9 @@ impl EntityCache {
278278
) -> Result<Option<Entity>, anyhow::Error> {
279279
match op {
280280
EntityOp::Update(entity) | EntityOp::Overwrite(entity)
281-
if query.matches(key, entity) =>
281+
if query.matches(key, &entity.e) =>
282282
{
283-
Ok(Some(entity.clone()))
283+
Ok(Some(entity.e.clone()))
284284
}
285285
EntityOp::Remove => Ok(None),
286286
_ => Ok(None),
@@ -349,9 +349,9 @@ impl EntityCache {
349349
/// with existing data. The entity will be validated against the
350350
/// subgraph schema, and any errors will result in an `Err` being
351351
/// returned.
352-
pub fn set(&mut self, key: EntityKey, entity: Entity) -> Result<(), anyhow::Error> {
352+
pub fn set(&mut self, key: EntityKey, entity: EntityV) -> Result<(), anyhow::Error> {
353353
// check the validate for derived fields
354-
let is_valid = entity.validate(&key).is_ok();
354+
let is_valid = entity.e.validate(&key).is_ok();
355355

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

@@ -453,33 +453,33 @@ impl EntityCache {
453453
for (key, update) in self.updates {
454454
use EntityModification::*;
455455

456-
let is_poi = key.entity_type.is_poi();
456+
// let is_poi = key.entity_type.is_poi();
457457
let current = self.current.remove(&key).and_then(|entity| entity);
458458
let modification = match (current, update) {
459459
// Entity was created
460460
(None, EntityOp::Update(mut updates))
461461
| (None, EntityOp::Overwrite(mut updates)) => {
462-
updates.remove_null_fields();
462+
updates.e.remove_null_fields();
463463
let data = Arc::new(updates);
464-
self.current.insert(key.clone(), Some(data.cheap_clone()));
465-
let vid = if is_poi { 0 } else { data.vid() };
464+
self.current
465+
.insert(key.clone(), Some(data.e.clone().into()));
466466
Some(Insert {
467467
key,
468-
data,
468+
data: data.e.clone().into(),
469469
block,
470470
end: None,
471-
vid,
471+
vid: data.vid,
472472
})
473473
}
474474
// Entity may have been changed
475475
(Some(current), EntityOp::Update(updates)) => {
476476
let mut data = current.as_ref().clone();
477+
let vid = updates.vid;
477478
data.merge_remove_null_fields(updates)
478479
.map_err(|e| key.unknown_attribute(e))?;
479480
let data = Arc::new(data);
480481
self.current.insert(key.clone(), Some(data.cheap_clone()));
481482
if current != data {
482-
let vid = if is_poi { 0 } else { data.vid() };
483483
Some(Overwrite {
484484
key,
485485
data,
@@ -494,15 +494,15 @@ impl EntityCache {
494494
// Entity was removed and then updated, so it will be overwritten
495495
(Some(current), EntityOp::Overwrite(data)) => {
496496
let data = Arc::new(data);
497-
self.current.insert(key.clone(), Some(data.clone()));
498-
if current != data {
499-
let vid = if is_poi { 0 } else { data.vid() };
497+
self.current
498+
.insert(key.clone(), Some(data.e.clone().into()));
499+
if current != data.e.clone().into() {
500500
Some(Overwrite {
501501
key,
502-
data,
502+
data: data.e.clone().into(),
503503
block,
504504
end: None,
505-
vid,
505+
vid: data.vid,
506506
})
507507
} else {
508508
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/data/store/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ impl Entity {
913913
Id::try_from(self.get("id").unwrap().clone()).expect("the id is set to a valid value")
914914
}
915915

916+
// TODO: only for tests!
916917
pub fn vid(&self) -> i64 {
917918
self.get("vid")
918919
.expect("the vid is set")
@@ -934,8 +935,8 @@ impl Entity {
934935
/// If a key exists in both entities, the value from `update` is chosen.
935936
/// If a key only exists on one entity, the value from that entity is chosen.
936937
/// If a key is set to `Value::Null` in `update`, the key/value pair is removed.
937-
pub fn merge_remove_null_fields(&mut self, update: Entity) -> Result<(), InternError> {
938-
for (key, value) in update.0.into_iter() {
938+
pub fn merge_remove_null_fields(&mut self, update: EntityV) -> Result<(), InternError> {
939+
for (key, value) in update.e.0.into_iter() {
939940
match value {
940941
Value::Null => self.0.remove(&key),
941942
_ => self.0.insert(&key, value)?,
@@ -1086,6 +1087,19 @@ impl std::fmt::Debug for Entity {
10861087
}
10871088
}
10881089

1090+
/// An entity is represented as a map of attribute names to values.
1091+
#[derive(Debug, Clone, CacheWeight, PartialEq, Eq, Serialize)]
1092+
pub struct EntityV {
1093+
pub e: Entity,
1094+
pub vid: i64,
1095+
}
1096+
1097+
impl EntityV {
1098+
pub fn new(e: Entity, vid: i64) -> Self {
1099+
Self { e, vid }
1100+
}
1101+
}
1102+
10891103
/// An object that is returned from a query. It's a an `r::Value` which
10901104
/// carries the attributes of the object (`__typename`, `id` etc.) and
10911105
/// possibly a pointer to its parent if the query that constructed it is one

runtime/test/src/test.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,13 +478,12 @@ async fn test_ipfs_block() {
478478
const USER_DATA: &str = "user_data";
479479

480480
fn make_thing(id: &str, value: &str, vid: i64) -> (String, EntityModification) {
481-
const DOCUMENT: &str =
482-
" type Thing @entity { id: String!, value: String!, extra: String, vid: Int8 }";
481+
const DOCUMENT: &str = " type Thing @entity { id: String!, value: String!, extra: String }";
483482
lazy_static! {
484483
static ref SCHEMA: InputSchema = InputSchema::raw(DOCUMENT, "doesntmatter");
485484
static ref THING_TYPE: EntityType = SCHEMA.entity_type("Thing").unwrap();
486485
}
487-
let data = entity! { SCHEMA => id: id, value: value, extra: USER_DATA, vid:vid };
486+
let data = entity! { SCHEMA => id: id, value: value, extra: USER_DATA};
488487
let key = THING_TYPE.parse_key(id).unwrap();
489488
(
490489
format!("{{ \"id\": \"{}\", \"value\": \"{}\"}}", id, value),

runtime/wasm/src/host_exports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use graph::components::store::{EnsLookup, GetScope, LoadRelatedRequest};
1818
use graph::components::subgraph::{
1919
InstanceDSTemplate, PoICausalityRegion, ProofOfIndexingEvent, SharedProofOfIndexing,
2020
};
21-
use graph::data::store::{self};
21+
use graph::data::store::{self, EntityV};
2222
use graph::data_source::{CausalityRegion, DataSource, EntityTypeAccess};
2323
use graph::ensure;
2424
use graph::prelude::ethabi::param_type::Reader;
@@ -315,7 +315,7 @@ impl HostExports {
315315
data.insert(store::ID.clone(), value);
316316
}
317317
}
318-
data.insert(store::VID.clone(), Value::Int8(vid));
318+
// data.insert(store::VID.clone(), Value::Int8(vid));
319319

320320
self.check_invalid_fields(
321321
self.data_source.api_version.clone(),
@@ -352,7 +352,7 @@ impl HostExports {
352352

353353
state.metrics.track_entity_write(&entity_type, &entity);
354354

355-
state.entity_cache.set(key, entity)?;
355+
state.entity_cache.set(key, EntityV::new(entity, vid))?;
356356

357357
Ok(())
358358
}

server/index-node/src/resolver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ fn entity_changes_to_graphql(entity_changes: Vec<EntityOperation>) -> r::Value {
768768
.push(key.entity_id);
769769
}
770770
EntityOperation::Set { key, data } => {
771-
updates.entry(key.entity_type).or_default().push(data);
771+
updates.entry(key.entity_type).or_default().push(data.e);
772772
}
773773
}
774774
}

store/postgres/src/relational.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use crate::{
6767
},
6868
};
6969
use graph::components::store::{AttributeNames, DerivedEntityQuery};
70-
use graph::data::store::{Id, IdList, IdType, BYTES_SCALAR};
70+
use graph::data::store::{EntityV, Id, IdList, IdType, BYTES_SCALAR};
7171
use graph::data::subgraph::schema::POI_TABLE;
7272
use graph::prelude::{
7373
anyhow, info, BlockNumber, DeploymentHash, Entity, EntityChange, EntityOperation, Logger,
@@ -588,12 +588,12 @@ impl Layout {
588588

589589
for entity_data in inserts_or_updates.into_iter() {
590590
let entity_type = entity_data.entity_type(&self.input_schema);
591-
let data: Entity = entity_data.deserialize_with_layout(self, None)?;
592-
let entity_id = data.id();
591+
let data: EntityV = entity_data.deserialize_with_layout(self, None)?;
592+
let entity_id = data.e.id();
593593
processed_entities.insert((entity_type.clone(), entity_id.clone()));
594594

595595
changes.push(EntityOperation::Set {
596-
key: entity_type.key_in(entity_id, CausalityRegion::from_entity(&data)),
596+
key: entity_type.key_in(entity_id, CausalityRegion::from_entity(&data.e)),
597597
data,
598598
});
599599
}

store/postgres/src/relational_queries.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use diesel::sql_types::{Array, BigInt, Binary, Bool, Int8, Integer, Jsonb, Text,
1414
use diesel::QuerySource as _;
1515
use graph::components::store::write::{EntityWrite, RowGroup, WriteChunk};
1616
use graph::components::store::{Child as StoreChild, DerivedEntityQuery};
17-
use graph::data::store::{Id, IdType, NULL};
17+
use graph::data::store::{EntityV, Id, IdType, NULL};
1818
use graph::data::store::{IdList, IdRef, QueryObject};
1919
use graph::data::subgraph::schema::POI_TABLE;
2020
use graph::data::value::{Object, Word};
@@ -174,6 +174,23 @@ impl FromEntityData for Entity {
174174
}
175175
}
176176

177+
impl FromEntityData for EntityV {
178+
const WITH_INTERNAL_KEYS: bool = false;
179+
180+
type Value = graph::prelude::Value;
181+
182+
fn from_data<I: Iterator<Item = Result<(Word, Self::Value), StoreError>>>(
183+
schema: &InputSchema,
184+
parent_id: Option<Id>,
185+
iter: I,
186+
) -> Result<Self, StoreError> {
187+
debug_assert_eq!(None, parent_id);
188+
let e = schema.try_make_entity(iter).map_err(StoreError::from)?;
189+
let vid = e.vid();
190+
Ok(EntityV::new(e, vid))
191+
}
192+
}
193+
177194
impl FromEntityData for QueryObject {
178195
const WITH_INTERNAL_KEYS: bool = true;
179196

0 commit comments

Comments
 (0)