-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a4badc
commit d62dedd
Showing
21 changed files
with
315 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
from pkgutil import extend_path | ||
|
||
__path__ = extend_path(__path__, __name__) | ||
|
||
from mex.backend.identity.provider import GraphIdentityProvider | ||
from mex.backend.types import BackendIdentityProvider | ||
from mex.common.identity.registry import register_provider | ||
|
||
register_provider(BackendIdentityProvider.GRAPH, GraphIdentityProvider) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from functools import cache | ||
|
||
from mex.backend.graph.connector import GraphConnector | ||
from mex.backend.graph.transform import transform_identity_result_to_identity | ||
from mex.common.exceptions import MExError | ||
from mex.common.identity import BaseProvider, Identity | ||
from mex.common.models import ( | ||
MEX_PRIMARY_SOURCE_IDENTIFIER_IN_PRIMARY_SOURCE, | ||
MEX_PRIMARY_SOURCE_STABLE_TARGET_ID, | ||
) | ||
from mex.common.types import Identifier, PrimarySourceID | ||
|
||
|
||
class GraphIdentityProvider(BaseProvider, GraphConnector): | ||
"""Identity provider that communicates with the neo4j graph database.""" | ||
|
||
@cache | ||
def assign( | ||
self, | ||
had_primary_source: PrimarySourceID, | ||
identifier_in_primary_source: str, | ||
) -> Identity: | ||
"""Find an Identity in the database or assign a new one.""" | ||
graph_result = self.fetch_identities( | ||
had_primary_source=had_primary_source, | ||
identifier_in_primary_source=identifier_in_primary_source, | ||
) | ||
if len(graph_result.data) > 1: | ||
raise MExError("found multiple identities indicating graph inconsistency") | ||
if len(graph_result.data) == 1: | ||
return transform_identity_result_to_identity(graph_result.data[0]) | ||
if ( | ||
identifier_in_primary_source | ||
== MEX_PRIMARY_SOURCE_IDENTIFIER_IN_PRIMARY_SOURCE | ||
and had_primary_source == MEX_PRIMARY_SOURCE_STABLE_TARGET_ID | ||
): | ||
# This is to deal with the edge case where primary source is the parent of | ||
# all primary sources and has no parents for itself, | ||
# this will add itself as its parent. | ||
return Identity( | ||
hadPrimarySource=had_primary_source, | ||
identifier=MEX_PRIMARY_SOURCE_STABLE_TARGET_ID, | ||
identifierInPrimarySource=identifier_in_primary_source, | ||
stableTargetId=MEX_PRIMARY_SOURCE_STABLE_TARGET_ID, | ||
) | ||
return Identity( | ||
hadPrimarySource=had_primary_source, | ||
identifier=Identifier.generate(), | ||
identifierInPrimarySource=identifier_in_primary_source, | ||
stableTargetId=Identifier.generate(), | ||
) | ||
|
||
def fetch( | ||
self, | ||
*, | ||
had_primary_source: Identifier | None = None, | ||
identifier_in_primary_source: str | None = None, | ||
stable_target_id: Identifier | None = None, | ||
) -> list[Identity]: | ||
"""Find Identity instances matching the given filters. | ||
Either provide `stable_target_id` or `had_primary_source` | ||
and `identifier_in_primary_source` together to get a unique result. | ||
""" | ||
graph_result = self.fetch_identities( | ||
had_primary_source=had_primary_source, | ||
identifier_in_primary_source=identifier_in_primary_source, | ||
stable_target_id=stable_target_id, | ||
) | ||
return [ | ||
transform_identity_result_to_identity(result) | ||
for result in graph_result.data | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.