File size: 3,593 Bytes
7566ac3
acd7cf4
7566ac3
fb9c306
acd7cf4
 
fb9c306
acd7cf4
 
 
 
 
9e67c3b
acd7cf4
 
9e67c3b
acd7cf4
 
 
fb9c306
9e67c3b
acd7cf4
 
9e67c3b
acd7cf4
 
9e67c3b
acd7cf4
 
 
 
9e67c3b
283e483
 
9e67c3b
acd7cf4
 
 
9e67c3b
acd7cf4
 
9e67c3b
acd7cf4
 
31086ae
 
 
fb9c306
7566ac3
 
 
 
 
 
9e67c3b
acd7cf4
 
7566ac3
9e67c3b
acd7cf4
 
7566ac3
9e67c3b
acd7cf4
 
7566ac3
 
 
acd7cf4
7566ac3
 
 
 
 
 
 
 
9e67c3b
acd7cf4
 
7566ac3
9e67c3b
acd7cf4
 
7566ac3
9e67c3b
acd7cf4
 
7566ac3
 
 
 
 
9e67c3b
acd7cf4
 
7566ac3
9e67c3b
fb9c306
 
acd7cf4
 
7566ac3
9e67c3b
acd7cf4
 
7566ac3
 
 
 
 
9e67c3b
acd7cf4
 
7566ac3
9e67c3b
acd7cf4
 
7566ac3
9e67c3b
acd7cf4
 
 
 
7566ac3
9e67c3b
acd7cf4
31086ae
7566ac3
31086ae
 
7566ac3
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Dict, Generic, List, Set, TypeVar, Union

T = TypeVar("T")


@dataclass
class StorageNameSpace:
    working_dir: str = None
    namespace: str = None

    def index_done_callback(self):
        """commit the storage operations after indexing"""

    def query_done_callback(self):
        """commit the storage operations after querying"""


class BaseKVStorage(Generic[T], StorageNameSpace):
    def all_keys(self) -> list[str]:
        raise NotImplementedError

    def get_by_id(self, id: str) -> Union[T, None]:
        raise NotImplementedError

    def get_by_ids(
        self, ids: list[str], fields: Union[set[str], None] = None
    ) -> list[Union[T, None]]:
        raise NotImplementedError

    def get_all(self) -> dict[str, T]:
        raise NotImplementedError

    def filter_keys(self, data: list[str]) -> set[str]:
        """return un-exist keys"""
        raise NotImplementedError

    def upsert(self, data: dict[str, T]):
        raise NotImplementedError

    def drop(self):
        raise NotImplementedError

    def reload(self):
        raise NotImplementedError


class BaseGraphStorage(StorageNameSpace, ABC):
    @abstractmethod
    def is_directed(self) -> bool:
        pass

    @abstractmethod
    def has_node(self, node_id: str) -> bool:
        raise NotImplementedError

    @abstractmethod
    def has_edge(self, source_node_id: str, target_node_id: str) -> bool:
        raise NotImplementedError

    @abstractmethod
    def node_degree(self, node_id: str) -> int:
        raise NotImplementedError

    @abstractmethod
    def get_all_node_degrees(self) -> Dict[str, int]:
        pass

    def get_isolated_nodes(self) -> List[str]:
        return [
            node_id
            for node_id, degree in self.get_all_node_degrees().items()
            if degree == 0
        ]

    @abstractmethod
    def get_node(self, node_id: str) -> Union[dict, None]:
        raise NotImplementedError

    @abstractmethod
    def update_node(self, node_id: str, node_data: dict[str, str]):
        raise NotImplementedError

    @abstractmethod
    def get_all_nodes(self) -> Union[list[tuple[str, dict]], None]:
        raise NotImplementedError

    @abstractmethod
    def get_node_count(self) -> int:
        pass

    @abstractmethod
    def get_edge(self, source_node_id: str, target_node_id: str) -> Union[dict, None]:
        raise NotImplementedError

    @abstractmethod
    def update_edge(
        self, source_node_id: str, target_node_id: str, edge_data: dict[str, str]
    ):
        raise NotImplementedError

    @abstractmethod
    def get_all_edges(self) -> Union[list[tuple[str, str, dict]], None]:
        raise NotImplementedError

    @abstractmethod
    def get_edge_count(self) -> int:
        pass

    @abstractmethod
    def get_node_edges(self, source_node_id: str) -> Union[list[tuple[str, str]], None]:
        raise NotImplementedError

    @abstractmethod
    def upsert_node(self, node_id: str, node_data: dict[str, str]):
        raise NotImplementedError

    @abstractmethod
    def upsert_edge(
        self, source_node_id: str, target_node_id: str, edge_data: dict[str, str]
    ):
        raise NotImplementedError

    @abstractmethod
    def delete_node(self, node_id: str):
        raise NotImplementedError

    @abstractmethod
    def reload(self):
        raise NotImplementedError

    @abstractmethod
    def get_connected_components(self, undirected: bool = True) -> List[Set[str]]:
        raise NotImplementedError