|
| 1 | +from abc import ABC, abstractmethod |
1 | 2 | from dataclasses import dataclass |
2 | | -from typing import Generic, TypeVar, Union |
| 3 | +from typing import Dict, Generic, List, Set, TypeVar, Union |
3 | 4 |
|
4 | 5 | T = TypeVar("T") |
5 | 6 |
|
@@ -45,52 +46,90 @@ def reload(self): |
45 | 46 | raise NotImplementedError |
46 | 47 |
|
47 | 48 |
|
48 | | -class BaseGraphStorage(StorageNameSpace): |
| 49 | +class BaseGraphStorage(StorageNameSpace, ABC): |
| 50 | + @abstractmethod |
| 51 | + def is_directed(self) -> bool: |
| 52 | + pass |
| 53 | + |
| 54 | + @abstractmethod |
49 | 55 | def has_node(self, node_id: str) -> bool: |
50 | 56 | raise NotImplementedError |
51 | 57 |
|
| 58 | + @abstractmethod |
52 | 59 | def has_edge(self, source_node_id: str, target_node_id: str) -> bool: |
53 | 60 | raise NotImplementedError |
54 | 61 |
|
| 62 | + @abstractmethod |
55 | 63 | def node_degree(self, node_id: str) -> int: |
56 | 64 | raise NotImplementedError |
57 | 65 |
|
58 | | - def edge_degree(self, src_id: str, tgt_id: str) -> int: |
59 | | - raise NotImplementedError |
| 66 | + @abstractmethod |
| 67 | + def get_all_node_degrees(self) -> Dict[str, int]: |
| 68 | + pass |
60 | 69 |
|
| 70 | + def get_isolated_nodes(self) -> List[str]: |
| 71 | + return [ |
| 72 | + node_id |
| 73 | + for node_id, degree in self.get_all_node_degrees().items() |
| 74 | + if degree == 0 |
| 75 | + ] |
| 76 | + |
| 77 | + @abstractmethod |
61 | 78 | def get_node(self, node_id: str) -> Union[dict, None]: |
62 | 79 | raise NotImplementedError |
63 | 80 |
|
| 81 | + @abstractmethod |
64 | 82 | def update_node(self, node_id: str, node_data: dict[str, str]): |
65 | 83 | raise NotImplementedError |
66 | 84 |
|
| 85 | + @abstractmethod |
67 | 86 | def get_all_nodes(self) -> Union[list[tuple[str, dict]], None]: |
68 | 87 | raise NotImplementedError |
69 | 88 |
|
| 89 | + @abstractmethod |
| 90 | + def get_node_count(self) -> int: |
| 91 | + pass |
| 92 | + |
| 93 | + @abstractmethod |
70 | 94 | def get_edge(self, source_node_id: str, target_node_id: str) -> Union[dict, None]: |
71 | 95 | raise NotImplementedError |
72 | 96 |
|
| 97 | + @abstractmethod |
73 | 98 | def update_edge( |
74 | 99 | self, source_node_id: str, target_node_id: str, edge_data: dict[str, str] |
75 | 100 | ): |
76 | 101 | raise NotImplementedError |
77 | 102 |
|
| 103 | + @abstractmethod |
78 | 104 | def get_all_edges(self) -> Union[list[tuple[str, str, dict]], None]: |
79 | 105 | raise NotImplementedError |
80 | 106 |
|
| 107 | + @abstractmethod |
| 108 | + def get_edge_count(self) -> int: |
| 109 | + pass |
| 110 | + |
| 111 | + @abstractmethod |
81 | 112 | def get_node_edges(self, source_node_id: str) -> Union[list[tuple[str, str]], None]: |
82 | 113 | raise NotImplementedError |
83 | 114 |
|
| 115 | + @abstractmethod |
84 | 116 | def upsert_node(self, node_id: str, node_data: dict[str, str]): |
85 | 117 | raise NotImplementedError |
86 | 118 |
|
| 119 | + @abstractmethod |
87 | 120 | def upsert_edge( |
88 | 121 | self, source_node_id: str, target_node_id: str, edge_data: dict[str, str] |
89 | 122 | ): |
90 | 123 | raise NotImplementedError |
91 | 124 |
|
| 125 | + @abstractmethod |
92 | 126 | def delete_node(self, node_id: str): |
93 | 127 | raise NotImplementedError |
94 | 128 |
|
| 129 | + @abstractmethod |
95 | 130 | def reload(self): |
96 | 131 | raise NotImplementedError |
| 132 | + |
| 133 | + @abstractmethod |
| 134 | + def get_connected_components(self, undirected: bool = True) -> List[Set[str]]: |
| 135 | + raise NotImplementedError |
0 commit comments