|
15 | 15 | """User friendly container for Google Cloud Bigtable Column Family.""" |
16 | 16 |
|
17 | 17 |
|
18 | | -import datetime |
19 | | - |
20 | | -from google.protobuf import duration_pb2 |
21 | | - |
| 18 | +from google.cloud import _helpers |
22 | 19 | from google.cloud.bigtable._generated import ( |
23 | 20 | table_pb2 as table_v2_pb2) |
24 | 21 | from google.cloud.bigtable._generated import ( |
25 | 22 | bigtable_table_admin_pb2 as table_admin_v2_pb2) |
26 | 23 |
|
27 | 24 |
|
28 | | -def _timedelta_to_duration_pb(timedelta_val): |
29 | | - """Convert a Python timedelta object to a duration protobuf. |
30 | | -
|
31 | | - .. note:: |
32 | | -
|
33 | | - The Python timedelta has a granularity of microseconds while |
34 | | - the protobuf duration type has a duration of nanoseconds. |
35 | | -
|
36 | | - :type timedelta_val: :class:`datetime.timedelta` |
37 | | - :param timedelta_val: A timedelta object. |
38 | | -
|
39 | | - :rtype: :class:`google.protobuf.duration_pb2.Duration` |
40 | | - :returns: A duration object equivalent to the time delta. |
41 | | - """ |
42 | | - seconds_decimal = timedelta_val.total_seconds() |
43 | | - # Truncate the parts other than the integer. |
44 | | - seconds = int(seconds_decimal) |
45 | | - if seconds_decimal < 0: |
46 | | - signed_micros = timedelta_val.microseconds - 10**6 |
47 | | - else: |
48 | | - signed_micros = timedelta_val.microseconds |
49 | | - # Convert nanoseconds to microseconds. |
50 | | - nanos = 1000 * signed_micros |
51 | | - return duration_pb2.Duration(seconds=seconds, nanos=nanos) |
52 | | - |
53 | | - |
54 | | -def _duration_pb_to_timedelta(duration_pb): |
55 | | - """Convert a duration protobuf to a Python timedelta object. |
56 | | -
|
57 | | - .. note:: |
58 | | -
|
59 | | - The Python timedelta has a granularity of microseconds while |
60 | | - the protobuf duration type has a duration of nanoseconds. |
61 | | -
|
62 | | - :type duration_pb: :class:`google.protobuf.duration_pb2.Duration` |
63 | | - :param duration_pb: A protobuf duration object. |
64 | | -
|
65 | | - :rtype: :class:`datetime.timedelta` |
66 | | - :returns: The converted timedelta object. |
67 | | - """ |
68 | | - return datetime.timedelta( |
69 | | - seconds=duration_pb.seconds, |
70 | | - microseconds=(duration_pb.nanos / 1000.0), |
71 | | - ) |
72 | | - |
73 | | - |
74 | 25 | class GarbageCollectionRule(object): |
75 | 26 | """Garbage collection rule for column families within a table. |
76 | 27 |
|
@@ -137,7 +88,7 @@ def to_pb(self): |
137 | 88 | :rtype: :class:`.table_v2_pb2.GcRule` |
138 | 89 | :returns: The converted current object. |
139 | 90 | """ |
140 | | - max_age = _timedelta_to_duration_pb(self.max_age) |
| 91 | + max_age = _helpers._timedelta_to_duration_pb(self.max_age) |
141 | 92 | return table_v2_pb2.GcRule(max_age=max_age) |
142 | 93 |
|
143 | 94 |
|
@@ -325,7 +276,7 @@ def _gc_rule_from_pb(gc_rule_pb): |
325 | 276 | if rule_name == 'max_num_versions': |
326 | 277 | return MaxVersionsGCRule(gc_rule_pb.max_num_versions) |
327 | 278 | elif rule_name == 'max_age': |
328 | | - max_age = _duration_pb_to_timedelta(gc_rule_pb.max_age) |
| 279 | + max_age = _helpers._duration_pb_to_timedelta(gc_rule_pb.max_age) |
329 | 280 | return MaxAgeGCRule(max_age) |
330 | 281 | elif rule_name == 'union': |
331 | 282 | return GCRuleUnion([_gc_rule_from_pb(rule) |
|
0 commit comments