-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding constructor for Batch HappyBase class.
Also adding autodoc for this new module.
- Loading branch information
Showing
5 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |