Skip to content

Commit 68a12cb

Browse files
committed
Remove duplicate neo4j func and add upload to delete_cache()
1 parent a13572b commit 68a12cb

File tree

2 files changed

+35
-64
lines changed

2 files changed

+35
-64
lines changed

src/app.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4514,25 +4514,29 @@ def require_json(request):
45144514
"""
45154515
def delete_cache(id):
45164516
if MEMCACHED_MODE and memcached_client_instance and MEMCACHED_PREFIX:
4517+
# First delete the target entity cache
45174518
entity_dict = query_target_entity(id, get_internal_token())
45184519
entity_uuid = entity_dict['uuid']
45194520

4521+
# If the target entity is Sample (`direct_ancestor`) or Dataset/Publication (`direct_ancestors`)
45204522
# Delete the cache of all the direct descendants (children)
4521-
# Otherwise they'll have old cached data for the `direct_ancestor` (Sample) `direct_ancestors` (Dataset/Publication) fields
4522-
children_uuids = schema_neo4j_queries.get_children(neo4j_driver_instance, entity_uuid , 'uuid')
4523+
child_uuids = schema_neo4j_queries.get_children(neo4j_driver_instance, entity_uuid , 'uuid')
45234524

45244525
# If the target entity is Collection, delete the cache for each of its associated
4525-
# Datasets (via [:IN_COLLECTION] relationship) and Publications (via [:USES_DATA] relationship)
4526-
collection_associated_uuids = schema_neo4j_queries.get_collection_associated_entities(neo4j_driver_instance, entity_uuid , 'uuid')
4526+
# Datasets and Publications (via [:IN_COLLECTION] relationship) as well as just Publications (via [:USES_DATA] relationship)
4527+
collection_dataset_uuids = schema_neo4j_queries.get_collection_associated_datasets(neo4j_driver_instance, entity_uuid , 'uuid')
45274528

4528-
# Also find the associated Collections if the target entity is Datasets or Publication
4529-
# `get_dataset_collections()` applies to both Dataset and Publication (subclass of Dataset)
4529+
# If the target entity is Upload, delete the cache for each of its associated Datasets (via [:IN_UPLOAD] relationship)
4530+
upload_dataset_uuids = schema_neo4j_queries.get_upload_datasets(neo4j_driver_instance, entity_uuid , 'uuid')
4531+
4532+
# If the target entity is Datasets/Publication, delete the associated Collections cache, Upload cache
45304533
collection_uuids = schema_neo4j_queries.get_dataset_collections(neo4j_driver_instance, entity_uuid , 'uuid')
4531-
pubication_associated_collection_dict = schema_neo4j_queries.get_publication_associated_collection(neo4j_driver_instance, entity_uuid)
4534+
collection_dict = schema_neo4j_queries.get_publication_associated_collection(neo4j_driver_instance, entity_uuid)
4535+
upload_dict = schema_neo4j_queries.get_dataset_upload(neo4j_driver_instance, entity_uuid)
45324536

45334537
# We only use uuid in the cache key acorss all the cache types
45344538
cache_keys = []
4535-
for uuid in ([entity_uuid] + children_uuids + collection_associated_uuids + collection_uuids + [pubication_associated_collection_dict['uuid']]):
4539+
for uuid in ([entity_uuid] + child_uuids + collection_dataset_uuids + upload_dataset_uuids + collection_uuids + [collection_dict['uuid']] + [upload_dict['uuid']]):
45364540
cache_keys.append(f'{MEMCACHED_PREFIX}_neo4j_{uuid}')
45374541
cache_keys.append(f'{MEMCACHED_PREFIX}_complete_{uuid}')
45384542
cache_keys.append(f'{MEMCACHED_PREFIX}_normalized_{uuid}')

src/schema/schema_neo4j_queries.py

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def get_next_revision_uuid(neo4j_driver, uuid):
691691

692692

693693
"""
694-
Get a list of associated dataset and publication uuids for a given collection
694+
Get a list of associated Datasets and Publications (subclass of Dataset) uuids for a given collection
695695
696696
Parameters
697697
----------
@@ -707,7 +707,7 @@ def get_next_revision_uuid(neo4j_driver, uuid):
707707
list
708708
A list of datasets and publications
709709
"""
710-
def get_collection_associated_entities(neo4j_driver, uuid, property_key = None):
710+
def get_collection_associated_datasets(neo4j_driver, uuid, property_key = None):
711711
results = []
712712

713713
if property_key:
@@ -719,7 +719,7 @@ def get_collection_associated_entities(neo4j_driver, uuid, property_key = None):
719719
f"WHERE c.uuid = '{uuid}' "
720720
f"RETURN apoc.coll.toSet(COLLECT(e)) AS {record_field_name}")
721721

722-
logger.info("======get_collection_associated_entities() query======")
722+
logger.info("======get_collection_associated_datasets() query======")
723723
logger.info(query)
724724

725725
with neo4j_driver.session() as session:
@@ -735,50 +735,6 @@ def get_collection_associated_entities(neo4j_driver, uuid, property_key = None):
735735

736736
return results
737737

738-
"""
739-
Get a list of associated dataset and publication uuids for a given collection
740-
741-
Parameters
742-
----------
743-
neo4j_driver : neo4j.Driver object
744-
The neo4j database connection pool
745-
uuid : str
746-
The uuid of collection
747-
property_key : str
748-
A target property key for result filtering
749-
750-
Returns
751-
-------
752-
list
753-
A list of datasets and publications
754-
"""
755-
def get_collection_associated_entities(neo4j_driver, uuid, property_key = None):
756-
results = []
757-
758-
if property_key:
759-
query = (f"MATCH (e:Entity)-[:IN_COLLECTION|:USES_DATA]->(c:Collection) "
760-
f"WHERE c.uuid = '{uuid}' "
761-
f"RETURN apoc.coll.toSet(COLLECT(e.{property_key})) AS {record_field_name}")
762-
else:
763-
query = (f"MATCH (e:Entity)-[:IN_COLLECTION|:USES_DATA]->(c:Collection) "
764-
f"WHERE c.uuid = '{uuid}' "
765-
f"RETURN apoc.coll.toSet(COLLECT(e)) AS {record_field_name}")
766-
767-
logger.info("======get_collection_associated_entities() query======")
768-
logger.info(query)
769-
770-
with neo4j_driver.session() as session:
771-
record = session.read_transaction(execute_readonly_tx, query)
772-
773-
if record and record[record_field_name]:
774-
if property_key:
775-
# Just return the list of property values from each entity node
776-
results = record[record_field_name]
777-
else:
778-
# Convert the list of nodes to a list of dicts
779-
results = nodes_to_dicts(record[record_field_name])
780-
781-
return results
782738

783739
"""
784740
Get a list of associated collection uuids for a given dataset or publication (subclass)
@@ -870,15 +826,13 @@ def get_publication_associated_collection(neo4j_driver, uuid):
870826
The neo4j database connection pool
871827
uuid : str
872828
The uuid of dataset
873-
property_key : str
874-
A target property key for result filtering
875829
876830
Returns
877831
-------
878832
dict
879833
A Upload dict
880834
"""
881-
def get_dataset_upload(neo4j_driver, uuid, property_key = None):
835+
def get_dataset_upload(neo4j_driver, uuid):
882836
result = {}
883837

884838
query = (f"MATCH (e:Entity)-[:IN_UPLOAD]->(s:Upload) "
@@ -1111,18 +1065,27 @@ def unlink_datasets_from_upload(neo4j_driver, upload_uuid, dataset_uuids_list):
11111065
The neo4j database connection pool
11121066
uuid : str
11131067
The uuid of Upload
1068+
property_key : str
1069+
A target property key for result filtering
11141070
11151071
Returns
11161072
-------
11171073
list
11181074
The list containing associated dataset dicts
11191075
"""
1120-
def get_upload_datasets(neo4j_driver, uuid):
1076+
def get_upload_datasets(neo4j_driver, uuid, property_key = None):
11211077
results = []
11221078

1123-
query = (f"MATCH (e:Dataset)-[:IN_UPLOAD]->(s:Upload) "
1124-
f"WHERE s.uuid = '{uuid}' "
1125-
f"RETURN apoc.coll.toSet(COLLECT(e)) AS {record_field_name}")
1079+
if property_key:
1080+
query = (f"MATCH (e:Dataset)-[:IN_UPLOAD]->(s:Upload) "
1081+
f"WHERE s.uuid = '{uuid}' "
1082+
# COLLECT() returns a list
1083+
# apoc.coll.toSet() reruns a set containing unique nodes
1084+
f"RETURN apoc.coll.toSet(COLLECT(e.{property_key})) AS {record_field_name}")
1085+
else:
1086+
query = (f"MATCH (e:Dataset)-[:IN_UPLOAD]->(s:Upload) "
1087+
f"WHERE s.uuid = '{uuid}' "
1088+
f"RETURN apoc.coll.toSet(COLLECT(e)) AS {record_field_name}")
11261089

11271090
logger.info("======get_upload_datasets() query======")
11281091
logger.info(query)
@@ -1131,8 +1094,12 @@ def get_upload_datasets(neo4j_driver, uuid):
11311094
record = session.read_transaction(execute_readonly_tx, query)
11321095

11331096
if record and record[record_field_name]:
1134-
# Convert the list of nodes to a list of dicts
1135-
results = nodes_to_dicts(record[record_field_name])
1097+
if property_key:
1098+
# Just return the list of property values from each entity node
1099+
results = record[record_field_name]
1100+
else:
1101+
# Convert the list of nodes to a list of dicts
1102+
results = nodes_to_dicts(record[record_field_name])
11361103

11371104
return results
11381105

0 commit comments

Comments
 (0)