33
33
doc_get_or_insert ,
34
34
doc_insert ,
35
35
doc_update ,
36
+ get_arangodb_graph ,
36
37
get_node_id ,
37
38
get_node_type_and_id ,
38
39
key_is_not_reserved ,
@@ -321,56 +322,6 @@ def __delitem__(self, key: str) -> None:
321
322
root_data = self .root .data if self .root else self .data
322
323
root_data ["_rev" ] = doc_update (self .db , self .node_id , update_dict )
323
324
324
- # @logger_debug
325
- # def __iter__(self) -> Iterator[str]:
326
- # """for key in G._node['node/1']"""
327
- # yield from aql_doc_get_keys(self.db, self.node_id, self.parent_keys)
328
-
329
- # @logger_debug
330
- # def __len__(self) -> int:
331
- # """len(G._node['node/1'])"""
332
- # return aql_doc_get_length(self.db, self.node_id, self.parent_keys)
333
-
334
- # @logger_debug
335
- # def keys(self) -> Any:
336
- # """G._node['node/1'].keys()"""
337
- # yield from self.__iter__()
338
-
339
- # @logger_debug
340
- # # TODO: Revisit typing of return value
341
- # def values(self) -> Any:
342
- # """G._node['node/1'].values()"""
343
- # self.data = self.db.document(self.node_id)
344
- # yield from self.data.values()
345
-
346
- # @logger_debug
347
- # # TODO: Revisit typing of return value
348
- # def items(self) -> Any:
349
- # """G._node['node/1'].items()"""
350
-
351
- # # TODO: Revisit this lazy hack
352
- # if self.parent_keys:
353
- # yield from self.data.items()
354
- # else:
355
- # self.data = self.db.document(self.node_id)
356
- # yield from self.data.items()
357
-
358
- # ?
359
- # def pull():
360
- # pass
361
-
362
- # ?
363
- # def push():
364
- # pass
365
-
366
- # @logger_debug
367
- # def clear(self) -> None:
368
- # """G._node['node/1'].clear()"""
369
- # self.data.clear()
370
-
371
- # # if clear_remote:
372
- # # doc_insert(self.db, self.node_id, silent=True, overwrite=True)
373
-
374
325
@keys_are_strings
375
326
@keys_are_not_reserved
376
327
# @values_are_json_serializable # TODO?
@@ -435,6 +386,9 @@ def __contains__(self, key: str) -> bool:
435
386
if node_id in self .data :
436
387
return True
437
388
389
+ if self .FETCHED_ALL_DATA :
390
+ return False
391
+
438
392
return bool (self .graph .has_vertex (node_id ))
439
393
440
394
@key_is_string
@@ -446,6 +400,9 @@ def __getitem__(self, key: str) -> NodeAttrDict:
446
400
if vertex := self .data .get (node_id ):
447
401
return vertex
448
402
403
+ if self .FETCHED_ALL_DATA :
404
+ raise KeyError (key )
405
+
449
406
if vertex := self .graph .vertex (node_id ):
450
407
node_attr_dict : NodeAttrDict = self .node_attr_dict_factory ()
451
408
node_attr_dict .node_id = node_id
@@ -472,7 +429,7 @@ def __setitem__(self, key: str, value: NodeAttrDict) -> None:
472
429
473
430
node_attr_dict = self .node_attr_dict_factory ()
474
431
node_attr_dict .node_id = node_id
475
- node_attr_dict .data = result
432
+ node_attr_dict .data = build_node_attr_dict_data ( node_attr_dict , result )
476
433
477
434
self .data [node_id ] = node_attr_dict
478
435
@@ -570,16 +527,23 @@ def items(self, data: str | None = None, default: Any | None = None) -> Any:
570
527
571
528
@logger_debug
572
529
def __fetch_all (self ):
573
- self .data .clear ()
574
- for collection in self .graph .vertex_collections ():
575
- for doc in self .graph .vertex_collection (collection ).all ():
576
- node_id = doc ["_id" ]
530
+ self .clear ()
577
531
578
- node_attr_dict = self .node_attr_dict_factory ()
579
- node_attr_dict .node_id = node_id
580
- node_attr_dict .data = doc
532
+ node_dict , _ , _ , _ , _ = get_arangodb_graph (
533
+ self .graph ,
534
+ load_node_dict = True ,
535
+ load_adj_dict = False ,
536
+ load_adj_dict_as_directed = False , # not used
537
+ load_adj_dict_as_multigraph = False , # not used
538
+ load_coo = False ,
539
+ )
581
540
582
- self .data [node_id ] = node_attr_dict
541
+ for node_id , node_data in node_dict .items ():
542
+ node_attr_dict = self .node_attr_dict_factory ()
543
+ node_attr_dict .node_id = node_id
544
+ node_attr_dict .data = build_node_attr_dict_data (node_attr_dict , node_data )
545
+
546
+ self .data [node_id ] = node_attr_dict
583
547
584
548
self .FETCHED_ALL_DATA = True
585
549
@@ -710,43 +674,6 @@ def __delitem__(self, key: str) -> None:
710
674
root_data = self .root .data if self .root else self .data
711
675
root_data ["_rev" ] = doc_update (self .db , self .edge_id , update_dict )
712
676
713
- # @logger_debug
714
- # def __iter__(self) -> Iterator[str]:
715
- # """for key in G._adj['node/1']['node/2']"""
716
- # assert self.edge_id
717
- # yield from aql_doc_get_keys(self.db, self.edge_id)
718
-
719
- # @logger_debug
720
- # def __len__(self) -> int:
721
- # """len(G._adj['node/1']['node/'2])"""
722
- # assert self.edge_id
723
- # return aql_doc_get_length(self.db, self.edge_id)
724
-
725
- # # TODO: Revisit typing of return value
726
- # @logger_debug
727
- # def keys(self) -> Any:
728
- # """G._adj['node/1']['node/'2].keys()"""
729
- # return self.__iter__()
730
-
731
- # # TODO: Revisit typing of return value
732
- # @logger_debug
733
- # def values(self) -> Any:
734
- # """G._adj['node/1']['node/'2].values()"""
735
- # self.data = self.db.document(self.edge_id)
736
- # yield from self.data.values()
737
-
738
- # # TODO: Revisit typing of return value
739
- # @logger_debug
740
- # def items(self) -> Any:
741
- # """G._adj['node/1']['node/'2].items()"""
742
- # self.data = self.db.document(self.edge_id)
743
- # yield from self.data.items()
744
-
745
- # @logger_debug
746
- # def clear(self) -> None:
747
- # """G._adj['node/1']['node/'2].clear()"""
748
- # self.data.clear()
749
-
750
677
@keys_are_strings
751
678
@keys_are_not_reserved
752
679
@logger_debug
@@ -836,6 +763,9 @@ def __contains__(self, key: str) -> bool:
836
763
if dst_node_id in self .data :
837
764
return True
838
765
766
+ if self .FETCHED_ALL_DATA :
767
+ return False
768
+
839
769
result = aql_edge_exists (
840
770
self .db ,
841
771
self .src_node_id ,
@@ -859,6 +789,9 @@ def __getitem__(self, key: str) -> EdgeAttrDict:
859
789
self .data [dst_node_id ] = edge
860
790
return edge # type: ignore # false positive
861
791
792
+ if self .FETCHED_ALL_DATA :
793
+ raise KeyError (key )
794
+
862
795
assert self .src_node_id
863
796
edge = aql_edge_get (
864
797
self .db ,
@@ -1022,8 +955,7 @@ def items(self) -> Any:
1022
955
1023
956
@logger_debug
1024
957
def __fetch_all (self ) -> None :
1025
- if self .FETCHED_ALL_DATA :
1026
- return
958
+ assert self .src_node_id
1027
959
1028
960
self .clear ()
1029
961
@@ -1037,8 +969,7 @@ def __fetch_all(self) -> None:
1037
969
for edge in aql (self .db , query , bind_vars ):
1038
970
edge_attr_dict = self .edge_attr_dict_factory ()
1039
971
edge_attr_dict .edge_id = edge ["_id" ]
1040
- edge_attr_dict .data = edge
1041
-
972
+ edge_attr_dict .data = build_edge_attr_dict_data (edge_attr_dict , edge )
1042
973
self .data [edge ["_to" ]] = edge_attr_dict
1043
974
1044
975
self .FETCHED_ALL_DATA = True
@@ -1100,6 +1031,9 @@ def __contains__(self, key: str) -> bool:
1100
1031
if node_id in self .data :
1101
1032
return True
1102
1033
1034
+ if self .FETCHED_ALL_DATA :
1035
+ return False
1036
+
1103
1037
return bool (self .graph .has_vertex (node_id ))
1104
1038
1105
1039
@key_is_string
@@ -1114,7 +1048,6 @@ def __getitem__(self, key: str) -> AdjListInnerDict:
1114
1048
if self .graph .has_vertex (node_id ):
1115
1049
adjlist_inner_dict : AdjListInnerDict = self .adjlist_inner_dict_factory ()
1116
1050
adjlist_inner_dict .src_node_id = node_id
1117
-
1118
1051
self .data [node_id ] = adjlist_inner_dict
1119
1052
1120
1053
return adjlist_inner_dict
@@ -1237,41 +1170,45 @@ def items(self, data: str | None = None, default: Any | None = None) -> Any:
1237
1170
result = aql_fetch_data_edge (self .db , e_cols , data , default )
1238
1171
yield from result
1239
1172
1240
- # TODO: Revisit this logic
1241
1173
@logger_debug
1242
1174
def __fetch_all (self ) -> None :
1243
- if self .FETCHED_ALL_DATA :
1244
- return
1245
-
1246
1175
self .clear ()
1247
- # items = defaultdict(dict)
1248
- for ed in self .graph .edge_definitions ():
1249
- collection = ed ["edge_collection" ]
1250
1176
1251
- for edge in self .graph .edge_collection (collection ):
1252
- src_node_id = edge ["_from" ]
1253
- dst_node_id = edge ["_to" ]
1177
+ _ , adj_dict , _ , _ , _ = get_arangodb_graph (
1178
+ self .graph ,
1179
+ load_node_dict = False ,
1180
+ load_adj_dict = True ,
1181
+ load_adj_dict_as_directed = False , # TODO: Abstract based on Graph type
1182
+ load_adj_dict_as_multigraph = False , # TODO: Abstract based on Graph type
1183
+ load_coo = False ,
1184
+ )
1185
+
1186
+ for src_node_id , inner_dict in adj_dict .items ():
1187
+ for dst_node_id , edge in inner_dict .items ():
1254
1188
1255
- # items[src_node_id][dst_node_id] = edge
1256
- # items[dst_node_id][src_node_id] = edge
1189
+ if src_node_id in self .data :
1190
+ if dst_node_id in self .data [src_node_id ].data :
1191
+ continue
1257
1192
1258
1193
if src_node_id in self .data :
1259
1194
src_inner_dict = self .data [src_node_id ]
1260
1195
else :
1261
1196
src_inner_dict = self .adjlist_inner_dict_factory ()
1262
1197
src_inner_dict .src_node_id = src_node_id
1198
+ src_inner_dict .FETCHED_ALL_DATA = True
1263
1199
self .data [src_node_id ] = src_inner_dict
1264
1200
1265
1201
if dst_node_id in self .data :
1266
1202
dst_inner_dict = self .data [dst_node_id ]
1267
1203
else :
1268
1204
dst_inner_dict = self .adjlist_inner_dict_factory ()
1269
1205
dst_inner_dict .src_node_id = dst_node_id
1206
+ src_inner_dict .FETCHED_ALL_DATA = True
1270
1207
self .data [dst_node_id ] = dst_inner_dict
1271
1208
1272
1209
edge_attr_dict = src_inner_dict .edge_attr_dict_factory ()
1273
1210
edge_attr_dict .edge_id = edge ["_id" ]
1274
- edge_attr_dict .data = edge
1211
+ edge_attr_dict .data = build_edge_attr_dict_data ( edge_attr_dict , edge )
1275
1212
1276
1213
self .data [src_node_id ].data [dst_node_id ] = edge_attr_dict
1277
1214
self .data [dst_node_id ].data [src_node_id ] = edge_attr_dict
0 commit comments