@@ -46,11 +46,11 @@ def add_nodes(self, nodes: List[GNode]) -> dict:
46
46
node_str_list = []
47
47
for node in nodes :
48
48
node_type = node .type
49
- node_attributes = {"@id" : double_hashing (node .id ), "id" : node .id }
49
+ node_attributes = {"id" : node .id }
50
+ node_attributes ["@id" ] = node .attributes .pop ("ID" , "" ) or double_hashing (node .id )
50
51
node_attributes .update (node .attributes )
51
- # _ = node_attributes.pop("type")
52
- # logger.debug(f"{node_attributes}")
53
- node_str = ", " .join ([f"{ k } : '{ v } '" if isinstance (v , str ) else f"{ k } : { v } " for k , v in node_attributes .items ()])
52
+
53
+ node_str = ", " .join ([f"{ k } : '{ v } '" if isinstance (v , (str , bool )) else f"{ k } : { v } " for k , v in node_attributes .items ()])
54
54
node_str_list .append (f"(:{ node_type } {{{ node_str } }})" )
55
55
56
56
gql = f"INSERT { ',' .join (node_str_list )} "
@@ -64,19 +64,21 @@ def add_edges(self, edges: List[GEdge]) -> dict:
64
64
edge_str_list = []
65
65
for edge in edges :
66
66
edge_type = edge .type
67
- src_id , dst_id = double_hashing (edge .start_id ,), double_hashing (edge .end_id ,)
68
- edge_attributes = {"@src_id" : src_id , "@dst_id" : dst_id }
67
+ edge_attributes = {
68
+ "@src_id" : edge .attributes .pop ("SRCID" , 0 ) or double_hashing (edge .start_id ,),
69
+ "@dst_id" : edge .attributes .pop ("DSTID" , 0 ) or double_hashing (edge .end_id ,)
70
+ }
69
71
edge_attributes .update (edge .attributes )
70
- # _ = edge_attributes.pop("type")
71
- edge_str = ", " .join ([f"{ k } : '{ v } '" if isinstance (v , str ) else f"{ k } : { v } " for k , v in edge_attributes .items ()])
72
+
73
+ edge_str = ", " .join ([f"{ k } : '{ v } '" if isinstance (v , ( str , bool ) ) else f"{ k } : { v } " for k , v in edge_attributes .items ()])
72
74
edge_str_list .append (f"()-[:{ edge_type } {{{ edge_str } }}]->()" )
73
75
74
76
gql = f"INSERT { ',' .join (edge_str_list )} "
75
77
return self .execute (gql )
76
78
77
79
def update_node (self , attributes : dict , set_attributes : dict , node_type : str = None , ID : int = None ) -> dict :
78
80
# demo: "MATCH (n:opsgptkg_employee {@ID: xxxx}) SET n.originname = 'xxx', n.description = 'xxx'"
79
- set_str = ", " .join ([f"n.{ k } ='{ v } '" if isinstance (v , str ) else f"n.{ k } ={ v } " for k , v in set_attributes .items ()])
81
+ set_str = ", " .join ([f"n.{ k } ='{ v } '" if isinstance (v , ( str , bool ) ) else f"n.{ k } ={ v } " for k , v in set_attributes .items ()])
80
82
81
83
if (ID is None ) or (not isinstance (ID , int )):
82
84
ID = self .get_current_nodeID (attributes , node_type )
@@ -89,7 +91,7 @@ def update_edge(self, src_id, dst_id, set_attributes: dict, edge_type: str = Non
89
91
src_id , dst_id , timestamp = self .get_current_edgeID (src_id , dst_id , edge_type )
90
92
src_type , dst_type = self .get_nodetypes_by_edgetype (edge_type )
91
93
# src_id, dst_id = double_hashing(src_id), double_hashing(dst_id)
92
- set_str = ", " .join ([f"e.{ k } ='{ v } '" if isinstance (v , str ) else f"e.{ k } ={ v } " for k , v in set_attributes .items ()])
94
+ set_str = ", " .join ([f"e.{ k } ='{ v } '" if isinstance (v , ( str , bool ) ) else f"e.{ k } ={ v } " for k , v in set_attributes .items ()])
93
95
# demo: MATCH ()-[r:PlayFor{@src_id:1, @dst_id:100, @timestamp:0}]->() SET r.contract = 0;
94
96
# gql = f"MATCH ()-[e:{edge_type}{{@src_id:{src_id}, @dst_id:{dst_id}, timestamp:{timestamp}}}]->() SET {set_str}"
95
97
gql = f"MATCH (n0:{ src_type } {{@id: { src_id } }})-[e]->(n1:{ dst_type } {{@id:{ dst_id } }}) SET { set_str } "
@@ -102,7 +104,7 @@ def delete_node(self, attributes: dict, node_type: str = None, ID: int = None) -
102
104
gql = f"MATCH (n:{ node_type } ) WHERE n.@ID={ ID } DELETE n"
103
105
return self .execute (gql )
104
106
105
- def delete_nodes (self , attributes : dict , node_type : str = None , IDs : List [int ] = None ) -> dict :
107
+ def delete_nodes (self , attributes : dict , node_type : str = None , IDs : List [int ] = [] ) -> dict :
106
108
if (IDs is None ) or len (IDs )== 0 :
107
109
IDs = self .get_nodeIDs (attributes , node_type )
108
110
# ID = double_hashing(ID)
@@ -138,7 +140,7 @@ def get_current_edgeID(self, src_id, dst_id, edeg_type:str = None):
138
140
if not isinstance (src_id , int ) or not isinstance (dst_id , int ):
139
141
result = self .get_current_edge (src_id , dst_id , edeg_type )
140
142
logger .debug (f"{ result } " )
141
- return result .attributes .get ("srcId " ), result .attributes .get ("dstId " ), result .attributes .get ("timestamp" )
143
+ return result .attributes .get ("SRCID " ), result .attributes .get ("DSTID " ), result .attributes .get ("timestamp" )
142
144
else :
143
145
return src_id , dst_id , 1
144
146
@@ -164,7 +166,8 @@ def get_current_nodes(self, attributes: dict, node_type: str = None, return_keys
164
166
result = self .execute (gql , return_keys = return_keys )
165
167
result = self .decode_result (result , gql )
166
168
167
- nodes = result .get ("n0" , []) or result .get ("n0.attr" , [])
169
+ nodes = result .get ("n0" , []) or result .get ("n0.attr" , [])
170
+ return self .convert2GNodes (nodes )
168
171
return [GNode (id = node ["id" ], type = node ["type" ], attributes = node ) for node in nodes ]
169
172
170
173
def get_current_edge (self , src_id , dst_id , edge_type :str = None , return_keys : list = []) -> GEdge :
@@ -177,6 +180,7 @@ def get_current_edge(self, src_id, dst_id, edge_type:str = None, return_keys: li
177
180
result = self .decode_result (result , gql )
178
181
179
182
edges = result .get ("e" , []) or result .get ("e.attr" , [])
183
+ return self .convert2GEdges (edges )[0 ]
180
184
return [GEdge (start_id = edge ["start_id" ], end_id = edge ["end_id" ], type = edge ["type" ], attributes = edge ) for edge in edges ][0 ]
181
185
182
186
def get_neighbor_nodes (self , attributes : dict , node_type : str = None , return_keys : list = [], reverse = False ) -> List [GNode ]:
@@ -192,6 +196,7 @@ def get_neighbor_nodes(self, attributes: dict, node_type: str = None, return_key
192
196
result = self .execute (gql , return_keys = return_keys )
193
197
result = self .decode_result (result , gql )
194
198
nodes = result .get ("n1" , []) or result .get ("n1.attr" , [])
199
+ return self .convert2GNodes (nodes )
195
200
return [GNode (id = node ["id" ], type = node ["type" ], attributes = node ) for node in nodes ]
196
201
197
202
def get_neighbor_edges (self , attributes : dict , node_type : str = None , return_keys : list = []) -> List [GEdge ]:
@@ -205,6 +210,7 @@ def get_neighbor_edges(self, attributes: dict, node_type: str = None, return_key
205
210
result = self .decode_result (result , gql )
206
211
207
212
edges = result .get ("e" , []) or result .get ("e.attr" , [])
213
+ return self .convert2GEdges (edges )
208
214
return [GEdge (start_id = edge ["start_id" ], end_id = edge ["end_id" ], type = edge ["type" ], attributes = edge ) for edge in edges ]
209
215
210
216
def check_neighbor_exist (self , attributes : dict , node_type : str = None , check_attributes : dict = {}) -> bool :
@@ -244,8 +250,10 @@ def get_hop_infos(self, attributes: dict, node_type: str = None, hop: int = 2, b
244
250
last_node_ids , last_node_types , result = self .deduplicate_paths (result , block_attributes , select_attributes )
245
251
hop -= hop_max
246
252
247
- nodes = [GNode (id = node ["id" ], type = node ["type" ], attributes = node ) for node in result .get ("n1" , [])]
248
- edges = [GEdge (start_id = edge ["start_id" ], end_id = edge ["end_id" ], type = edge ["type" ], attributes = edge ) for edge in result .get ("e" , [])]
253
+ nodes = self .convert2GNodes (result .get ("n1" , []))
254
+ edges = self .convert2GEdges (result .get ("e" , []))
255
+ # nodes = [GNode(id=node["id"], type=node["type"], attributes=node) for node in result.get("n1", [])]
256
+ # edges = [GEdge(start_id=edge["start_id"], end_id=edge["end_id"], type=edge["type"], attributes=edge) for edge in result.get("e", [])]
249
257
return Graph (nodes = nodes , edges = edges , paths = result .get ("p" , []))
250
258
251
259
def get_hop_nodes (self , attributes : dict , node_type : str = None , hop : int = 2 , block_attributes : dict = []) -> List [GNode ]:
@@ -387,16 +395,17 @@ def decode_path(self, col_data, k) -> List:
387
395
def decode_vertex (self , col_data , k ) -> Dict :
388
396
vertextVal = col_data .get ("vertexVal" , {})
389
397
node_val_json = {
390
- ** {"ID" : vertextVal .get ("id" , "" ), "type" : vertextVal .get ("type" , "" )},
398
+ ** {"ID" : int ( vertextVal .get ("id" , "" ) ), "type" : vertextVal .get ("type" , "" )},
391
399
** {k : v .get ("strVal" , "" ) or v .get ("intVal" , "0" ) for k , v in vertextVal .get ("props" , {}).items ()}
392
400
}
401
+ node_val_json .pop ("biz_node_id" , "" )
393
402
return node_val_json
394
403
395
404
def decode_edge (self , col_data , k ) -> Dict :
396
405
def _decode_edge (data ):
397
406
edgeVal = data .get ("edgeVal" , {})
398
407
edge_val_json = {
399
- ** {"srcId " : edgeVal .get ("srcId" , "" ), "dstId " : edgeVal .get ("dstId" , "" ), "type" : edgeVal .get ("type" , "" )},
408
+ ** {"SRCID " : int ( edgeVal .get ("srcId" , "" )) , "DSTID " : int ( edgeVal .get ("dstId" , "" ) ), "type" : edgeVal .get ("type" , "" )},
400
409
** {k : v .get ("strVal" , "" ) or v .get ("intVal" , "0" ) for k , v in edgeVal .get ("props" , {}).items ()}
401
410
}
402
411
# 存在业务逻辑
@@ -422,4 +431,21 @@ def get_nodetypes_by_edgetype(self, edge_type: str):
422
431
if edge_bridge in edge_type :
423
432
src_type , dst_type = edge_type .split (edge_bridge )
424
433
break
425
- return src_type , dst_type
434
+ return src_type , dst_type
435
+
436
+ def convert2GNodes (self , raw_nodes : List [Dict ]) -> List [GNode ]:
437
+ nodes = []
438
+ for node in raw_nodes :
439
+ node_id = node .pop ("id" )
440
+ node_type = node .pop ("type" )
441
+ nodes .append (GNode (id = node_id , type = node_type , attributes = node ))
442
+ return nodes
443
+
444
+ def convert2GEdges (self , raw_edges : List [Dict ]) -> List [GEdge ]:
445
+ edges = []
446
+ for edge in raw_edges :
447
+ start_id = edge .pop ("start_id" )
448
+ end_id = edge .pop ("end_id" )
449
+ edge_type = edge .pop ("type" )
450
+ edges .append (GEdge (start_id = start_id , end_id = end_id , type = edge_type , attributes = edge ))
451
+ return edges
0 commit comments