Skip to content

Commit 4e3f599

Browse files
committed
Adding Bigtable garbage collection rule class.
These are used as settings when creating new column families within a table. Also slipping in 3 small Bigtable docstring updates.
1 parent 94c007d commit 4e3f599

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

gcloud/bigtable/cluster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def finished(self):
213213
operation_name = ('operations/' + self._cluster.name +
214214
'/operations/%d' % (self.op_id,))
215215
request_pb = operations_pb2.GetOperationRequest(name=operation_name)
216-
# We expact a `._generated.operations_pb2.Operation`.
216+
# We expect a `._generated.operations_pb2.Operation`.
217217
operation_pb = self._cluster._client._operations_stub.GetOperation(
218218
request_pb, self._cluster._client.timeout_seconds)
219219

@@ -258,7 +258,7 @@ class Cluster(object):
258258
259259
:type serve_nodes: int
260260
:param serve_nodes: (Optional) The number of nodes in the cluster.
261-
Defaults to 3.
261+
Defaults to 3 (``_DEFAULT_SERVE_NODES``).
262262
"""
263263

264264
def __init__(self, zone, cluster_id, client,

gcloud/bigtable/column_family.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,59 @@
1515
"""User friendly container for Google Cloud Bigtable Column Family."""
1616

1717

18+
class GarbageCollectionRule(object):
19+
"""Table garbage collection rule.
20+
21+
Cells in the table fitting the rule will be deleted during
22+
garbage collection.
23+
24+
.. note::
25+
26+
At most one of ``max_num_versions`` and ``max_age`` can be specified
27+
at once.
28+
29+
.. note::
30+
31+
A string ``gc_expression`` can also be used with API requests, but
32+
that value would be superceded by a ``gc_rule``. As a result, we
33+
don't support that feature and instead support via this native
34+
object.
35+
36+
:type max_num_versions: int
37+
:param max_num_versions: The maximum number of versions
38+
39+
:type max_age: :class:`datetime.timedelta`
40+
:param max_age: The maximum age allowed for a cell in the table.
41+
42+
:raises: :class:`TypeError <exceptions.TypeError>` if both
43+
``max_num_versions`` and ``max_age`` are set.
44+
"""
45+
46+
def __init__(self, max_num_versions=None, max_age=None):
47+
self.max_num_versions = max_num_versions
48+
self.max_age = max_age
49+
self._check_single_value()
50+
51+
def _check_single_value(self):
52+
"""Checks that at most one value is set on the instance.
53+
54+
:raises: :class:`TypeError <exceptions.TypeError>` if not exactly one
55+
value set on the instance.
56+
"""
57+
if self.max_num_versions is not None and self.max_age is not None:
58+
raise TypeError('At most one of max_num_versions and '
59+
'max_age can be set')
60+
61+
def __eq__(self, other):
62+
if not isinstance(other, self.__class__):
63+
return False
64+
return (other.max_num_versions == self.max_num_versions and
65+
other.max_age == self.max_age)
66+
67+
def __ne__(self, other):
68+
return not self.__eq__(other)
69+
70+
1871
class ColumnFamily(object):
1972
"""Representation of a Google Cloud Bigtable Column Family.
2073

gcloud/bigtable/table.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Table(object):
4141
We can use a :class:`Table` to:
4242
4343
* :meth:`create` the table
44+
* :meth:`rename` the table
4445
* :meth:`delete` the table
4546
4647
:type table_id: str

gcloud/bigtable/test_column_family.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,47 @@
1616
import unittest2
1717

1818

19+
class TestGarbageCollectionRule(unittest2.TestCase):
20+
21+
def _getTargetClass(self):
22+
from gcloud.bigtable.column_family import GarbageCollectionRule
23+
return GarbageCollectionRule
24+
25+
def _makeOne(self, *args, **kwargs):
26+
return self._getTargetClass()(*args, **kwargs)
27+
28+
def test_constructor_defaults(self):
29+
gc_rule = self._makeOne()
30+
self.assertEqual(gc_rule.max_num_versions, None)
31+
self.assertEqual(gc_rule.max_age, None)
32+
33+
def test_constructor_failure(self):
34+
with self.assertRaises(TypeError):
35+
self._makeOne(max_num_versions=1, max_age=object())
36+
37+
def test___eq__max_age(self):
38+
max_age = object()
39+
gc_rule1 = self._makeOne(max_age=max_age)
40+
gc_rule2 = self._makeOne(max_age=max_age)
41+
self.assertEqual(gc_rule1, gc_rule2)
42+
43+
def test___eq__max_num_versions(self):
44+
gc_rule1 = self._makeOne(max_num_versions=2)
45+
gc_rule2 = self._makeOne(max_num_versions=2)
46+
self.assertEqual(gc_rule1, gc_rule2)
47+
48+
def test___eq__type_differ(self):
49+
gc_rule1 = self._makeOne()
50+
gc_rule2 = object()
51+
self.assertNotEqual(gc_rule1, gc_rule2)
52+
53+
def test___ne__same_value(self):
54+
gc_rule1 = self._makeOne()
55+
gc_rule2 = self._makeOne()
56+
comparison_val = (gc_rule1 != gc_rule2)
57+
self.assertFalse(comparison_val)
58+
59+
1960
class TestColumnFamily(unittest2.TestCase):
2061

2162
def _getTargetClass(self):

0 commit comments

Comments
 (0)