Skip to content

Commit ae3d818

Browse files
authored
Merge pull request #1913 from tseaver/bigtable-v2-column_family_crud
Update bigtable.column_family to use V2 protos
2 parents cdbc47c + 7e9c61d commit ae3d818

File tree

2 files changed

+125
-104
lines changed

2 files changed

+125
-104
lines changed

gcloud/bigtable/column_family.py

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
from google.protobuf import duration_pb2
2121

2222
from gcloud._helpers import _total_seconds
23-
from gcloud.bigtable._generated import (
24-
bigtable_table_data_pb2 as data_v1_pb2)
25-
from gcloud.bigtable._generated import (
26-
bigtable_table_service_messages_pb2 as messages_v1_pb2)
23+
from gcloud.bigtable._generated_v2 import (
24+
table_pb2 as table_v2_pb2)
25+
from gcloud.bigtable._generated_v2 import (
26+
bigtable_table_admin_pb2 as table_admin_v2_pb2)
2727

2828

2929
def _timedelta_to_duration_pb(timedelta_val):
@@ -111,10 +111,10 @@ def __eq__(self, other):
111111
def to_pb(self):
112112
"""Converts the garbage collection rule to a protobuf.
113113
114-
:rtype: :class:`.data_v1_pb2.GcRule`
114+
:rtype: :class:`.table_v2_pb2.GcRule`
115115
:returns: The converted current object.
116116
"""
117-
return data_v1_pb2.GcRule(max_num_versions=self.max_num_versions)
117+
return table_v2_pb2.GcRule(max_num_versions=self.max_num_versions)
118118

119119

120120
class MaxAgeGCRule(GarbageCollectionRule):
@@ -135,11 +135,11 @@ def __eq__(self, other):
135135
def to_pb(self):
136136
"""Converts the garbage collection rule to a protobuf.
137137
138-
:rtype: :class:`.data_v1_pb2.GcRule`
138+
:rtype: :class:`.table_v2_pb2.GcRule`
139139
:returns: The converted current object.
140140
"""
141141
max_age = _timedelta_to_duration_pb(self.max_age)
142-
return data_v1_pb2.GcRule(max_age=max_age)
142+
return table_v2_pb2.GcRule(max_age=max_age)
143143

144144

145145
class GCRuleUnion(GarbageCollectionRule):
@@ -160,12 +160,12 @@ def __eq__(self, other):
160160
def to_pb(self):
161161
"""Converts the union into a single GC rule as a protobuf.
162162
163-
:rtype: :class:`.data_v1_pb2.GcRule`
163+
:rtype: :class:`.table_v2_pb2.GcRule`
164164
:returns: The converted current object.
165165
"""
166-
union = data_v1_pb2.GcRule.Union(
166+
union = table_v2_pb2.GcRule.Union(
167167
rules=[rule.to_pb() for rule in self.rules])
168-
return data_v1_pb2.GcRule(union=union)
168+
return table_v2_pb2.GcRule(union=union)
169169

170170

171171
class GCRuleIntersection(GarbageCollectionRule):
@@ -186,12 +186,12 @@ def __eq__(self, other):
186186
def to_pb(self):
187187
"""Converts the intersection into a single GC rule as a protobuf.
188188
189-
:rtype: :class:`.data_v1_pb2.GcRule`
189+
:rtype: :class:`.table_v2_pb2.GcRule`
190190
:returns: The converted current object.
191191
"""
192-
intersection = data_v1_pb2.GcRule.Intersection(
192+
intersection = table_v2_pb2.GcRule.Intersection(
193193
rules=[rule.to_pb() for rule in self.rules])
194-
return data_v1_pb2.GcRule(intersection=intersection)
194+
return table_v2_pb2.GcRule(intersection=intersection)
195195

196196

197197
class ColumnFamily(object):
@@ -251,21 +251,22 @@ def __ne__(self, other):
251251
def create(self):
252252
"""Create this column family."""
253253
if self.gc_rule is None:
254-
column_family = data_v1_pb2.ColumnFamily()
254+
column_family = table_v2_pb2.ColumnFamily()
255255
else:
256-
column_family = data_v1_pb2.ColumnFamily(
256+
column_family = table_v2_pb2.ColumnFamily(
257257
gc_rule=self.gc_rule.to_pb())
258-
request_pb = messages_v1_pb2.CreateColumnFamilyRequest(
259-
name=self._table.name,
260-
column_family_id=self.column_family_id,
261-
column_family=column_family,
258+
request_pb = table_admin_v2_pb2.ModifyColumnFamiliesRequest(
259+
name=self._table.name)
260+
request_pb.modifications.add(
261+
id=self.column_family_id,
262+
create=column_family,
262263
)
263264
client = self._table._cluster._client
264-
# We expect a `.data_v1_pb2.ColumnFamily`. We ignore it since the only
265+
# We expect a `.table_v2_pb2.ColumnFamily`. We ignore it since the only
265266
# data it contains are the GC rule and the column family ID already
266267
# stored on this instance.
267-
client._table_stub.CreateColumnFamily(request_pb,
268-
client.timeout_seconds)
268+
client._table_stub.ModifyColumnFamilies(request_pb,
269+
client.timeout_seconds)
269270

270271
def update(self):
271272
"""Update this column family.
@@ -275,30 +276,40 @@ def update(self):
275276
Only the GC rule can be updated. By changing the column family ID,
276277
you will simply be referring to a different column family.
277278
"""
278-
request_kwargs = {'name': self.name}
279-
if self.gc_rule is not None:
280-
request_kwargs['gc_rule'] = self.gc_rule.to_pb()
281-
request_pb = data_v1_pb2.ColumnFamily(**request_kwargs)
279+
if self.gc_rule is None:
280+
column_family = table_v2_pb2.ColumnFamily()
281+
else:
282+
column_family = table_v2_pb2.ColumnFamily(
283+
gc_rule=self.gc_rule.to_pb())
284+
request_pb = table_admin_v2_pb2.ModifyColumnFamiliesRequest(
285+
name=self._table.name)
286+
request_pb.modifications.add(
287+
id=self.column_family_id,
288+
update=column_family)
282289
client = self._table._cluster._client
283-
# We expect a `.data_v1_pb2.ColumnFamily`. We ignore it since the only
290+
# We expect a `.table_v2_pb2.ColumnFamily`. We ignore it since the only
284291
# data it contains are the GC rule and the column family ID already
285292
# stored on this instance.
286-
client._table_stub.UpdateColumnFamily(request_pb,
287-
client.timeout_seconds)
293+
client._table_stub.ModifyColumnFamilies(request_pb,
294+
client.timeout_seconds)
288295

289296
def delete(self):
290297
"""Delete this column family."""
291-
request_pb = messages_v1_pb2.DeleteColumnFamilyRequest(name=self.name)
298+
request_pb = table_admin_v2_pb2.ModifyColumnFamiliesRequest(
299+
name=self._table.name)
300+
request_pb.modifications.add(
301+
id=self.column_family_id,
302+
drop=True)
292303
client = self._table._cluster._client
293304
# We expect a `google.protobuf.empty_pb2.Empty`
294-
client._table_stub.DeleteColumnFamily(request_pb,
295-
client.timeout_seconds)
305+
client._table_stub.ModifyColumnFamilies(request_pb,
306+
client.timeout_seconds)
296307

297308

298309
def _gc_rule_from_pb(gc_rule_pb):
299310
"""Convert a protobuf GC rule to a native object.
300311
301-
:type gc_rule_pb: :class:`.data_v1_pb2.GcRule`
312+
:type gc_rule_pb: :class:`.table_v2_pb2.GcRule`
302313
:param gc_rule_pb: The GC rule to convert.
303314
304315
:rtype: :class:`GarbageCollectionRule` or :data:`NoneType <types.NoneType>`

0 commit comments

Comments
 (0)