| 1 |
import warnings |
| 2 |
|
| 3 |
from ..helpers import quote_string, random_string, stringify_param_value |
| 4 |
from .commands import AsyncGraphCommands, GraphCommands |
| 5 |
from .edge import Edge # noqa |
| 6 |
from .node import Node # noqa |
| 7 |
from .path import Path # noqa |
| 8 |
|
| 9 |
DB_LABELS = "DB.LABELS" |
| 10 |
DB_RELATIONSHIPTYPES = "DB.RELATIONSHIPTYPES" |
| 11 |
DB_PROPERTYKEYS = "DB.PROPERTYKEYS" |
| 12 |
|
| 13 |
|
| 14 |
class Graph(GraphCommands): |
| 15 |
""" |
| 16 |
Graph, collection of nodes and edges. |
| 17 |
""" |
| 18 |
|
| 19 |
def __init__(self, client, name=random_string()): |
| 20 |
""" |
| 21 |
Create a new graph. |
| 22 |
""" |
| 23 |
warnings.warn( |
| 24 |
DeprecationWarning( |
| 25 |
"RedisGraph support is deprecated as of Redis Stack 7.2 \ |
| 26 |
(https://redis.com/blog/redisgraph-eol/)" |
| 27 |
) |
| 28 |
) |
| 29 |
self.NAME = name # Graph key |
| 30 |
self.client = client |
| 31 |
self.execute_command = client.execute_command |
| 32 |
|
| 33 |
self.nodes = {} |
| 34 |
self.edges = [] |
| 35 |
self._labels = [] # List of node labels. |
| 36 |
self._properties = [] # List of properties. |
| 37 |
self._relationship_types = [] # List of relation types. |
| 38 |
self.version = 0 # Graph version |
| 39 |
|
| 40 |
@property |
| 41 |
def name(self): |
| 42 |
return self.NAME |
| 43 |
|
| 44 |
def _clear_schema(self): |
| 45 |
self._labels = [] |
| 46 |
self._properties = [] |
| 47 |
self._relationship_types = [] |
| 48 |
|
| 49 |
def _refresh_schema(self): |
| 50 |
self._clear_schema() |
| 51 |
self._refresh_labels() |
| 52 |
self._refresh_relations() |
| 53 |
self._refresh_attributes() |
| 54 |
|
| 55 |
def _refresh_labels(self): |
| 56 |
lbls = self.labels() |
| 57 |
|
| 58 |
# Unpack data. |
| 59 |
self._labels = [l[0] for _, l in enumerate(lbls)] |
| 60 |
|
| 61 |
def _refresh_relations(self): |
| 62 |
rels = self.relationship_types() |
| 63 |
|
| 64 |
# Unpack data. |
| 65 |
self._relationship_types = [r[0] for _, r in enumerate(rels)] |
| 66 |
|
| 67 |
def _refresh_attributes(self): |
| 68 |
props = self.property_keys() |
| 69 |
|
| 70 |
# Unpack data. |
| 71 |
self._properties = [p[0] for _, p in enumerate(props)] |
| 72 |
|
| 73 |
def get_label(self, idx): |
| 74 |
""" |
| 75 |
Returns a label by it's index |
| 76 |
|
| 77 |
Args: |
| 78 |
|
| 79 |
idx: |
| 80 |
The index of the label |
| 81 |
""" |
| 82 |
try: |
| 83 |
label = self._labels[idx] |
| 84 |
except IndexError: |
| 85 |
# Refresh labels. |
| 86 |
self._refresh_labels() |
| 87 |
label = self._labels[idx] |
| 88 |
return label |
| 89 |
|
| 90 |
def get_relation(self, idx): |
| 91 |
""" |
| 92 |
Returns a relationship type by it's index |
| 93 |
|
| 94 |
Args: |
| 95 |
|
| 96 |
idx: |
| 97 |
The index of the relation |
| 98 |
""" |
| 99 |
try: |
| 100 |
relationship_type = self._relationship_types[idx] |
| 101 |
except IndexError: |
| 102 |
# Refresh relationship types. |
| 103 |
self._refresh_relations() |
| 104 |
relationship_type = self._relationship_types[idx] |
| 105 |
return relationship_type |
| 106 |
|
| 107 |
def get_property(self, idx): |
| 108 |
""" |
| 109 |
Returns a property by it's index |
| 110 |
|
| 111 |
Args: |
| 112 |
|
| 113 |
idx: |
| 114 |
The index of the property |
| 115 |
""" |
| 116 |
try: |
| 117 |
p = self._properties[idx] |
| 118 |
except IndexError: |
| 119 |
# Refresh properties. |
| 120 |
self._refresh_attributes() |
| 121 |
p = self._properties[idx] |
| 122 |
return p |
| 123 |
|
| 124 |
def add_node(self, node): |
| 125 |
""" |
| 126 |
Adds a node to the graph. |
| 127 |
""" |
| 128 |
if node.alias is None: |
| 129 |
node.alias = random_string() |
| 130 |
self.nodes[node.alias] = node |
| 131 |
|
| 132 |
def add_edge(self, edge): |
| 133 |
""" |
| 134 |
Adds an edge to the graph. |
| 135 |
""" |
| 136 |
if not (self.nodes[edge.src_node.alias] and self.nodes[edge.dest_node.alias]): |
| 137 |
raise AssertionError("Both edge's end must be in the graph") |
| 138 |
|
| 139 |
self.edges.append(edge) |
| 140 |
|
| 141 |
def _build_params_header(self, params): |
| 142 |
if params is None: |
| 143 |
return "" |
| 144 |
if not isinstance(params, dict): |
| 145 |
raise TypeError("'params' must be a dict") |
| 146 |
# Header starts with "CYPHER" |
| 147 |
params_header = "CYPHER " |
| 148 |
for key, value in params.items(): |
| 149 |
params_header += str(key) + "=" + stringify_param_value(value) + " " |
| 150 |
return params_header |
| 151 |
|
| 152 |
# Procedures. |
| 153 |
def call_procedure(self, procedure, *args, read_only=False, **kwagrs): |
| 154 |
args = [quote_string(arg) for arg in args] |
| 155 |
q = f"CALL {procedure}({','.join(args)})" |
| 156 |
|
| 157 |
y = kwagrs.get("y", None) |
| 158 |
if y is not None: |
| 159 |
q += f"YIELD {','.join(y)}" |
| 160 |
|
| 161 |
return self.query(q, read_only=read_only) |
| 162 |
|
| 163 |
def labels(self): |
| 164 |
return self.call_procedure(DB_LABELS, read_only=True).result_set |
| 165 |
|
| 166 |
def relationship_types(self): |
| 167 |
return self.call_procedure(DB_RELATIONSHIPTYPES, read_only=True).result_set |
| 168 |
|
| 169 |
def property_keys(self): |
| 170 |
return self.call_procedure(DB_PROPERTYKEYS, read_only=True).result_set |
| 171 |
|
| 172 |
|
| 173 |
class AsyncGraph(Graph, AsyncGraphCommands): |
| 174 |
"""Async version for Graph""" |
| 175 |
|
| 176 |
async def _refresh_labels(self): |
| 177 |
lbls = await self.labels() |
| 178 |
|
| 179 |
# Unpack data. |
| 180 |
self._labels = [l[0] for _, l in enumerate(lbls)] |
| 181 |
|
| 182 |
async def _refresh_attributes(self): |
| 183 |
props = await self.property_keys() |
| 184 |
|
| 185 |
# Unpack data. |
| 186 |
self._properties = [p[0] for _, p in enumerate(props)] |
| 187 |
|
| 188 |
async def _refresh_relations(self): |
| 189 |
rels = await self.relationship_types() |
| 190 |
|
| 191 |
# Unpack data. |
| 192 |
self._relationship_types = [r[0] for _, r in enumerate(rels)] |
| 193 |
|
| 194 |
async def get_label(self, idx): |
| 195 |
""" |
| 196 |
Returns a label by it's index |
| 197 |
|
| 198 |
Args: |
| 199 |
|
| 200 |
idx: |
| 201 |
The index of the label |
| 202 |
""" |
| 203 |
try: |
| 204 |
label = self._labels[idx] |
| 205 |
except IndexError: |
| 206 |
# Refresh labels. |
| 207 |
await self._refresh_labels() |
| 208 |
label = self._labels[idx] |
| 209 |
return label |
| 210 |
|
| 211 |
async def get_property(self, idx): |
| 212 |
""" |
| 213 |
Returns a property by it's index |
| 214 |
|
| 215 |
Args: |
| 216 |
|
| 217 |
idx: |
| 218 |
The index of the property |
| 219 |
""" |
| 220 |
try: |
| 221 |
p = self._properties[idx] |
| 222 |
except IndexError: |
| 223 |
# Refresh properties. |
| 224 |
await self._refresh_attributes() |
| 225 |
p = self._properties[idx] |
| 226 |
return p |
| 227 |
|
| 228 |
async def get_relation(self, idx): |
| 229 |
""" |
| 230 |
Returns a relationship type by it's index |
| 231 |
|
| 232 |
Args: |
| 233 |
|
| 234 |
idx: |
| 235 |
The index of the relation |
| 236 |
""" |
| 237 |
try: |
| 238 |
relationship_type = self._relationship_types[idx] |
| 239 |
except IndexError: |
| 240 |
# Refresh relationship types. |
| 241 |
await self._refresh_relations() |
| 242 |
relationship_type = self._relationship_types[idx] |
| 243 |
return relationship_type |
| 244 |
|
| 245 |
async def call_procedure(self, procedure, *args, read_only=False, **kwagrs): |
| 246 |
args = [quote_string(arg) for arg in args] |
| 247 |
q = f"CALL {procedure}({','.join(args)})" |
| 248 |
|
| 249 |
y = kwagrs.get("y", None) |
| 250 |
if y is not None: |
| 251 |
f"YIELD {','.join(y)}" |
| 252 |
return await self.query(q, read_only=read_only) |
| 253 |
|
| 254 |
async def labels(self): |
| 255 |
return (await self.call_procedure(DB_LABELS, read_only=True)).result_set |
| 256 |
|
| 257 |
async def property_keys(self): |
| 258 |
return (await self.call_procedure(DB_PROPERTYKEYS, read_only=True)).result_set |
| 259 |
|
| 260 |
async def relationship_types(self): |
| 261 |
return ( |
| 262 |
await self.call_procedure(DB_RELATIONSHIPTYPES, read_only=True) |
| 263 |
).result_set |