forked from googleapis/python-bigtable
/
column_family.py
362 lines (271 loc) · 11.4 KB
/
column_family.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Copyright 2015 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""User friendly container for Google Cloud Bigtable Column Family."""
from google.cloud import _helpers
from google.cloud.bigtable_admin_v2.types import table as table_v2_pb2
from google.cloud.bigtable_admin_v2.types import (
bigtable_table_admin as table_admin_v2_pb2,
)
from google.api_core.gapic_v1.method import DEFAULT
class GarbageCollectionRule(object):
"""Garbage collection rule for column families within a table.
Cells in the column family (within a table) fitting the rule will be
deleted during garbage collection.
.. note::
This class is a do-nothing base class for all GC rules.
.. note::
A string ``gc_expression`` can also be used with API requests, but
that value would be superceded by a ``gc_rule``. As a result, we
don't support that feature and instead support via native classes.
"""
class MaxVersionsGCRule(GarbageCollectionRule):
"""Garbage collection limiting the number of versions of a cell.
For example:
.. literalinclude:: snippets_table.py
:start-after: [START bigtable_api_create_family_gc_max_versions]
:end-before: [END bigtable_api_create_family_gc_max_versions]
:dedent: 4
:type max_num_versions: int
:param max_num_versions: The maximum number of versions
"""
def __init__(self, max_num_versions):
self.max_num_versions = max_num_versions
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return other.max_num_versions == self.max_num_versions
def __ne__(self, other):
return not self == other
def to_pb(self):
"""Converts the garbage collection rule to a protobuf.
:rtype: :class:`.table_v2_pb2.GcRule`
:returns: The converted current object.
"""
return table_v2_pb2.GcRule(max_num_versions=self.max_num_versions)
class MaxAgeGCRule(GarbageCollectionRule):
"""Garbage collection limiting the age of a cell.
For example:
.. literalinclude:: snippets_table.py
:start-after: [START bigtable_api_create_family_gc_max_age]
:end-before: [END bigtable_api_create_family_gc_max_age]
:dedent: 4
:type max_age: :class:`datetime.timedelta`
:param max_age: The maximum age allowed for a cell in the table.
"""
def __init__(self, max_age):
self.max_age = max_age
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return other.max_age == self.max_age
def __ne__(self, other):
return not self == other
def to_pb(self):
"""Converts the garbage collection rule to a protobuf.
:rtype: :class:`.table_v2_pb2.GcRule`
:returns: The converted current object.
"""
max_age = _helpers._timedelta_to_duration_pb(self.max_age)
return table_v2_pb2.GcRule(max_age=max_age)
class GCRuleUnion(GarbageCollectionRule):
"""Union of garbage collection rules.
For example:
.. literalinclude:: snippets_table.py
:start-after: [START bigtable_api_create_family_gc_union]
:end-before: [END bigtable_api_create_family_gc_union]
:dedent: 4
:type rules: list
:param rules: List of :class:`GarbageCollectionRule`.
"""
def __init__(self, rules):
self.rules = rules
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return other.rules == self.rules
def __ne__(self, other):
return not self == other
def to_pb(self):
"""Converts the union into a single GC rule as a protobuf.
:rtype: :class:`.table_v2_pb2.GcRule`
:returns: The converted current object.
"""
union = table_v2_pb2.GcRule.Union(rules=[rule.to_pb() for rule in self.rules])
return table_v2_pb2.GcRule(union=union)
class GCRuleIntersection(GarbageCollectionRule):
"""Intersection of garbage collection rules.
For example:
.. literalinclude:: snippets_table.py
:start-after: [START bigtable_api_create_family_gc_intersection]
:end-before: [END bigtable_api_create_family_gc_intersection]
:dedent: 4
:type rules: list
:param rules: List of :class:`GarbageCollectionRule`.
"""
def __init__(self, rules):
self.rules = rules
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return other.rules == self.rules
def __ne__(self, other):
return not self == other
def to_pb(self):
"""Converts the intersection into a single GC rule as a protobuf.
:rtype: :class:`.table_v2_pb2.GcRule`
:returns: The converted current object.
"""
intersection = table_v2_pb2.GcRule.Intersection(
rules=[rule.to_pb() for rule in self.rules]
)
return table_v2_pb2.GcRule(intersection=intersection)
class ColumnFamily(object):
"""Representation of a Google Cloud Bigtable Column Family.
We can use a :class:`ColumnFamily` to:
* :meth:`create` itself
* :meth:`update` itself
* :meth:`delete` itself
:type column_family_id: str
:param column_family_id: The ID of the column family. Must be of the
form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
:type table: :class:`Table <google.cloud.bigtable.deprecated.table.Table>`
:param table: The table that owns the column family.
:type gc_rule: :class:`GarbageCollectionRule`
:param gc_rule: (Optional) The garbage collection settings for this
column family.
"""
def __init__(self, column_family_id, table, gc_rule=None):
self.column_family_id = column_family_id
self._table = table
self.gc_rule = gc_rule
@property
def name(self):
"""Column family name used in requests.
For example:
.. literalinclude:: snippets_table.py
:start-after: [START bigtable_api_column_family_name]
:end-before: [END bigtable_api_column_family_name]
:dedent: 4
.. note::
This property will not change if ``column_family_id`` does not, but
the return value is not cached.
The Column family name is of the form
``"projects/../zones/../clusters/../tables/../columnFamilies/.."``
:rtype: str
:returns: The column family name.
"""
return self._table.name + "/columnFamilies/" + self.column_family_id
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return (
other.column_family_id == self.column_family_id
and other._table == self._table
and other.gc_rule == self.gc_rule
)
def __ne__(self, other):
return not self == other
def to_pb(self):
"""Converts the column family to a protobuf.
:rtype: :class:`.table_v2_pb2.ColumnFamily`
:returns: The converted current object.
"""
if self.gc_rule is None:
return table_v2_pb2.ColumnFamily()
else:
return table_v2_pb2.ColumnFamily(gc_rule=self.gc_rule.to_pb())
def create(self):
"""Create this column family.
For example:
.. literalinclude:: snippets_table.py
:start-after: [START bigtable_api_create_column_family]
:end-before: [END bigtable_api_create_column_family]
:dedent: 4
"""
column_family = self.to_pb()
modification = table_admin_v2_pb2.ModifyColumnFamiliesRequest.Modification(
id=self.column_family_id, create=column_family
)
client = self._table._instance._client
# data it contains are the GC rule and the column family ID already
# stored on this instance.
client.table_admin_client.modify_column_families(
request={"name": self._table.name, "modifications": [modification]},
timeout=DEFAULT,
)
def update(self):
"""Update this column family.
For example:
.. literalinclude:: snippets_table.py
:start-after: [START bigtable_api_update_column_family]
:end-before: [END bigtable_api_update_column_family]
:dedent: 4
.. note::
Only the GC rule can be updated. By changing the column family ID,
you will simply be referring to a different column family.
"""
column_family = self.to_pb()
modification = table_admin_v2_pb2.ModifyColumnFamiliesRequest.Modification(
id=self.column_family_id, update=column_family
)
client = self._table._instance._client
# data it contains are the GC rule and the column family ID already
# stored on this instance.
client.table_admin_client.modify_column_families(
request={"name": self._table.name, "modifications": [modification]},
timeout=DEFAULT,
)
def delete(self):
"""Delete this column family.
For example:
.. literalinclude:: snippets_table.py
:start-after: [START bigtable_api_delete_column_family]
:end-before: [END bigtable_api_delete_column_family]
:dedent: 4
"""
modification = table_admin_v2_pb2.ModifyColumnFamiliesRequest.Modification(
id=self.column_family_id, drop=True
)
client = self._table._instance._client
# data it contains are the GC rule and the column family ID already
# stored on this instance.
client.table_admin_client.modify_column_families(
request={"name": self._table.name, "modifications": [modification]},
timeout=DEFAULT,
)
def _gc_rule_from_pb(gc_rule_pb):
"""Convert a protobuf GC rule to a native object.
:type gc_rule_pb: :class:`.table_v2_pb2.GcRule`
:param gc_rule_pb: The GC rule to convert.
:rtype: :class:`GarbageCollectionRule` or :data:`NoneType <types.NoneType>`
:returns: An instance of one of the native rules defined
in :module:`column_family` or :data:`None` if no values were
set on the protobuf passed in.
:raises: :class:`ValueError <exceptions.ValueError>` if the rule name
is unexpected.
"""
rule_name = gc_rule_pb._pb.WhichOneof("rule")
if rule_name is None:
return None
if rule_name == "max_num_versions":
return MaxVersionsGCRule(gc_rule_pb.max_num_versions)
elif rule_name == "max_age":
return MaxAgeGCRule(gc_rule_pb.max_age)
elif rule_name == "union":
return GCRuleUnion([_gc_rule_from_pb(rule) for rule in gc_rule_pb.union.rules])
elif rule_name == "intersection":
rules = [_gc_rule_from_pb(rule) for rule in gc_rule_pb.intersection.rules]
return GCRuleIntersection(rules)
else:
raise ValueError("Unexpected rule name", rule_name)