Skip to content

Commit

Permalink
all: Rename EntityRef to EntityKey
Browse files Browse the repository at this point in the history
  • Loading branch information
lutter committed Aug 22, 2022
1 parent a3ee629 commit fbd6f47
Show file tree
Hide file tree
Showing 26 changed files with 114 additions and 114 deletions.
6 changes: 3 additions & 3 deletions chain/substreams/src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Error;
use graph::{
blockchain::{self, block_stream::BlockWithTriggers, BlockPtr},
components::{
store::{DeploymentLocator, EntityRef, SubgraphFork},
store::{DeploymentLocator, EntityKey, SubgraphFork},
subgraph::{MappingError, ProofOfIndexingEvent, SharedProofOfIndexing},
},
data::store::scalar::Bytes,
Expand Down Expand Up @@ -150,7 +150,7 @@ where
let entity_type: &str = &entity_change.entity;
let entity_id: String = String::from_utf8(entity_change.id.clone())
.map_err(|e| MappingError::Unknown(anyhow::Error::from(e)))?;
let key = EntityRef::data(entity_type.to_string(), entity_id.clone());
let key = EntityKey::data(entity_type.to_string(), entity_id.clone());

let mut data: HashMap<String, Value> = HashMap::from_iter(vec![]);
for field in entity_change.fields.iter() {
Expand Down Expand Up @@ -205,7 +205,7 @@ where
let entity_type: &str = &entity_change.entity;
let entity_id: String = String::from_utf8(entity_change.id.clone())
.map_err(|e| MappingError::Unknown(anyhow::Error::from(e)))?;
let key = EntityRef::data(entity_type.to_string(), entity_id.clone());
let key = EntityKey::data(entity_type.to_string(), entity_id.clone());

state.entity_cache.remove(key);

Expand Down
4 changes: 2 additions & 2 deletions core/src/subgraph/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::subgraph::stream::new_block_stream;
use atomic_refcell::AtomicRefCell;
use graph::blockchain::block_stream::{BlockStreamEvent, BlockWithTriggers, FirehoseCursor};
use graph::blockchain::{Block, Blockchain, DataSource, TriggerFilter as _};
use graph::components::store::EntityRef;
use graph::components::store::EntityKey;
use graph::components::{
store::ModificationsAndCache,
subgraph::{CausalityRegion, MappingError, ProofOfIndexing, SharedProofOfIndexing},
Expand Down Expand Up @@ -867,7 +867,7 @@ async fn update_proof_of_indexing(

for (causality_region, stream) in proof_of_indexing.drain() {
// Create the special POI entity key specific to this causality_region
let entity_key = EntityRef {
let entity_key = EntityKey {
entity_type: POI_OBJECT.to_owned(),
entity_id: causality_region.into(),
};
Expand Down
4 changes: 2 additions & 2 deletions core/src/subgraph/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use graph::{
components::store::EntityRef,
components::store::EntityKey,
prelude::Entity,
util::{backoff::ExponentialBackoff, lfu_cache::LfuCache},
};
Expand All @@ -18,5 +18,5 @@ pub struct IndexingState {
/// - The time THRESHOLD is passed
/// - Or the subgraph has triggers for the block
pub skip_ptr_updates_timer: Instant,
pub entity_lfu_cache: LfuCache<EntityRef, Option<Entity>>,
pub entity_lfu_cache: LfuCache<EntityKey, Option<Entity>>,
}
28 changes: 14 additions & 14 deletions graph/src/components/store/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::Arc;
use crate::blockchain::BlockPtr;
use crate::blockchain::DataSource;
use crate::components::store::{
self as s, Entity, EntityOp, EntityOperation, EntityRef, EntityType,
self as s, Entity, EntityKey, EntityOp, EntityOperation, EntityType,
};
use crate::prelude::ENV_VARS;
use crate::util::lfu_cache::LfuCache;
Expand All @@ -21,13 +21,13 @@ use crate::util::lfu_cache::LfuCache;
pub struct EntityCache {
/// The state of entities in the store. An entry of `None`
/// means that the entity is not present in the store
current: LfuCache<EntityRef, Option<Entity>>,
current: LfuCache<EntityKey, Option<Entity>>,

/// The accumulated changes to an entity.
updates: HashMap<EntityRef, EntityOp>,
updates: HashMap<EntityKey, EntityOp>,

// Updates for a currently executing handler.
handler_updates: HashMap<EntityRef, EntityOp>,
handler_updates: HashMap<EntityKey, EntityOp>,

// Marks whether updates should go in `handler_updates`.
in_handler: bool,
Expand All @@ -50,7 +50,7 @@ impl Debug for EntityCache {
pub struct ModificationsAndCache {
pub modifications: Vec<s::EntityModification>,
pub data_sources: Vec<s::StoredDynamicDataSource>,
pub entity_lfu_cache: LfuCache<EntityRef, Option<Entity>>,
pub entity_lfu_cache: LfuCache<EntityKey, Option<Entity>>,
}

impl EntityCache {
Expand All @@ -67,7 +67,7 @@ impl EntityCache {

pub fn with_current(
store: Arc<dyn s::WritableStore>,
current: LfuCache<EntityRef, Option<Entity>>,
current: LfuCache<EntityKey, Option<Entity>>,
) -> EntityCache {
EntityCache {
current,
Expand Down Expand Up @@ -101,7 +101,7 @@ impl EntityCache {
self.handler_updates.clear();
}

pub fn get(&mut self, eref: &EntityRef) -> Result<Option<Entity>, s::QueryExecutionError> {
pub fn get(&mut self, eref: &EntityKey) -> Result<Option<Entity>, s::QueryExecutionError> {
// Get the current entity, apply any updates from `updates`, then
// from `handler_updates`.
let mut entity = self.current.get_entity(&*self.store, eref)?;
Expand All @@ -114,7 +114,7 @@ impl EntityCache {
Ok(entity)
}

pub fn remove(&mut self, key: EntityRef) {
pub fn remove(&mut self, key: EntityKey) {
self.entity_op(key, EntityOp::Remove);
}

Expand All @@ -123,8 +123,8 @@ impl EntityCache {
/// with existing data. The entity will be validated against the
/// subgraph schema, and any errors will result in an `Err` being
/// returned.
pub fn set(&mut self, key: EntityRef, mut entity: Entity) -> Result<(), anyhow::Error> {
fn check_id(key: &EntityRef, prev_id: &str) -> Result<(), anyhow::Error> {
pub fn set(&mut self, key: EntityKey, mut entity: Entity) -> Result<(), anyhow::Error> {
fn check_id(key: &EntityKey, prev_id: &str) -> Result<(), anyhow::Error> {
if prev_id != key.entity_id.as_str() {
return Err(anyhow!(
"Value of {} attribute 'id' conflicts with ID passed to `store.set()`: \
Expand Down Expand Up @@ -194,7 +194,7 @@ impl EntityCache {
.push(data_source.as_stored_dynamic_data_source());
}

fn entity_op(&mut self, key: EntityRef, op: EntityOp) {
fn entity_op(&mut self, key: EntityKey, op: EntityOp) {
use std::collections::hash_map::Entry;
let updates = match self.in_handler {
true => &mut self.handler_updates,
Expand Down Expand Up @@ -253,7 +253,7 @@ impl EntityCache {

for (entity_type, entities) in self.store.get_many(missing_by_type)? {
for entity in entities {
let key = EntityRef {
let key = EntityKey {
entity_type: entity_type.clone(),
entity_id: entity.id().unwrap().into(),
};
Expand Down Expand Up @@ -317,12 +317,12 @@ impl EntityCache {
}
}

impl LfuCache<EntityRef, Option<Entity>> {
impl LfuCache<EntityKey, Option<Entity>> {
// Helper for cached lookup of an entity.
fn get_entity(
&mut self,
store: &(impl s::WritableStore + ?Sized),
key: &EntityRef,
key: &EntityKey,
) -> Result<Option<Entity>, s::QueryExecutionError> {
match self.get(key) {
None => {
Expand Down
18 changes: 9 additions & 9 deletions graph/src/components/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ impl EntityFilterDerivative {
/// Key by which an individual entity in the store can be accessed. Stores
/// only the entity type and id. The deployment must be known from context.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EntityRef {
pub struct EntityKey {
/// Name of the entity type.
pub entity_type: EntityType,

/// ID of the individual entity.
pub entity_id: Word,
}

impl EntityRef {
impl EntityKey {
pub fn data(entity_type: String, entity_id: String) -> Self {
Self {
entity_type: EntityType::new(entity_type),
Expand Down Expand Up @@ -536,7 +536,7 @@ pub enum EntityChange {
}

impl EntityChange {
pub fn for_data(subgraph_id: DeploymentHash, key: EntityRef) -> Self {
pub fn for_data(subgraph_id: DeploymentHash, key: EntityKey) -> Self {
Self::Data {
subgraph_id: subgraph_id,
entity_type: key.entity_type,
Expand Down Expand Up @@ -781,10 +781,10 @@ where
pub enum EntityOperation {
/// Locates the entity specified by `key` and sets its attributes according to the contents of
/// `data`. If no entity exists with this key, creates a new entity.
Set { key: EntityRef, data: Entity },
Set { key: EntityKey, data: Entity },

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

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -865,15 +865,15 @@ pub type PoolWaitStats = Arc<RwLock<MovingStats>>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EntityModification {
/// Insert the entity
Insert { key: EntityRef, data: Entity },
Insert { key: EntityKey, data: Entity },
/// Update the entity by overwriting it
Overwrite { key: EntityRef, data: Entity },
Overwrite { key: EntityKey, data: Entity },
/// Remove the entity
Remove { key: EntityRef },
Remove { key: EntityKey },
}

impl EntityModification {
pub fn entity_ref(&self) -> &EntityRef {
pub fn entity_ref(&self) -> &EntityKey {
use EntityModification::*;
match self {
Insert { key, .. } | Overwrite { key, .. } | Remove { key } => key,
Expand Down
2 changes: 1 addition & 1 deletion graph/src/components/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub trait WritableStore: Send + Sync + 'static {
async fn supports_proof_of_indexing(&self) -> Result<bool, StoreError>;

/// Looks up an entity using the given store key at the latest block.
fn get(&self, key: &EntityRef) -> Result<Option<Entity>, StoreError>;
fn get(&self, key: &EntityKey) -> Result<Option<Entity>, StoreError>;

/// Transact the entity changes from a single block atomically into the store, and update the
/// subgraph block pointer to `block_ptr_to`, and update the firehose cursor to `firehose_cursor`
Expand Down
4 changes: 2 additions & 2 deletions graph/src/components/subgraph/instance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::blockchain::Blockchain;
use crate::components::store::EntityRef;
use crate::components::store::EntityKey;
use crate::prelude::*;
use crate::util::lfu_cache::LfuCache;
use crate::{components::store::WritableStore, data::subgraph::schema::SubgraphError};
Expand Down Expand Up @@ -28,7 +28,7 @@ pub struct BlockState<C: Blockchain> {
impl<C: Blockchain> BlockState<C> {
pub fn new(
store: Arc<dyn WritableStore>,
lfu_cache: LfuCache<EntityRef, Option<Entity>>,
lfu_cache: LfuCache<EntityKey, Option<Entity>>,
) -> Self {
BlockState {
entity_cache: EntityCache::with_current(store, lfu_cache),
Expand Down
4 changes: 2 additions & 2 deletions graph/src/data/schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cheap_clone::CheapClone;
use crate::components::store::{EntityRef, EntityType, SubgraphStore};
use crate::components::store::{EntityKey, EntityType, SubgraphStore};
use crate::data::graphql::ext::{DirectiveExt, DirectiveFinder, DocumentExt, TypeExt, ValueExt};
use crate::data::graphql::ObjectTypeExt;
use crate::data::store::{self, ValueType};
Expand Down Expand Up @@ -608,7 +608,7 @@ impl Schema {
}

/// Construct a value for the entity type's id attribute
pub fn id_value(&self, key: &EntityRef) -> Result<store::Value, Error> {
pub fn id_value(&self, key: &EntityKey) -> Result<store::Value, Error> {
let base_type = self
.document
.get_object_type_definition(key.entity_type.as_str())
Expand Down
6 changes: 3 additions & 3 deletions graph/src/data/store/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
components::store::{DeploymentLocator, EntityRef, EntityType},
components::store::{DeploymentLocator, EntityKey, EntityType},
data::graphql::ObjectTypeExt,
prelude::{anyhow::Context, q, r, s, CacheWeight, QueryExecutionError, Schema},
runtime::gas::{Gas, GasSizeOf},
Expand Down Expand Up @@ -700,7 +700,7 @@ impl Entity {
/// Validate that this entity matches the object type definition in the
/// schema. An entity that passes these checks can be stored
/// successfully in the subgraph's database schema
pub fn validate(&self, schema: &Schema, key: &EntityRef) -> Result<(), anyhow::Error> {
pub fn validate(&self, schema: &Schema, key: &EntityKey) -> Result<(), anyhow::Error> {
fn scalar_value_type(schema: &Schema, field_type: &s::Type) -> ValueType {
use s::TypeDefinition as t;
match field_type {
Expand Down Expand Up @@ -938,7 +938,7 @@ fn entity_validation() {
let schema =
crate::prelude::Schema::parse(DOCUMENT, subgraph).expect("Failed to parse test schema");
let id = thing.id().unwrap_or("none".to_owned());
let key = EntityRef::data("Thing".to_owned(), id.clone());
let key = EntityKey::data("Thing".to_owned(), id.clone());

let err = thing.validate(&schema, &key);
if errmsg == "" {
Expand Down
4 changes: 2 additions & 2 deletions graph/src/runtime/gas/size_of.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Various implementations of GasSizeOf;
use crate::{
components::store::{EntityRef, EntityType},
components::store::{EntityKey, EntityType},
data::store::{scalar::Bytes, Value},
prelude::{BigDecimal, BigInt},
};
Expand Down Expand Up @@ -162,7 +162,7 @@ impl GasSizeOf for usize {
}
}

impl GasSizeOf for EntityRef {
impl GasSizeOf for EntityKey {
fn gas_size_of(&self) -> Gas {
self.entity_type.gas_size_of() + self.entity_id.gas_size_of()
}
Expand Down
4 changes: 2 additions & 2 deletions graph/src/util/cache_weight.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
components::store::{EntityRef, EntityType},
components::store::{EntityKey, EntityType},
data::value::Word,
prelude::{q, BigDecimal, BigInt, Value},
};
Expand Down Expand Up @@ -121,7 +121,7 @@ impl CacheWeight for EntityType {
}
}

impl CacheWeight for EntityRef {
impl CacheWeight for EntityKey {
fn indirect_weight(&self) -> usize {
self.entity_id.indirect_weight() + self.entity_type.indirect_weight()
}
Expand Down
8 changes: 4 additions & 4 deletions graph/tests/entity_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use slog::Logger;
use std::collections::BTreeMap;
use std::sync::Arc;

use graph::components::store::{EntityRef, EntityType, StoredDynamicDataSource, WritableStore};
use graph::components::store::{EntityKey, EntityType, StoredDynamicDataSource, WritableStore};
use graph::{
components::store::{DeploymentId, DeploymentLocator},
prelude::{anyhow, DeploymentHash, Entity, EntityCache, EntityModification, Value},
Expand Down Expand Up @@ -86,7 +86,7 @@ impl WritableStore for MockStore {
unimplemented!()
}

fn get(&self, key: &EntityRef) -> Result<Option<Entity>, StoreError> {
fn get(&self, key: &EntityKey) -> Result<Option<Entity>, StoreError> {
match self.get_many_res.get(&key.entity_type) {
Some(entities) => Ok(entities
.iter()
Expand Down Expand Up @@ -155,9 +155,9 @@ impl WritableStore for MockStore {
}
}

fn make_band(id: &'static str, data: Vec<(&str, Value)>) -> (EntityRef, Entity) {
fn make_band(id: &'static str, data: Vec<(&str, Value)>) -> (EntityKey, Entity) {
(
EntityRef {
EntityKey {
entity_type: EntityType::new("Band".to_string()),
entity_id: id.into(),
},
Expand Down
4 changes: 2 additions & 2 deletions graphql/src/schema/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ pub fn is_list(field_type: &s::Type) -> bool {

#[test]
fn entity_validation() {
use graph::components::store::EntityRef;
use graph::components::store::EntityKey;
use graph::data::store;
use graph::prelude::{DeploymentHash, Entity};

Expand Down Expand Up @@ -435,7 +435,7 @@ fn entity_validation() {
let schema =
graph::prelude::Schema::parse(DOCUMENT, subgraph).expect("Failed to parse test schema");
let id = thing.id().unwrap_or("none".to_owned());
let key = EntityRef::data("Thing".to_owned(), id.clone());
let key = EntityKey::data("Thing".to_owned(), id.clone());

let err = thing.validate(&schema, &key);
if errmsg == "" {
Expand Down
4 changes: 2 additions & 2 deletions graphql/tests/query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate pretty_assertions;

use graph::components::store::{EntityRef, EntityType};
use graph::components::store::{EntityKey, EntityType};
use graph::data::subgraph::schema::DeploymentCreate;
use graph::entity;
use graph::prelude::SubscriptionResult;
Expand Down Expand Up @@ -222,7 +222,7 @@ async fn insert_test_entities(

async fn insert_at(entities: Vec<Entity>, deployment: &DeploymentLocator, block_ptr: BlockPtr) {
let insert_ops = entities.into_iter().map(|data| EntityOperation::Set {
key: EntityRef {
key: EntityKey {
entity_type: EntityType::new(
data.get("__typename").unwrap().clone().as_string().unwrap(),
),
Expand Down
2 changes: 1 addition & 1 deletion runtime/test/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ fn make_thing(id: &str, value: &str) -> (String, EntityModification) {
data.set("id", id);
data.set("value", value);
data.set("extra", USER_DATA);
let key = EntityRef {
let key = EntityKey {
entity_type: EntityType::new("Thing".to_string()),
entity_id: id.into(),
};
Expand Down
Loading

0 comments on commit fbd6f47

Please sign in to comment.