Skip to content

Commit

Permalink
made base class for table and authorized_view
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche committed Oct 31, 2024
1 parent ec5f462 commit 9b6cadf
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
4 changes: 2 additions & 2 deletions google/cloud/bigtable/data/_async/_mutate_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
BigtableAsyncClient,
)
from google.cloud.bigtable.data.mutations import RowMutationEntry
from google.cloud.bigtable.data._async.client import TableAsync
from google.cloud.bigtable.data._async.client import _ApiSurfaceAsync


@dataclass
Expand Down Expand Up @@ -69,7 +69,7 @@ class _MutateRowsOperationAsync:
def __init__(
self,
gapic_client: "BigtableAsyncClient",
table: "TableAsync",
table: "_ApiSurfaceAsync",
mutation_entries: list["RowMutationEntry"],
operation_timeout: float,
attempt_timeout: float | None,
Expand Down
4 changes: 2 additions & 2 deletions google/cloud/bigtable/data/_async/_read_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from google.api_core.retry import exponential_sleep_generator

if TYPE_CHECKING:
from google.cloud.bigtable.data._async.client import TableAsync
from google.cloud.bigtable.data._async.client import _ApiSurfaceAsync


class _ResetRow(Exception):
Expand Down Expand Up @@ -82,7 +82,7 @@ class _ReadRowsOperationAsync:
def __init__(
self,
query: ReadRowsQuery,
table: "TableAsync",
table: "_ApiSurfaceAsync",
operation_timeout: float,
attempt_timeout: float,
retryable_exceptions: Sequence[type[Exception]] = (),
Expand Down
45 changes: 35 additions & 10 deletions google/cloud/bigtable/data/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ async def _manage_channel(
next_sleep = next_refresh - (time.time() - start_timestamp)

async def _register_instance(
self, instance_id: str, owner: Union[TableAsync, ExecuteQueryIteratorAsync]
self,
instance_id: str,
owner: Union[_ApiSurfaceAsync, ExecuteQueryIteratorAsync],
) -> None:
"""
Registers an instance with the client, and warms the channel pool
Expand Down Expand Up @@ -366,7 +368,9 @@ async def _register_instance(
self._start_background_channel_refresh()

async def _remove_instance_registration(
self, instance_id: str, owner: Union[TableAsync, ExecuteQueryIteratorAsync]
self,
instance_id: str,
owner: Union[_ApiSurfaceAsync, ExecuteQueryIteratorAsync],
) -> bool:
"""
Removes an instance from the client's registered instances, to prevent
Expand Down Expand Up @@ -479,7 +483,12 @@ def get_authorized_view(
RuntimeError: If called outside an async context (no running event loop)
"""
return AuthorizedViewAsync(
self, instance_id, table_id, view_id, *args, **kwargs,
self,
instance_id,
table_id,
view_id,
*args,
**kwargs,
)

async def execute_query(
Expand Down Expand Up @@ -587,12 +596,11 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
await self._gapic_client.__aexit__(exc_type, exc_val, exc_tb)


class TableAsync:
class _ApiSurfaceAsync:
"""
Main Data API surface
Abstract class containing API surface for BigtableDataClient. Should not be created directly
Table object maintains table_id, and app_profile_id context, and passes them with
each call
Can be instantiated as a TableAsync, or an AuthorizedViewAsync
"""

def __init__(
Expand Down Expand Up @@ -623,8 +631,6 @@ def __init__(
),
):
"""
Initialize a Table instance
Must be created within an async context (running event loop)
Args:
Expand Down Expand Up @@ -1429,7 +1435,26 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.close()


class AuthorizedViewAsync(TableAsync):
class TableAsync(_ApiSurfaceAsync):
"""
Main Data API surface for interacting with a Bigtable table.
Table object maintains table_id, and app_profile_id context, and passes them with
each call
"""


class AuthorizedViewAsync(_ApiSurfaceAsync):
"""
Provides access to an authorized view of a table.
An authorized view is a subset of a table that you configure to include specific table data.
Then you grant access to the authorized view separately from access to the table.
AuthorizedView object maintains table_id, app_profile_id, and authorized_view_id context,
and passed them with each call
"""

def __init__(
self, client, instance_id, table_id, view_id, app_profile_id, **kwargs
):
Expand Down
4 changes: 3 additions & 1 deletion google/cloud/bigtable/data/_async/mutations_batcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

if TYPE_CHECKING:
from google.cloud.bigtable.data._async.client import TableAsync
from google.cloud.bigtable.data._async.client import AuthorizedViewAsync
from google.cloud.bigtable.data._async.client import _ApiSurfaceAsync

# used to make more readable default values
_MB_SIZE = 1024 * 1024
Expand Down Expand Up @@ -199,7 +201,7 @@ class MutationsBatcherAsync:

def __init__(
self,
table: "TableAsync",
table: "TableAsync" | "AuthorizedViewAsync" | "_ApiSurfaceAsync",
*,
flush_interval: float | None = 5,
flush_limit_mutation_count: int | None = 1000,
Expand Down
6 changes: 3 additions & 3 deletions google/cloud/bigtable/data/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

if TYPE_CHECKING:
import grpc
from google.cloud.bigtable.data import TableAsync
from google.cloud.bigtable.data._async.client import _ApiSurfaceAsync

"""
Helper functions used in various places in the library.
Expand Down Expand Up @@ -145,7 +145,7 @@ def _retry_exception_factory(
def _get_timeouts(
operation: float | TABLE_DEFAULT,
attempt: float | None | TABLE_DEFAULT,
table: "TableAsync",
table: "_ApiSurfaceAsync",
) -> tuple[float, float]:
"""
Convert passed in timeout values to floats, using table defaults if necessary.
Expand Down Expand Up @@ -232,7 +232,7 @@ def _get_error_type(

def _get_retryable_errors(
call_codes: Sequence["grpc.StatusCode" | int | type[Exception]] | TABLE_DEFAULT,
table: "TableAsync",
table: "_ApiSurfaceAsync",
) -> list[type[Exception]]:
"""
Convert passed in retryable error codes to a list of exception types.
Expand Down

0 comments on commit 9b6cadf

Please sign in to comment.