Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aMahanna committed Oct 11, 2023
1 parent fb1209f commit 875039b
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 320 deletions.
5 changes: 4 additions & 1 deletion adbnx_adapter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
from adbnx_adapter.adapter import ADBNX_Adapter # noqa: F401
from adbnx_adapter.controller import ADBNX_Controller # noqa: F401
from adbnx_adapter.controller import ( # noqa: F401
ADBNX_Controller,
ADBNX_Controller_Full_Cycle,
)
11 changes: 6 additions & 5 deletions adbnx_adapter/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ def networkx_to_arangodb(
nx_graph: NXGraph,
edge_definitions: Optional[List[Json]] = None,
orphan_collections: Optional[List[str]] = None,
keyify_nodes: bool = False,
keyify_edges: bool = False,
overwrite_graph: bool = False,
batch_size: Optional[int] = None,
use_async: bool = False,
Expand All @@ -76,21 +74,24 @@ def _identify_networkx_edge(
nx_edge: NxData,
from_nx_id: NxId,
to_nx_id: NxId,
adb_e_cols: List[str],
nx_map: Dict[NxId, str],
adb_e_cols: List[str],
) -> str:
raise NotImplementedError # pragma: no cover

def _keyify_networkx_node(self, nx_node_id: NxId, nx_node: NxData, col: str) -> str:
def _keyify_networkx_node(
self, i: int, nx_node_id: NxId, nx_node: NxData, col: str
) -> str:
raise NotImplementedError # pragma: no cover

def _keyify_networkx_edge(
self,
i: int,
nx_edge: NxData,
from_nx_id: NxId,
to_nx_id: NxId,
col: str,
nx_map: Dict[NxId, str],
col: str,
) -> str:
raise NotImplementedError # pragma: no cover

Expand Down
89 changes: 35 additions & 54 deletions adbnx_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,6 @@ def networkx_to_arangodb(
nx_graph: NXGraph,
edge_definitions: Optional[List[Json]] = None,
orphan_collections: Optional[List[str]] = None,
keyify_nodes: bool = False,
keyify_edges: bool = False,
overwrite_graph: bool = False,
batch_size: Optional[int] = None,
use_async: bool = False,
Expand All @@ -274,16 +272,6 @@ def networkx_to_arangodb(
:param orphan_collections: A list of vertex collections that will be stored as
orphans in the ArangoDB graph. Can be omitted if the graph already exists.
:type orphan_collections: List[str]
:param keyify_nodes: If set to True, will create custom vertex keys based on the
behavior of ADBNX_Controller._keyify_networkx_node(). Otherwise, ArangoDB
_key values for vertices will range from 1 to N, where N is the number of
NetworkX nodes.
:type keyify_nodes: bool
:param keyify_edges: If set to True, will create custom edge keys based on
the behavior of ADBNX_Controller._keyify_networkx_edge().
Otherwise, ArangoDB _key values for edges will range from 1 to E,
where E is the number of NetworkX edges.
:type keyify_edges: bool
:param overwrite_graph: Overwrites the graph if it already exists.
Does not drop associated collections.
:type overwrite_graph: bool
Expand Down Expand Up @@ -323,9 +311,9 @@ def networkx_to_arangodb(
c["edge_collection"] for c in adb_graph.edge_definitions() # type: ignore
]

has_one_vcol = len(adb_v_cols) == 1
has_one_ecol = len(adb_e_cols) == 1
logger.debug(f"Is graph '{name}' homogeneous? {has_one_vcol and has_one_ecol}")
has_one_v_col = len(adb_v_cols) == 1
has_one_e_col = len(adb_e_cols) == 1
logger.debug(f"Is '{name}' homogeneous? {has_one_v_col and has_one_e_col}")

# This maps NetworkX node IDs to ArangoDB vertex IDs
nx_map: Dict[NxId, str] = dict()
Expand All @@ -341,6 +329,7 @@ def networkx_to_arangodb(

nx_id: NxId
nx_node: NxData

nx_nodes = nx_graph.nodes(data=True)
node_batch_size = batch_size or len(nx_nodes)

Expand All @@ -358,8 +347,7 @@ def networkx_to_arangodb(
nx_map,
adb_docs,
adb_v_cols,
has_one_vcol,
keyify_nodes,
has_one_v_col,
)

bar_progress.advance(bar_progress_task)
Expand All @@ -382,6 +370,7 @@ def networkx_to_arangodb(
from_nx_id: NxId
to_nx_id: NxId
nx_edge: NxData

nx_edges = nx_graph.edges(data=True)
edge_batch_size = batch_size or len(nx_edges)

Expand All @@ -400,8 +389,7 @@ def networkx_to_arangodb(
nx_map,
adb_docs,
adb_e_cols,
has_one_ecol,
keyify_edges,
has_one_e_col,
)

bar_progress.advance(bar_progress_task)
Expand Down Expand Up @@ -610,7 +598,6 @@ def __process_nx_node(
adb_docs: DefaultDict[str, List[Json]],
adb_v_cols: List[str],
has_one_v_col: bool,
keyify_nodes: bool,
) -> None:
"""NetworkX -> ArangoDB: Processes a NetworkX node.
Expand All @@ -628,9 +615,6 @@ def __process_nx_node(
:type adb_v_cols: List[str]
:param has_one_v_col: True if the Graph has one Vertex collection.
:type has_one_v_col: bool
:param keyify_nodes: Create custom vertex keys based on the
behavior of ADBNX_Controller._keyify_networkx_node().
:type keyify_nodes: bool
"""
logger.debug(f"N{i}: {nx_id}")

Expand All @@ -644,16 +628,15 @@ def __process_nx_node(
msg = f"'{nx_id}' identified as '{col}', which is not in {adb_v_cols}"
raise ValueError(msg)

key = (
self.__cntrl._keyify_networkx_node(nx_id, nx_node, col)
if keyify_nodes
else str(i)
)
key = self.__cntrl._keyify_networkx_node(i, nx_id, nx_node, col)

nx_node.update({"_key": key})
self.__cntrl._prepare_networkx_node(nx_node, col)
adb_id = f"{col}/{key}"
nx_node["_id"] = adb_id
nx_node["_key"] = key

nx_map[nx_id] = adb_id

nx_map[nx_id] = f"{col}/{key}"
self.__cntrl._prepare_networkx_node(nx_node, col)
adb_docs[col].append(nx_node)

def __process_nx_edge(
Expand All @@ -665,8 +648,7 @@ def __process_nx_edge(
nx_map: Dict[NxId, str],
adb_documents: DefaultDict[str, List[Json]],
adb_e_cols: List[str],
has_one_ecol: bool,
keyify_edges: bool,
has_one_e_col: bool,
) -> None:
"""NetworkX -> ArangoDB: Processes a NetworkX edge.
Expand All @@ -684,42 +666,41 @@ def __process_nx_edge(
:type adb_documents: DefaultDict[str, List[adbnx_adapter.typings.Json]]
:param adb_e_cols: The ArangoDB edge collections.
:type adb_e_cols: List[str]
:param has_one_ecol: True if the Graph has one Edge collection.
:type has_one_ecol: bool
:param keyify_edges: Create custom edge keys based on the
behavior of ADBNX_Controller._keyify_networkx_edge().
:type keyify_edges: bool
:param has_one_e_col: True if the Graph has one Edge collection.
:type has_one_e_col: bool
"""
edge_str = f"({from_nx_id}, {to_nx_id})"
logger.debug(f"E{i}: {edge_str}")

col = (
adb_e_cols[0]
if has_one_ecol
if has_one_e_col
else self.__cntrl._identify_networkx_edge(
nx_edge, from_nx_id, to_nx_id, adb_e_cols, nx_map
nx_edge,
from_nx_id,
to_nx_id,
nx_map,
adb_e_cols,
)
)

if not has_one_ecol and col not in adb_e_cols:
if not has_one_e_col and col not in adb_e_cols:
msg = f"{edge_str} identified as '{col}', which is not in {adb_e_cols}"
raise ValueError(msg)

key = (
self.__cntrl._keyify_networkx_edge(
nx_edge, from_nx_id, to_nx_id, col, nx_map
)
if keyify_edges
else str(i)
key = self.__cntrl._keyify_networkx_edge(
i,
nx_edge,
from_nx_id,
to_nx_id,
nx_map,
col,
)

nx_edge.update(
{
"_id": col + "/" + key,
"_from": nx_map[from_nx_id],
"_to": nx_map[to_nx_id],
}
)
nx_edge["_id"] = f"{col}/{key}"
nx_edge["_key"] = key
nx_edge["_from"] = nx_map[from_nx_id]
nx_edge["_to"] = nx_map[to_nx_id]

self.__cntrl._prepare_networkx_edge(nx_edge, col)
adb_documents[col].append(nx_edge)
Expand Down
Loading

0 comments on commit 875039b

Please sign in to comment.