Skip to content

Commit

Permalink
fixed the deprecated keywords in the neo4j queries (#159)
Browse files Browse the repository at this point in the history
Closes #154 

---------

Co-authored-by: Malte Tashiro <[email protected]>
  • Loading branch information
ronibhakta1 and m-appel authored Dec 9, 2024
1 parent 7ae5982 commit 4985ad7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
29 changes: 15 additions & 14 deletions iyp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,16 @@ def batch_get_nodes_by_single_prop(self, label, prop_name, prop_set=set(), all=T

if all:
logging.info(f'Fetching all {label_str} nodes.')
existing_nodes = self.tx.run(f'MATCH (n:{label_str}) RETURN n.{prop_name} AS {prop_name}, ID(n) AS _id')
existing_nodes = self.tx.run(
f'MATCH (n:{label_str}) RETURN n.{prop_name} AS {prop_name}, elementId(n) AS _id')
else:
logging.info(f'Fetching up to {len(prop_set)} {label_str} nodes.')
list_prop = list(prop_set)
existing_nodes = self.tx.run(f"""
WITH $list_prop AS list_prop
MATCH (n:{label_str})
WHERE n.{prop_name} IN list_prop
RETURN n.{prop_name} AS {prop_name}, ID(n) AS _id""", list_prop=list_prop)
RETURN n.{prop_name} AS {prop_name}, elementId(n) AS _id""", list_prop=list_prop)

ids = {node[prop_name]: node['_id'] for node in existing_nodes}
existing_nodes_set = set(ids.keys())
Expand All @@ -294,7 +295,7 @@ def batch_get_nodes_by_single_prop(self, label, prop_name, prop_set=set(), all=T

create_query = f"""WITH $batch AS batch
UNWIND batch AS item CREATE (n:{label_str})
SET n = item RETURN n.{prop_name} AS {prop_name}, ID(n) AS _id"""
SET n = item RETURN n.{prop_name} AS {prop_name}, elementId(n) AS _id"""

new_nodes = self.tx.run(create_query, batch=batch)

Expand Down Expand Up @@ -407,7 +408,7 @@ def batch_get_nodes(self, label, properties, id_properties=list(), create=True):
query = f"""UNWIND $props AS prop
{action} (a:{label_str} {where_clause_str})
{set_line}
RETURN {return_clause_str}, ID(a) AS _id"""
RETURN {return_clause_str}, elementId(a) AS _id"""

ids = dict()
for i in range(0, len(properties), BATCH_SIZE):
Expand Down Expand Up @@ -463,11 +464,11 @@ def get_node(self, label, properties, id_properties=list(), create=True):
result = self.tx.run(
f"""MERGE (a:{label} {dict2str(id_property_dict)})
SET a += {dict2str(properties)}
RETURN ID(a)"""
RETURN elementId(a)"""
).single()
else:
# MATCH node
result = self.tx.run(f'MATCH (a:{label_str} {dict2str(properties)}) RETURN ID(a)').single()
result = self.tx.run(f'MATCH (a:{label_str} {dict2str(properties)}) RETURN elementId(a)').single()

if result is not None:
return result[0]
Expand All @@ -491,7 +492,7 @@ def batch_add_node_label(self, node_ids, label):

self.tx.run(f"""WITH $batch AS batch
MATCH (n)
WHERE ID(n) IN batch
WHERE elementId(n) IN batch
SET n:{label_str}""",
batch=batch)
self.commit()
Expand All @@ -503,7 +504,7 @@ def batch_get_node_extid(self, id_type):
Return None if the node does not exist.
"""

result = self.tx.run(f'MATCH (a)-[:EXTERNAL_ID]->(i:{id_type}) RETURN i.id AS extid, ID(a) AS nodeid')
result = self.tx.run(f'MATCH (a)-[:EXTERNAL_ID]->(i:{id_type}) RETURN i.id AS extid, elementId(a) AS nodeid')

ids = {}
for node in result:
Expand All @@ -518,7 +519,7 @@ def get_node_extid(self, id_type, id):
Return None if the node does not exist.
"""

result = self.tx.run(f'MATCH (a)-[:EXTERNAL_ID]->(:{id_type} {{id:{id}}}) RETURN ID(a)').single()
result = self.tx.run(f'MATCH (a)-[:EXTERNAL_ID]->(:{id_type} {{id:{id}}}) RETURN elementId(a)').single()

if result is not None:
return result[0]
Expand Down Expand Up @@ -549,7 +550,7 @@ def batch_add_links(self, type, links, action='create'):
create_query = f"""WITH $batch AS batch
UNWIND batch AS link
MATCH (x), (y)
WHERE ID(x) = link.src_id AND ID(y) = link.dst_id
WHERE elementId(x) = link.src_id AND elementId(y) = link.dst_id
CREATE (x)-[l:{type}]->(y)
WITH l, link
UNWIND link.props AS prop
Expand All @@ -559,7 +560,7 @@ def batch_add_links(self, type, links, action='create'):
create_query = f"""WITH $batch AS batch
UNWIND batch AS link
MATCH (x), (y)
WHERE ID(x) = link.src_id AND ID(y) = link.dst_id
WHERE elementId(x) = link.src_id AND elementId(y) = link.dst_id
MERGE (x)-[l:{type}]-(y)
WITH l, link
UNWIND link.props AS prop
Expand Down Expand Up @@ -587,7 +588,7 @@ def add_links(self, src_node, links):
self.__create_range_index(relationship_type, 'reference_name', on_relationship=True)

matches = ' MATCH (x)'
where = f' WHERE ID(x) = {src_node}'
where = f' WHERE elementId(x) = "{src_node}"'
merges = ''

for i, (type, dst_node, prop) in enumerate(links):
Expand All @@ -600,7 +601,7 @@ def add_links(self, src_node, links):
prop = format_properties(prop)

matches += f', (x{i})'
where += f' AND ID(x{i}) = {dst_node}'
where += f' AND elementId(x{i}) = "{dst_node}"'
merges += f' MERGE (x)-[:{type} {dict2str(prop)}]->(x{i}) '

self.tx.run(matches + where + merges).consume()
Expand All @@ -621,7 +622,7 @@ def batch_add_properties(self, id_prop_list):
add_query = """WITH $batch AS batch
UNWIND batch AS item
MATCH (n)
WHERE ID(n) = item.id
WHERE elementId(n) = item.id
SET n += item.props"""

res = self.tx.run(add_query, batch=batch)
Expand Down
4 changes: 2 additions & 2 deletions iyp/crawlers/alice_lg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
# (:AS)-[:MEMBER_OF]->(:IXP)
# Get IXP peering LANs:
# MATCH (p:Prefix)-[:MANAGED_BY]->(i:IXP)
# RETURN p.prefix AS peering_lan, ID(i) AS ixp_qid
# RETURN p.prefix AS peering_lan, elementId(i) AS ixp_qid
# neighbors -> list of neighbors
# neighbor['address'] -> map to prefix
# /routeservers/:id/neighbors/:neighborId/routes/received
Expand Down Expand Up @@ -338,7 +338,7 @@ def __get_peering_lans(self) -> radix.Radix:
"""Get IXP peering LANs from IYP and return a radix tree containing the QID of
the IXP node in the data['ixp_qid'] field of each tree node."""
query = """MATCH (p:Prefix)-[:MANAGED_BY]->(i:IXP)
RETURN p.prefix AS peering_lan, ID(i) AS ixp_qid"""
RETURN p.prefix AS peering_lan, elementId(i) AS ixp_qid"""
peering_lans = radix.Radix()
for res in self.iyp.tx.run(query):
n = peering_lans.add(res['peering_lan'])
Expand Down
2 changes: 1 addition & 1 deletion iyp/crawlers/cloudflare/dns_top_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, organization, url, name):
existing_dn = self.iyp.tx.run(
f"""MATCH (dn:DomainName)-[r:RANK]-(:Ranking)
WHERE r.rank < {RANK_THRESHOLD}
RETURN ID(dn) AS _id, dn.name AS dname;""")
RETURN elementId(dn) AS _id, dn.name AS dname;""")

self.domain_names_id = {node['dname']: node['_id'] for node in existing_dn}
self.domain_names = list(self.domain_names_id.keys())
Expand Down
4 changes: 2 additions & 2 deletions iyp/post/clean_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_links_of_type(self, link_type, prop_dict=None):
query = f"""
MATCH (src)-[r:{link_type}]->(dst)
WHERE {prop_conditions}
RETURN ID(r) AS link_id, ID(src) AS src_id, ID(dst) AS dst_id, {prop_str}
RETURN elementId(r) AS link_id, elementId(src) AS src_id, elementId(dst) AS dst_id, {prop_str}
"""
result = self.iyp.tx.run(query)
if result:
Expand All @@ -49,7 +49,7 @@ def delete_links(self, link_ids):
query = """
UNWIND $link_ids AS link_id
MATCH ()-[r]->()
WHERE ID(r) = link_id
WHERE elementId(r) = link_id
DELETE r
"""
self.iyp.tx.run(query, link_ids=link_ids)
Expand Down
2 changes: 1 addition & 1 deletion iyp/post/country_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def run(self):
'alpha3': country_info.alpha3}
self.iyp.tx.run("""
MATCH (n:Country)
WHERE ID(n) = $id
WHERE elementId(n) = $id
SET n += $props
""",
id=country_id[country_code],
Expand Down

0 comments on commit 4985ad7

Please sign in to comment.