Skip to content

Commit

Permalink
Adding constructor for Batch HappyBase class.
Browse files Browse the repository at this point in the history
Also adding autodoc for this new module.
  • Loading branch information
dhermes committed Sep 4, 2015
1 parent c171a2d commit 1ac713d
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 4 deletions.
7 changes: 7 additions & 0 deletions docs/happybase-batch.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HappyBase Batch
~~~~~~~~~~~~~~~

.. automodule:: gcloud_bigtable.happybase.batch
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
:caption: HappyBase Compatibility Sub-Package

happybase-connection
happybase-batch

Google Cloud Bigtable: Python
=============================
Expand Down
5 changes: 1 addition & 4 deletions gcloud_bigtable/happybase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
as the backing store.
"""

from gcloud_bigtable.happybase.batch import Batch
from gcloud_bigtable.happybase.connection import Connection
from gcloud_bigtable.happybase.connection import DEFAULT_HOST
from gcloud_bigtable.happybase.connection import DEFAULT_PORT
Expand All @@ -28,10 +29,6 @@ class Table(object):
"""Unimplemented Table stub."""


class Batch(object):
"""Unimplemented Batch stub."""


class ConnectionPool(object):
"""Unimplemented ConnectionPool stub."""

Expand Down
70 changes: 70 additions & 0 deletions gcloud_bigtable/happybase/batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# 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.

"""Google Cloud Bigtable HappyBase batch module."""


_WAL_SENTINEL = object()


class Batch(object):
"""Batch class for accumulating mutations.
:type table: :class:`.Table`
:param table: The table where mutations will be applied.
:type timestamp: int
:param timestamp: (Optional) Timestamp (in milliseconds since the epoch)
that all mutations will be applied at.
:type batch_size: int
:param batch_size: (Optional) The maximum number of mutations to allow
to accumulate before committing them.
:type transaction: bool
:param transaction: Flag indicating if the mutations should be sent
transactionally or not. If ``transaction=True`` and
an error occurs while a :class:`Batch` is active,
then none of the accumulated mutations will be
committed. If ``batch_size`` is set, the mutation
can't be transactional.
:type wal: object
:param wal: Unused parameter (Boolean for using the HBase Write Ahead Log.)
Provided for compatibility with HappyBase, but irrelevant for
Cloud Bigtable since it does not have a Write Ahead Log.
:raises: :class:`TypeError <exceptions.TypeError>` if ``batch_size``
is set and ``transaction=True``.
:class:`ValueError <exceptions.ValueError>` if ``batch_size``
is not positive.
"""

def __init__(self, table, timestamp=None, batch_size=None,
transaction=False, wal=_WAL_SENTINEL):
if wal is not _WAL_SENTINEL:
raise ValueError('The wal argument cannot be used with '
'Cloud Bigtable.')

if batch_size is not None:
if transaction:
raise TypeError('When batch_size is set, a Batch cannot be '
'transactional')
if batch_size <= 0:
raise ValueError('batch_size must be positive')

self._table = table
self._batch_size = batch_size
self._timestamp = timestamp
self._transaction = transaction
67 changes: 67 additions & 0 deletions gcloud_bigtable/happybase/test_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 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.


import unittest2


class TestBatch(unittest2.TestCase):

def _getTargetClass(self):
from gcloud_bigtable.happybase.batch import Batch
return Batch

def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_constructor_defaults(self):
table = object()
batch = self._makeOne(table)
self.assertEqual(batch._table, table)
self.assertEqual(batch._batch_size, None)
self.assertEqual(batch._timestamp, None)
self.assertEqual(batch._transaction, False)

def test_constructor_explicit(self):
table = object()
timestamp = object()
batch_size = 42
transaction = False # Must be False when batch_size is non-null

batch = self._makeOne(table, timestamp=timestamp,
batch_size=batch_size, transaction=transaction)
self.assertEqual(batch._table, table)
self.assertEqual(batch._batch_size, batch_size)
self.assertEqual(batch._timestamp, timestamp)
self.assertEqual(batch._transaction, transaction)

def test_constructor_with_non_default_wal(self):
table = object()
wal = object()
with self.assertRaises(ValueError):
self._makeOne(table, wal=wal)

def test_constructor_with_negative_batch_size(self):
table = object()
batch_size = -1
with self.assertRaises(ValueError):
self._makeOne(table, batch_size=batch_size)

def test_constructor_with_batch_size_and_transactional(self):
table = object()
batch_size = 1
transaction = True
with self.assertRaises(TypeError):
self._makeOne(table, batch_size=batch_size,
transaction=transaction)

0 comments on commit 1ac713d

Please sign in to comment.