Skip to content

Commit

Permalink
Move PropertyTypeStore to store crate
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann committed Nov 8, 2024
1 parent 560d82c commit 64fc087
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 289 deletions.
16 changes: 9 additions & 7 deletions libs/@local/graph/api/src/rest/property_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ use graph::{
store::{
StorePool,
error::{OntologyVersionDoesNotExist, VersionedUrlAlreadyExists},
ontology::{
ArchivePropertyTypeParams, CreatePropertyTypeParams, GetPropertyTypeSubgraphParams,
GetPropertyTypesParams, GetPropertyTypesResponse, PropertyTypeStore as _,
UnarchivePropertyTypeParams, UpdatePropertyTypeEmbeddingParams,
UpdatePropertyTypesParams,
},
},
};
use hash_graph_authorization::{
Expand All @@ -36,7 +30,15 @@ use hash_graph_authorization::{
},
zanzibar::Consistency,
};
use hash_graph_store::{ConflictBehavior, property_type::PropertyTypeQueryToken};
use hash_graph_store::{
ConflictBehavior,
property_type::{
ArchivePropertyTypeParams, CreatePropertyTypeParams, GetPropertyTypeSubgraphParams,
GetPropertyTypesParams, GetPropertyTypesResponse, PropertyTypeQueryToken,
PropertyTypeStore as _, UnarchivePropertyTypeParams, UpdatePropertyTypeEmbeddingParams,
UpdatePropertyTypesParams,
},
};
use hash_graph_types::{
ontology::{
OntologyTemporalMetadata, OntologyTypeClassificationMetadata, OntologyTypeMetadata,
Expand Down
17 changes: 10 additions & 7 deletions libs/@local/graph/graph/src/store/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ use hash_graph_store::{
},
error::{InsertionError, QueryError, UpdateError},
filter::{Filter, QueryRecord},
property_type::{
ArchivePropertyTypeParams, CountPropertyTypesParams, CreatePropertyTypeParams,
GetPropertyTypeSubgraphParams, GetPropertyTypeSubgraphResponse, GetPropertyTypesParams,
GetPropertyTypesResponse, PropertyTypeStore, UnarchivePropertyTypeParams,
UpdatePropertyTypeEmbeddingParams, UpdatePropertyTypesParams,
},
subgraph::temporal_axes::{
PinnedTemporalAxisUnresolved, QueryTemporalAxes, QueryTemporalAxesUnresolved,
VariableTemporalAxisUnresolved,
Expand Down Expand Up @@ -56,7 +62,7 @@ use type_system::{
use crate::{
ontology::domain_validator::DomainValidator,
store::{
EntityStore, EntityTypeStore, PropertyTypeStore, StorePool,
EntityStore, EntityTypeStore, StorePool,
crud::{QueryResult, Read, ReadPaginated, Sorting},
error::StoreError,
knowledge::{
Expand All @@ -65,14 +71,11 @@ use crate::{
UpdateEntityEmbeddingsParams, ValidateEntityError, ValidateEntityParams,
},
ontology::{
ArchiveEntityTypeParams, ArchivePropertyTypeParams, CountEntityTypesParams,
CountPropertyTypesParams, CreateEntityTypeParams, CreatePropertyTypeParams,
ArchiveEntityTypeParams, CountEntityTypesParams, CreateEntityTypeParams,
GetClosedMultiEntityTypeParams, GetClosedMultiEntityTypeResponse,
GetEntityTypeSubgraphParams, GetEntityTypeSubgraphResponse, GetEntityTypesParams,
GetEntityTypesResponse, GetPropertyTypeSubgraphParams, GetPropertyTypeSubgraphResponse,
GetPropertyTypesParams, GetPropertyTypesResponse, UnarchiveEntityTypeParams,
UnarchivePropertyTypeParams, UpdateEntityTypeEmbeddingParams, UpdateEntityTypesParams,
UpdatePropertyTypeEmbeddingParams, UpdatePropertyTypesParams,
GetEntityTypesResponse, UnarchiveEntityTypeParams, UpdateEntityTypeEmbeddingParams,
UpdateEntityTypesParams,
},
},
};
Expand Down
9 changes: 4 additions & 5 deletions libs/@local/graph/graph/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ mod validation;
mod fetcher;
pub(crate) mod postgres;

use hash_graph_store::{account::AccountStore, data_type::DataTypeStore};
use hash_graph_store::{
account::AccountStore, data_type::DataTypeStore, property_type::PropertyTypeStore,
};
use serde::Deserialize;
#[cfg(feature = "utoipa")]
use utoipa::ToSchema;
Expand All @@ -22,10 +24,7 @@ pub use self::{
postgres::{AsClient, PostgresStore, PostgresStorePool},
validation::{StoreCache, StoreProvider},
};
use crate::store::{
knowledge::EntityStore,
ontology::{EntityTypeStore, PropertyTypeStore},
};
use crate::store::{knowledge::EntityStore, ontology::EntityTypeStore};

/// Describes the API of a store implementation.
///
Expand Down
246 changes: 2 additions & 244 deletions libs/@local/graph/graph/src/store/ontology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use core::iter;
use std::collections::HashMap;

use error_stack::Report;
use hash_graph_authorization::schema::{
EntityTypeRelationAndSubject, PropertyTypeRelationAndSubject,
};
use hash_graph_authorization::schema::EntityTypeRelationAndSubject;
use hash_graph_store::{
ConflictBehavior,
error::{InsertionError, QueryError, UpdateError},
Expand All @@ -18,8 +16,7 @@ use hash_graph_types::{
account::{AccountId, EditionCreatedById},
ontology::{
EntityTypeMetadata, EntityTypeWithMetadata, OntologyTemporalMetadata,
OntologyTypeClassificationMetadata, PropertyTypeMetadata, PropertyTypeWithMetadata,
ProvidedOntologyEditionProvenance,
OntologyTypeClassificationMetadata, ProvidedOntologyEditionProvenance,
},
owned_by_id::OwnedById,
};
Expand All @@ -32,245 +29,6 @@ use type_system::{
url::VersionedUrl,
};

#[derive(Debug, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde(
rename_all = "camelCase",
deny_unknown_fields,
bound(deserialize = "R: Deserialize<'de>")
)]
pub struct CreatePropertyTypeParams<R> {
pub schema: PropertyType,
pub classification: OntologyTypeClassificationMetadata,
pub relationships: R,
pub conflict_behavior: ConflictBehavior,
#[serde(default, skip_serializing_if = "UserDefinedProvenanceData::is_empty")]
pub provenance: ProvidedOntologyEditionProvenance,
}

#[derive(Debug, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GetPropertyTypeSubgraphParams<'p> {
#[serde(borrow)]
pub filter: Filter<'p, PropertyTypeWithMetadata>,
pub graph_resolve_depths: GraphResolveDepths,
pub temporal_axes: QueryTemporalAxesUnresolved,
pub include_drafts: bool,
#[serde(default)]
pub after: Option<VersionedUrl>,
#[serde(default)]
pub limit: Option<usize>,
#[serde(default)]
pub include_count: bool,
}

#[derive(Debug)]
pub struct GetPropertyTypeSubgraphResponse {
pub subgraph: Subgraph,
pub cursor: Option<VersionedUrl>,
pub count: Option<usize>,
}

#[derive(Debug, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct CountPropertyTypesParams<'p> {
#[serde(borrow)]
pub filter: Filter<'p, PropertyTypeWithMetadata>,
pub temporal_axes: QueryTemporalAxesUnresolved,
pub include_drafts: bool,
}

#[derive(Debug, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GetPropertyTypesParams<'p> {
#[serde(borrow)]
pub filter: Filter<'p, PropertyTypeWithMetadata>,
pub temporal_axes: QueryTemporalAxesUnresolved,
pub include_drafts: bool,
#[serde(default)]
pub after: Option<VersionedUrl>,
#[serde(default)]
pub limit: Option<usize>,
#[serde(default)]
pub include_count: bool,
}

#[derive(Debug, Serialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct GetPropertyTypesResponse {
pub property_types: Vec<PropertyTypeWithMetadata>,
pub cursor: Option<VersionedUrl>,
pub count: Option<usize>,
}

#[derive(Debug, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct UpdatePropertyTypesParams<R> {
pub schema: PropertyType,
pub relationships: R,
#[serde(default, skip_serializing_if = "UserDefinedProvenanceData::is_empty")]
pub provenance: ProvidedOntologyEditionProvenance,
}

#[derive(Debug, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ArchivePropertyTypeParams<'a> {
#[serde(borrow)]
pub property_type_id: Cow<'a, VersionedUrl>,
}

#[derive(Debug, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct UnarchivePropertyTypeParams<'a> {
pub property_type_id: Cow<'a, VersionedUrl>,
#[serde(default, skip_serializing_if = "UserDefinedProvenanceData::is_empty")]
pub provenance: ProvidedOntologyEditionProvenance,
}

#[derive(Debug, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct UpdatePropertyTypeEmbeddingParams<'a> {
#[serde(borrow)]
pub property_type_id: Cow<'a, VersionedUrl>,
#[serde(borrow)]
pub embedding: Embedding<'a>,
pub updated_at_transaction_time: Timestamp<TransactionTime>,
pub reset: bool,
}

/// Describes the API of a store implementation for [`PropertyType`]s.
pub trait PropertyTypeStore {
/// Creates a new [`PropertyType`].
///
/// # Errors:
///
/// - if any account referred to by `metadata` does not exist.
/// - if the [`BaseUrl`] of the `property_type` already exists.
///
/// [`BaseUrl`]: type_system::url::BaseUrl
fn create_property_type<R>(
&mut self,
actor_id: AccountId,
params: CreatePropertyTypeParams<R>,
) -> impl Future<Output = Result<PropertyTypeMetadata, Report<InsertionError>>> + Send
where
Self: Send,
R: IntoIterator<Item = PropertyTypeRelationAndSubject> + Send + Sync,
{
async move {
Ok(self
.create_property_types(actor_id, iter::once(params))
.await?
.pop()
.expect("created exactly one property type"))
}
}

/// Creates the provided [`PropertyType`]s.
///
/// # Errors:
///
/// - if any account referred to by the metadata does not exist.
/// - if any [`BaseUrl`] of the property type already exists.
///
/// [`BaseUrl`]: type_system::url::BaseUrl
fn create_property_types<P, R>(
&mut self,
actor_id: AccountId,
params: P,
) -> impl Future<Output = Result<Vec<PropertyTypeMetadata>, Report<InsertionError>>> + Send
where
P: IntoIterator<Item = CreatePropertyTypeParams<R>, IntoIter: Send> + Send,
R: IntoIterator<Item = PropertyTypeRelationAndSubject> + Send + Sync;

/// Count the number of [`PropertyType`]s specified by the [`CountPropertyTypesParams`].
///
/// # Errors
///
/// - if the underlying store fails to count the property types.
fn count_property_types(
&self,
actor_id: AccountId,
params: CountPropertyTypesParams<'_>,
) -> impl Future<Output = Result<usize, Report<QueryError>>> + Send;

/// Get the [`Subgraph`] specified by the [`GetPropertyTypeSubgraphParams`].
///
/// # Errors
///
/// - if the requested [`PropertyType`] doesn't exist.
fn get_property_type_subgraph(
&self,
actor_id: AccountId,
params: GetPropertyTypeSubgraphParams<'_>,
) -> impl Future<Output = Result<GetPropertyTypeSubgraphResponse, Report<QueryError>>> + Send;

/// Get the [`PropertyTypes`] specified by the [`GetPropertyTypesParams`].
///
/// # Errors
///
/// - if the requested [`PropertyType`] doesn't exist.
///
/// [`PropertyTypes`]: PropertyType
fn get_property_types(
&self,
actor_id: AccountId,
params: GetPropertyTypesParams<'_>,
) -> impl Future<Output = Result<GetPropertyTypesResponse, Report<QueryError>>> + Send;

/// Update the definition of an existing [`PropertyType`].
///
/// # Errors
///
/// - if the [`PropertyType`] doesn't exist.
fn update_property_type<R>(
&mut self,
actor_id: AccountId,
params: UpdatePropertyTypesParams<R>,
) -> impl Future<Output = Result<PropertyTypeMetadata, Report<UpdateError>>> + Send
where
R: IntoIterator<Item = PropertyTypeRelationAndSubject> + Send + Sync;

/// Archives the definition of an existing [`PropertyType`].
///
/// # Errors
///
/// - if the [`PropertyType`] doesn't exist.
fn archive_property_type(
&mut self,
actor_id: AccountId,

params: ArchivePropertyTypeParams<'_>,
) -> impl Future<Output = Result<OntologyTemporalMetadata, Report<UpdateError>>> + Send;

/// Restores the definition of an existing [`PropertyType`].
///
/// # Errors
///
/// - if the [`PropertyType`] doesn't exist.
fn unarchive_property_type(
&mut self,
actor_id: AccountId,

params: UnarchivePropertyTypeParams<'_>,
) -> impl Future<Output = Result<OntologyTemporalMetadata, Report<UpdateError>>> + Send;

fn update_property_type_embeddings(
&mut self,
actor_id: AccountId,

params: UpdatePropertyTypeEmbeddingParams<'_>,
) -> impl Future<Output = Result<(), Report<UpdateError>>> + Send;
}

#[derive(Debug, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use hash_graph_store::{
entity_type::EntityTypeQueryPath,
error::{InsertionError, QueryError, UpdateError},
filter::{Filter, FilterExpression, ParameterList},
property_type::{GetPropertyTypeSubgraphParams, PropertyTypeStore as _},
subgraph::{
Subgraph, SubgraphRecord as _,
edges::{EdgeDirection, GraphResolveDepths, OntologyEdgeKind, OutgoingEdgeResolveDepth},
Expand Down Expand Up @@ -60,8 +61,8 @@ use crate::store::{
EntityTypeResolveDefinitions, GetClosedMultiEntityTypeParams,
GetClosedMultiEntityTypeResponse, GetEntityTypeSubgraphParams,
GetEntityTypeSubgraphResponse, GetEntityTypesParams, GetEntityTypesResponse,
GetPropertyTypeSubgraphParams, IncludeEntityTypeOption, PropertyTypeStore as _,
UnarchiveEntityTypeParams, UpdateEntityTypeEmbeddingParams, UpdateEntityTypesParams,
IncludeEntityTypeOption, UnarchiveEntityTypeParams, UpdateEntityTypeEmbeddingParams,
UpdateEntityTypesParams,
},
postgres::{
AsClient, PostgresStore, ResponseCountMap, TraversalContext,
Expand Down
Loading

0 comments on commit 64fc087

Please sign in to comment.