|
| 1 | +# Copyright 2017 Google LLC All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Helpers for applying Google Cloud Firestore changes in a transaction.""" |
| 16 | + |
| 17 | + |
| 18 | +from google.cloud.firestore_v1 import types |
| 19 | + |
| 20 | +MAX_ATTEMPTS = 5 |
| 21 | +"""int: Default number of transaction attempts (with retries).""" |
| 22 | +_CANT_BEGIN = "The transaction has already begun. Current transaction ID: {!r}." |
| 23 | +_MISSING_ID_TEMPLATE = "The transaction has no transaction ID, so it cannot be {}." |
| 24 | +_CANT_ROLLBACK = _MISSING_ID_TEMPLATE.format("rolled back") |
| 25 | +_CANT_COMMIT = _MISSING_ID_TEMPLATE.format("committed") |
| 26 | +_WRITE_READ_ONLY = "Cannot perform write operation in read-only transaction." |
| 27 | +_INITIAL_SLEEP = 1.0 |
| 28 | +"""float: Initial "max" for sleep interval. To be used in :func:`_sleep`.""" |
| 29 | +_MAX_SLEEP = 30.0 |
| 30 | +"""float: Eventual "max" sleep time. To be used in :func:`_sleep`.""" |
| 31 | +_MULTIPLIER = 2.0 |
| 32 | +"""float: Multiplier for exponential backoff. To be used in :func:`_sleep`.""" |
| 33 | +_EXCEED_ATTEMPTS_TEMPLATE = "Failed to commit transaction in {:d} attempts." |
| 34 | +_CANT_RETRY_READ_ONLY = "Only read-write transactions can be retried." |
| 35 | + |
| 36 | + |
| 37 | +class BaseTransaction(object): |
| 38 | + """Accumulate read-and-write operations to be sent in a transaction. |
| 39 | +
|
| 40 | + Args: |
| 41 | + max_attempts (Optional[int]): The maximum number of attempts for |
| 42 | + the transaction (i.e. allowing retries). Defaults to |
| 43 | + :attr:`~google.cloud.firestore_v1.transaction.MAX_ATTEMPTS`. |
| 44 | + read_only (Optional[bool]): Flag indicating if the transaction |
| 45 | + should be read-only or should allow writes. Defaults to |
| 46 | + :data:`False`. |
| 47 | + """ |
| 48 | + |
| 49 | + def __init__(self, max_attempts=MAX_ATTEMPTS, read_only=False): |
| 50 | + self._max_attempts = max_attempts |
| 51 | + self._read_only = read_only |
| 52 | + self._id = None |
| 53 | + |
| 54 | + def _add_write_pbs(self, write_pbs): |
| 55 | + raise NotImplementedError |
| 56 | + |
| 57 | + def _options_protobuf(self, retry_id): |
| 58 | + """Convert the current object to protobuf. |
| 59 | +
|
| 60 | + The ``retry_id`` value is used when retrying a transaction that |
| 61 | + failed (e.g. due to contention). It is intended to be the "first" |
| 62 | + transaction that failed (i.e. if multiple retries are needed). |
| 63 | +
|
| 64 | + Args: |
| 65 | + retry_id (Union[bytes, NoneType]): Transaction ID of a transaction |
| 66 | + to be retried. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + Optional[google.cloud.firestore_v1.types.TransactionOptions]: |
| 70 | + The protobuf ``TransactionOptions`` if ``read_only==True`` or if |
| 71 | + there is a transaction ID to be retried, else :data:`None`. |
| 72 | +
|
| 73 | + Raises: |
| 74 | + ValueError: If ``retry_id`` is not :data:`None` but the |
| 75 | + transaction is read-only. |
| 76 | + """ |
| 77 | + if retry_id is not None: |
| 78 | + if self._read_only: |
| 79 | + raise ValueError(_CANT_RETRY_READ_ONLY) |
| 80 | + |
| 81 | + return types.TransactionOptions( |
| 82 | + read_write=types.TransactionOptions.ReadWrite( |
| 83 | + retry_transaction=retry_id |
| 84 | + ) |
| 85 | + ) |
| 86 | + elif self._read_only: |
| 87 | + return types.TransactionOptions( |
| 88 | + read_only=types.TransactionOptions.ReadOnly() |
| 89 | + ) |
| 90 | + else: |
| 91 | + return None |
| 92 | + |
| 93 | + @property |
| 94 | + def in_progress(self): |
| 95 | + """Determine if this transaction has already begun. |
| 96 | +
|
| 97 | + Returns: |
| 98 | + bool: Indicates if the transaction has started. |
| 99 | + """ |
| 100 | + return self._id is not None |
| 101 | + |
| 102 | + @property |
| 103 | + def id(self): |
| 104 | + """Get the current transaction ID. |
| 105 | +
|
| 106 | + Returns: |
| 107 | + Optional[bytes]: The transaction ID (or :data:`None` if the |
| 108 | + current transaction is not in progress). |
| 109 | + """ |
| 110 | + return self._id |
| 111 | + |
| 112 | + def _clean_up(self): |
| 113 | + """Clean up the instance after :meth:`_rollback`` or :meth:`_commit``. |
| 114 | +
|
| 115 | + This intended to occur on success or failure of the associated RPCs. |
| 116 | + """ |
| 117 | + self._write_pbs = [] |
| 118 | + self._id = None |
| 119 | + |
| 120 | + def _begin(self, retry_id=None): |
| 121 | + raise NotImplementedError |
| 122 | + |
| 123 | + def _rollback(self): |
| 124 | + raise NotImplementedError |
| 125 | + |
| 126 | + def _commit(self): |
| 127 | + raise NotImplementedError |
| 128 | + |
| 129 | + def get_all(self, references): |
| 130 | + raise NotImplementedError |
| 131 | + |
| 132 | + def get(self, ref_or_query): |
| 133 | + raise NotImplementedError |
| 134 | + |
| 135 | + |
| 136 | +class _BaseTransactional(object): |
| 137 | + """Provide a callable object to use as a transactional decorater. |
| 138 | +
|
| 139 | + This is surfaced via |
| 140 | + :func:`~google.cloud.firestore_v1.transaction.transactional`. |
| 141 | +
|
| 142 | + Args: |
| 143 | + to_wrap (Callable[[:class:`~google.cloud.firestore_v1.transaction.Transaction`, ...], Any]): |
| 144 | + A callable that should be run (and retried) in a transaction. |
| 145 | + """ |
| 146 | + |
| 147 | + def __init__(self, to_wrap): |
| 148 | + self.to_wrap = to_wrap |
| 149 | + self.current_id = None |
| 150 | + """Optional[bytes]: The current transaction ID.""" |
| 151 | + self.retry_id = None |
| 152 | + """Optional[bytes]: The ID of the first attempted transaction.""" |
| 153 | + |
| 154 | + def _reset(self): |
| 155 | + """Unset the transaction IDs.""" |
| 156 | + self.current_id = None |
| 157 | + self.retry_id = None |
| 158 | + |
| 159 | + def _pre_commit(self, transaction, *args, **kwargs): |
| 160 | + raise NotImplementedError |
| 161 | + |
| 162 | + def _maybe_commit(self, transaction): |
| 163 | + raise NotImplementedError |
| 164 | + |
| 165 | + def __call__(self, transaction, *args, **kwargs): |
| 166 | + raise NotImplementedError |
0 commit comments