-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
108 lines (89 loc) · 4.08 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from flask import current_app as app
from neo4j import GraphDatabase
class Neo4J:
def __init__(self, uri: str, user: str, password: str) -> None:
self._driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self) -> None:
self._driver.close()
def get_nodes(self, message: str) -> dict:
with self._driver.session() as session:
node = session.write_transaction(self._get_nodes, message)
return node
def get_node(self, node_id: str) -> dict:
with self._driver.session() as session:
node = session.write_transaction(self._get_node, node_id)
return node
def create_node(self, message: str) -> int:
with self._driver.session() as session:
node_id = session.write_transaction(self._create_node, message)
return node_id
def delete_node(self, node_id: str) -> str:
with self._driver.session() as session:
node_id = session.write_transaction(self._delete_node, node_id)
return node_id
def create_edge(self, start: int, end: int, cost: int) -> int:
with self._driver.session() as session:
edge_id = session.write_transaction(
self._create_edge, start, end, cost)
return edge_id
def delete_edge(self, start: int, end: int) -> int:
with self._driver.session() as session:
edge_id = session.write_transaction(self._delete_edge, start, end)
return edge_id
def shortest_path(self, start: int, end: int) -> list:
with self._driver.session() as session:
edge_id = session.write_transaction(self._shortest_path, start, end)
return edge_id
@staticmethod
def _get_nodes(tx, message: str) -> list:
where = "WHERE a.message = $message " if message else ""
result = tx.run("MATCH (a) "
+ where
+ "RETURN a ", message=message)
return result.records()
@staticmethod
def _get_node(tx, node_id: str) -> dict:
result = tx.run("MATCH (a) "
"WHERE ID(a) = $node_id "
"RETURN a ", node_id=node_id)
return result.single()[0]
@staticmethod
def _create_node(tx, message: str) -> str:
result = tx.run("CREATE (a:Node) "
"SET a.message = $message "
"RETURN ID(a)", message=message)
return result.single()[0]
@staticmethod
def _delete_node(tx, node_id: str) -> str:
result = tx.run("MATCH (a:Node) where ID(a) = $node_id "
"OPTIONAL MATCH (a)-[r]-() "
"DELETE r,a "
"RETURN ID(a)", node_id=node_id)
return result.single()[0]
@staticmethod
def _create_edge(tx, start: int, end: int, cost: int) -> str:
result = tx.run("MATCH (a:Node),(b:Node) "
"WHERE ID(a) = $start AND ID(b) = $end "
"CREATE (a)-[r:CONNECTION {cost:$cost}]->(b) "
"RETURN ID(r)", start=start, end=end, cost=cost)
return result.single()[0]
@staticmethod
def _delete_edge(tx, start: int, end: int) -> str:
result = tx.run("MATCH (a:Node)-[r:CONNECTION]-(b:Node) "
"WHERE ID(a) = $start AND ID(b) = $end "
"DELETE r "
"RETURN ID(r)", start=start, end=end)
return result.single()[0]
@staticmethod
def _shortest_path(tx, start: int, end: int) -> list:
result = tx.run("MATCH (a:Node), (b:Node) "
"WHERE ID(a) = $start AND ID(b) = $end "
"CALL algo.shortestPath.stream(a, b, 'cost') "
"YIELD nodeId, cost "
"MATCH (other:Node) WHERE id(other) = nodeId "
"RETURN ID(other) AS id, other.message, cost ",
start=start, end=end)
return result.records()
def neo4j_connect() -> Neo4J:
return Neo4J(
app.config['DB_HOST'], app.config['DB_USER'], app.config['DB_PASS'])