Skip to content

Commit

Permalink
Moving garbage collection classes into column family module.
Browse files Browse the repository at this point in the history
Will be implementing ColumnFamily class and moving column
family methods from Table.
  • Loading branch information
dhermes committed Jul 28, 2015
1 parent 40ce7f9 commit 7992719
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 309 deletions.
20 changes: 5 additions & 15 deletions docs/garbage-collection.rst → docs/column-family.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Table Garbage Collection
========================
Table Column Families
=====================

When creating a :class:`Table <gcloud_bigtable.table.Table>`, it is
possible to set garbage collection rules for expired data.
Expand Down Expand Up @@ -37,20 +37,10 @@ at the lowest level of the nesting:
rule4 = GarbageCollectionRule(max_num_versions=3)
rule5 = GarbageCollectionUnion(rules=[rule3, rule4])
Garbage Collection Classes
~~~~~~~~~~~~~~~~~~~~~~~~~~
Module
~~~~~~

.. autoclass:: gcloud_bigtable.table.GarbageCollectionRule
:members:
:undoc-members:
:show-inheritance:

.. autoclass:: gcloud_bigtable.table.GarbageCollectionRuleUnion
:members:
:undoc-members:
:show-inheritance:

.. autoclass:: gcloud_bigtable.table.GarbageCollectionRuleIntersection
.. automodule:: gcloud_bigtable.column_family
:members:
:undoc-members:
:show-inheritance:
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Documented Modules
client
cluster
table
garbage-collection
column-family

Indices and tables
~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 0 additions & 2 deletions docs/table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ Table
:members:
:undoc-members:
:show-inheritance:
:exclude-members: GarbageCollectionRule,GarbageCollectionRuleUnion,
GarbageCollectionRuleIntersection
1 change: 1 addition & 0 deletions gcloud_bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
In the hierarchy of API concepts
* a client owns a cluster
* a cluster owns a table
* a table owns column families
* a table owns data
"""

Expand Down
115 changes: 115 additions & 0 deletions gcloud_bigtable/column_family.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# 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 gcloud_bigtable._generated import bigtable_table_data_pb2 as data_pb2
from gcloud_bigtable._helpers import _timedelta_to_duration_pb


class GarbageCollectionRule(object):
"""Table garbage collection rule.
Cells in the table fitting the rule will be deleted during
garbage collection.
These values can be combined via :class:`GarbageCollectionRuleUnion` and
:class:`GarbageCollectionRuleIntersection.`
.. note::
At most one of ``max_num_versions`` and ``max_age`` can be specified
at once.
.. 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 this native
object.
:type max_num_versions: integer
:param max_num_versions: The maximum number of versions
:type max_age: :class:`datetime.timedelta`
:param max_age: The maximum age allowed for a cell in the table.
:raises: :class:`ValueError` if both ``max_num_versions`` and ``max_age``
are set.
"""

def __init__(self, max_num_versions=None, max_age=None):
if max_num_versions is not None and max_age is not None:
raise ValueError('At most one of max_num_versions and '
'max_age can be set')
self.max_num_versions = max_num_versions
self.max_age = max_age

def to_pb(self):
"""Converts the :class:`GarbageCollectionRule` to a protobuf.
:rtype: :class:`data_pb2.GcRule`
:returns: The converted current object.
"""
gc_rule_kwargs = {}
if self.max_num_versions is not None:
gc_rule_kwargs['max_num_versions'] = self.max_num_versions
if self.max_age is not None:
gc_rule_kwargs['max_age'] = _timedelta_to_duration_pb(self.max_age)
return data_pb2.GcRule(**gc_rule_kwargs)


class GarbageCollectionRuleUnion(object):
"""Union of garbage collection rules.
:type rules: list
:param rules: List of garbage collection rules, unions and/or
intersections.
"""

def __init__(self, rules=None):
self.rules = rules

def to_pb(self):
"""Converts the union into a single gc rule as a protobuf.
:rtype: :class:`data_pb2.GcRule`
:returns: The converted current object.
"""
union = data_pb2.GcRule.Union(
rules=[rule.to_pb() for rule in self.rules])
return data_pb2.GcRule(union=union)


class GarbageCollectionRuleIntersection(object):
"""Intersection of garbage collection rules.
:type rules: list
:param rules: List of garbage collection rules, unions and/or
intersections.
"""

def __init__(self, rules=None):
self.rules = rules

def to_pb(self):
"""Converts the intersection into a single gc rule as a protobuf.
:rtype: :class:`data_pb2.GcRule`
:returns: The converted current object.
"""
intersection = data_pb2.GcRule.Intersection(
rules=[rule.to_pb() for rule in self.rules])
return data_pb2.GcRule(intersection=intersection)
109 changes: 6 additions & 103 deletions gcloud_bigtable/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from gcloud_bigtable._generated import (
bigtable_table_service_messages_pb2 as messages_pb2)
from gcloud_bigtable._generated import bigtable_table_service_pb2
from gcloud_bigtable._helpers import _timedelta_to_duration_pb
from gcloud_bigtable._helpers import make_stub


Expand All @@ -31,102 +30,6 @@
"""Table Admin API request port."""


class GarbageCollectionRule(object):
"""Table garbage collection rule.
Cells in the table fitting the rule will be deleted during
garbage collection.
These values can be combined via :class:`GarbageCollectionRuleUnion` and
:class:`GarbageCollectionRuleIntersection.`
.. note::
At most one of ``max_num_versions`` and ``max_age`` can be specified
at once.
.. 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 this native
object.
:type max_num_versions: integer
:param max_num_versions: The maximum number of versions
:type max_age: :class:`datetime.timedelta`
:param max_age: The maximum age allowed for a cell in the table.
:raises: :class:`ValueError` if both ``max_num_versions`` and ``max_age``
are set.
"""

def __init__(self, max_num_versions=None, max_age=None):
if max_num_versions is not None and max_age is not None:
raise ValueError('At most one of max_num_versions and '
'max_age can be set')
self.max_num_versions = max_num_versions
self.max_age = max_age

def to_pb(self):
"""Converts the :class:`GarbageCollectionRule` to a protobuf.
:rtype: :class:`data_pb2.GcRule`
:returns: The converted current object.
"""
gc_rule_kwargs = {}
if self.max_num_versions is not None:
gc_rule_kwargs['max_num_versions'] = self.max_num_versions
if self.max_age is not None:
gc_rule_kwargs['max_age'] = _timedelta_to_duration_pb(self.max_age)
return data_pb2.GcRule(**gc_rule_kwargs)


class GarbageCollectionRuleUnion(object):
"""Union of garbage collection rules.
:type rules: list
:param rules: List of garbage collection rules, unions and/or
intersections.
"""

def __init__(self, rules=None):
self.rules = rules

def to_pb(self):
"""Converts the union into a single gc rule as a protobuf.
:rtype: :class:`data_pb2.GcRule`
:returns: The converted current object.
"""
union = data_pb2.GcRule.Union(
rules=[rule.to_pb() for rule in self.rules])
return data_pb2.GcRule(union=union)


class GarbageCollectionRuleIntersection(object):
"""Intersection of garbage collection rules.
:type rules: list
:param rules: List of garbage collection rules, unions and/or
intersections.
"""

def __init__(self, rules=None):
self.rules = rules

def to_pb(self):
"""Converts the intersection into a single gc rule as a protobuf.
:rtype: :class:`data_pb2.GcRule`
:returns: The converted current object.
"""
intersection = data_pb2.GcRule.Intersection(
rules=[rule.to_pb() for rule in self.rules])
return data_pb2.GcRule(intersection=intersection)


class Table(object):
"""Representation of a Google Cloud Bigtable Table.
Expand Down Expand Up @@ -330,9 +233,9 @@ def create_column_family(self, column_family_id, gc_rule=None,
:type column_family_id: string
:param column_family_id: The ID of the column family.
:type gc_rule: :class:`GarbageCollectionRule`,
:class:`GarbageCollectionRuleUnion` or
:class:`GarbageCollectionRuleIntersection`
:type gc_rule: :class:`.column_family.GarbageCollectionRule`,
:class:`.column_family.GarbageCollectionRuleUnion` or
:class:`.column_family.GarbageCollectionRuleIntersection`
:param gc_rule: The garbage collection settings for the column family.
:type timeout_seconds: integer
Expand Down Expand Up @@ -365,9 +268,9 @@ def update_column_family(self, column_family_id, gc_rule=None,
:type column_family_id: string
:param column_family_id: The ID of the column family.
:type gc_rule: :class:`GarbageCollectionRule`,
:class:`GarbageCollectionRuleUnion` or
:class:`GarbageCollectionRuleIntersection`
:type gc_rule: :class:`.column_family.GarbageCollectionRule`,
:class:`.column_family.GarbageCollectionRuleUnion` or
:class:`.column_family.GarbageCollectionRuleIntersection`
:param gc_rule: The garbage collection settings for the column family.
:type timeout_seconds: integer
Expand Down

0 comments on commit 7992719

Please sign in to comment.