|
14 | 14 | # limitations under the License. |
15 | 15 | # |
16 | 16 |
|
17 | | -import math |
18 | | -from typing import Union |
| 17 | +from enum import IntEnum |
| 18 | +from typing import Union, Type |
19 | 19 |
|
20 | 20 | from pygridgain.api.tx_api import tx_end, tx_start, tx_end_async, tx_start_async |
21 | 21 | from pygridgain.datatypes import TransactionIsolation, TransactionConcurrency |
22 | 22 | from pygridgain.exceptions import CacheError |
23 | 23 | from pygridgain.utils import status_to_exception |
24 | 24 |
|
25 | 25 |
|
26 | | -def _convert_to_millis(timeout: Union[int, float]) -> int: |
27 | | - if isinstance(timeout, float): |
28 | | - return math.floor(timeout * 1000) |
29 | | - return timeout |
| 26 | +def _validate_int_enum_param(value: Union[int, IntEnum], cls: Type[IntEnum]): |
| 27 | + if value not in cls: |
| 28 | + raise ValueError(f'{value} not in {cls}') |
| 29 | + return value |
30 | 30 |
|
31 | 31 |
|
32 | | -class Transaction: |
| 32 | +def _validate_timeout(value): |
| 33 | + if not isinstance(value, int) or value < 0: |
| 34 | + raise ValueError(f'Timeout value should be a positive integer, {value} passed instead') |
| 35 | + return value |
| 36 | + |
| 37 | + |
| 38 | +def _validate_label(value): |
| 39 | + if value and not isinstance(value, str): |
| 40 | + raise ValueError(f'Label should be str, {type(value)} passed instead') |
| 41 | + return value |
| 42 | + |
| 43 | + |
| 44 | +class _BaseTransaction: |
| 45 | + def __init__(self, client, concurrency=TransactionConcurrency.PESSIMISTIC, |
| 46 | + isolation=TransactionIsolation.REPEATABLE_READ, timeout=0, label=None): |
| 47 | + self.client = client |
| 48 | + self.concurrency = _validate_int_enum_param(concurrency, TransactionConcurrency) |
| 49 | + self.isolation = _validate_int_enum_param(isolation, TransactionIsolation) |
| 50 | + self.timeout = _validate_timeout(timeout) |
| 51 | + self.label, self.closed = _validate_label(label), False |
| 52 | + |
| 53 | + |
| 54 | +class Transaction(_BaseTransaction): |
33 | 55 | """ |
34 | 56 | Thin client transaction. |
35 | 57 | """ |
36 | 58 | def __init__(self, client, concurrency=TransactionConcurrency.PESSIMISTIC, |
37 | 59 | isolation=TransactionIsolation.REPEATABLE_READ, timeout=0, label=None): |
38 | | - self.client, self.concurrency = client, concurrency |
39 | | - self.isolation, self.timeout = isolation, _convert_to_millis(timeout) |
40 | | - self.label, self.closed = label, False |
| 60 | + super().__init__(client, concurrency, isolation, timeout, label) |
41 | 61 | self.tx_id = self.__start_tx() |
42 | 62 |
|
43 | 63 | def commit(self) -> None: |
@@ -78,15 +98,13 @@ def __end_tx(self, committed): |
78 | 98 | return tx_end(self.tx_id, committed) |
79 | 99 |
|
80 | 100 |
|
81 | | -class AioTransaction: |
| 101 | +class AioTransaction(_BaseTransaction): |
82 | 102 | """ |
83 | 103 | Async thin client transaction. |
84 | 104 | """ |
85 | 105 | def __init__(self, client, concurrency=TransactionConcurrency.PESSIMISTIC, |
86 | 106 | isolation=TransactionIsolation.REPEATABLE_READ, timeout=0, label=None): |
87 | | - self.client, self.concurrency = client, concurrency |
88 | | - self.isolation, self.timeout = isolation, _convert_to_millis(timeout) |
89 | | - self.label, self.closed = label, False |
| 107 | + super().__init__(client, concurrency, isolation, timeout, label) |
90 | 108 |
|
91 | 109 | def __await__(self): |
92 | 110 | return (yield from self.__aenter__().__await__()) |
|
0 commit comments