|
| 1 | +from abc import ABC |
| 2 | +from typing import Any |
| 3 | +from typing import Dict |
| 4 | +from typing import Tuple |
| 5 | +from typing import Callable |
| 6 | +from typing import Iterable |
| 7 | +from typing import Optional |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | +from .serializers import Serializer |
| 11 | +from .serializers import UInt64String |
| 12 | + |
| 13 | + |
| 14 | +class SimpleClient(ABC): |
| 15 | + from abc import abstractmethod |
| 16 | + |
| 17 | + """ |
| 18 | + Abstract class for interacting with backend data storage system. |
| 19 | + Eg., BigTableClient for using big table as storage. |
| 20 | + """ |
| 21 | + |
| 22 | + @abstractmethod |
| 23 | + def create_table(self) -> None: |
| 24 | + """Initialize the table and store associated meta.""" |
| 25 | + |
| 26 | + @abstractmethod |
| 27 | + def write_metadata(self, metadata): |
| 28 | + """Update stored metadata.""" |
| 29 | + |
| 30 | + @abstractmethod |
| 31 | + def read_metadata(self): |
| 32 | + """Read stored metadata.""" |
| 33 | + |
| 34 | + @abstractmethod |
| 35 | + def read_entries( |
| 36 | + self, |
| 37 | + entry_ids, |
| 38 | + properties=None, |
| 39 | + start_time=None, |
| 40 | + end_time=None, |
| 41 | + end_time_inclusive=False, |
| 42 | + ): |
| 43 | + """Read entries and their properties.""" |
| 44 | + |
| 45 | + @abstractmethod |
| 46 | + def read_entry( |
| 47 | + self, |
| 48 | + entry_id, |
| 49 | + properties=None, |
| 50 | + start_time=None, |
| 51 | + end_time=None, |
| 52 | + end_time_inclusive=False, |
| 53 | + ): |
| 54 | + """Read a single entry and it's properties.""" |
| 55 | + |
| 56 | + @abstractmethod |
| 57 | + def write_entries(self, entries): |
| 58 | + """Writes/updates entries (IDs along with properties).""" |
| 59 | + |
| 60 | + |
| 61 | +class EntryKey: |
| 62 | + def __init__(self, key: Any, serializer: Optional[Serializer] = UInt64String()): |
| 63 | + self._key = key |
| 64 | + self._serializer = serializer |
| 65 | + |
| 66 | + def serialize(self) -> Any: |
| 67 | + return self._serializer(self._key) |
| 68 | + |
| 69 | + |
| 70 | +class Entry: |
| 71 | + """ |
| 72 | + Represents a single entry/record/entity in the database. |
| 73 | + """ |
| 74 | + |
| 75 | + def __init__( |
| 76 | + self, |
| 77 | + key: EntryKey, |
| 78 | + val_dict: Dict[Any, Any], |
| 79 | + timestamp: Optional[datetime] = None, |
| 80 | + ): |
| 81 | + self._key = key |
| 82 | + self._val_dict = val_dict |
| 83 | + self._timestamp = timestamp |
| 84 | + |
| 85 | + @property |
| 86 | + def key(self) -> Any: |
| 87 | + return self._key.serialize() |
| 88 | + |
| 89 | + @property |
| 90 | + def values(self) -> Dict: |
| 91 | + return self._val_dict |
| 92 | + |
| 93 | + @property |
| 94 | + def timestamp(self): |
| 95 | + return self._timestamp |
0 commit comments