|
| 1 | +""" |
| 2 | +Sub packages/modules for backend storage clients. |
| 3 | +Supports Google BigTable and Apache HBase. |
| 4 | +
|
| 5 | +A simple client needs to be able to create the table, |
| 6 | +store table meta and to write and read node information. |
| 7 | +Also needs locking support to prevent race conditions |
| 8 | +when modifying root/parent nodes. |
| 9 | +
|
| 10 | +In addition, clients with more features like generating unique IDs |
| 11 | +and logging facilities can be implemented by inherting respective base classes. |
| 12 | +
|
| 13 | +These methods are in separate classes because they are logically related. |
| 14 | +This also makes it possible to have different backend storage solutions, |
| 15 | +making it possible to use any unique features these solutions may provide. |
| 16 | +
|
| 17 | +Please see `base.py` for more details. |
| 18 | +""" |
| 19 | + |
1 | 20 | from collections import namedtuple |
| 21 | +from os import environ |
| 22 | +from typing import Union |
2 | 23 |
|
| 24 | +from .base import ColumnFamilyConfig, DEFAULT_COLUMN_FAMILIES |
| 25 | +from .bigtable import BigTableConfig |
| 26 | +from .bigtable import get_client_info as get_bigtable_client_info |
3 | 27 | from .bigtable.client import Client as BigTableClient |
4 | | -from .bigtable.attributes import Attribute |
5 | | -from .serializers import Serializer |
6 | | -from .serializers import UInt64String |
| 28 | +from .hbase import HBaseConfig |
| 29 | +from .hbase import get_client_info as get_hbase_client_info |
| 30 | +from .hbase.client import Client as HBaseClient |
| 31 | + |
| 32 | +ClientType = Union[BigTableClient, HBaseClient] |
7 | 33 |
|
8 | 34 |
|
9 | 35 | _backend_clientinfo_fields = ("TYPE", "CONFIG") |
10 | | -_backend_clientinfo_defaults = (None, None) |
| 36 | +_backend_clientinfo_defaults = ("bigtable", None) |
11 | 37 | BackendClientInfo = namedtuple( |
12 | 38 | "BackendClientInfo", |
13 | 39 | _backend_clientinfo_fields, |
14 | 40 | defaults=_backend_clientinfo_defaults, |
15 | 41 | ) |
16 | 42 |
|
17 | 43 |
|
| 44 | +def get_client_class(backend_type: str = "bigtable"): |
| 45 | + """Return the client class for the given backend type.""" |
| 46 | + backend_type = (backend_type or "bigtable").lower() |
| 47 | + if backend_type == "bigtable": |
| 48 | + return BigTableClient |
| 49 | + elif backend_type == "hbase": |
| 50 | + return HBaseClient |
| 51 | + else: |
| 52 | + raise ValueError(f"Unknown backend type: {backend_type}") |
| 53 | + |
| 54 | + |
18 | 55 | def get_default_client_info(): |
19 | 56 | """ |
20 | 57 | Load client from env variables. |
21 | 58 | """ |
22 | | - |
23 | | - # TODO make dynamic after multiple platform support is added |
24 | | - from .bigtable import get_client_config as get_bigtable_client_config |
| 59 | + backend_type = environ.get("PCG_BACKEND_TYPE", "bigtable").lower() |
| 60 | + if backend_type == "hbase": |
| 61 | + return BackendClientInfo( |
| 62 | + TYPE="hbase", CONFIG=get_hbase_client_info() |
| 63 | + ) |
25 | 64 |
|
26 | 65 | return BackendClientInfo( |
27 | | - CONFIG=get_bigtable_client_config(admin=True, read_only=False) |
| 66 | + TYPE="bigtable", CONFIG=get_bigtable_client_info(admin=True, read_only=False) |
28 | 67 | ) |
0 commit comments