diff options
author | Wouter Bolsterlee <uws@xs4all.nl> | 2013-11-03 17:17:37 +0100 |
---|---|---|
committer | Wouter Bolsterlee <uws@xs4all.nl> | 2013-11-03 17:17:37 +0100 |
commit | 7cfb9ee3096f553f699b706dc18e6b01303d3f49 (patch) | |
tree | d67ed3a722435e258caad1e330851587dba2b6b6 | |
parent | d8130916c61d6fa295930739f1bbee4e964a7353 (diff) | |
download | happybase-7cfb9ee3096f553f699b706dc18e6b01303d3f49.tar.gz |
Expose Mutation.writeToWAL in .put(), .delete(), and batch()
Various data manipulation methods on Table and Batch instances now
support a 'wal' argument to influence whether the mutation is written to
the Write-Ahead Log. This is only supported in recent HBase version.
Fixes issue #36.
-rw-r--r-- | happybase/batch.py | 30 | ||||
-rw-r--r-- | happybase/table.py | 33 |
2 files changed, 48 insertions, 15 deletions
diff --git a/happybase/batch.py b/happybase/batch.py index 5af6696..695da50 100644 --- a/happybase/batch.py +++ b/happybase/batch.py @@ -18,7 +18,7 @@ class Batch(object): instead. """ def __init__(self, table, timestamp=None, batch_size=None, - transaction=False): + transaction=False, wal=True): """Initialise a new Batch instance.""" if not (timestamp is None or isinstance(timestamp, Integral)): raise TypeError("'timestamp' must be an integer or None") @@ -34,6 +34,7 @@ class Batch(object): self._batch_size = batch_size self._timestamp = timestamp self._transaction = transaction + self._wal = wal self._families = None self._reset_mutations() @@ -62,25 +63,32 @@ class Batch(object): # Mutation methods # - def put(self, row, data): + def put(self, row, data, wal=None): """Store data in the table. - See :py:meth:`Table.put` for a description of the `row` and `data` - arguments. + See :py:meth:`Table.put` for a description of the `row`, `data`, + and `wal` arguments. """ + if wal is None: + wal = self._wal + self._mutations[row].extend( - Mutation(isDelete=False, column=column, value=value) + Mutation( + isDelete=False, + column=column, + value=value, + writeToWAL=wal) for column, value in data.iteritems()) self._mutation_count += len(data) if self._batch_size and self._mutation_count >= self._batch_size: self.send() - def delete(self, row, columns=None): + def delete(self, row, columns=None, wal=None): """Delete data from the table. - See :py:meth:`Table.delete` for a description of the `row` and - `columns` arguments. + See :py:meth:`Table.delete` for a description of the `row`, + `columns`, and `wal` arguments. """ # Work-around Thrift API limitation: the mutation API can only # delete specified columns, not complete rows, so just list the @@ -91,8 +99,12 @@ class Batch(object): self._families = self._table._column_family_names() columns = self._families + if wal is None: + wal = self._wal + self._mutations[row].extend( - Mutation(isDelete=True, column=column) for column in columns) + Mutation(isDelete=True, column=column, writeToWAL=wal) + for column in columns) self._mutation_count += len(columns) if self._batch_size and self._mutation_count >= self._batch_size: diff --git a/happybase/table.py b/happybase/table.py index 2ff046a..e0795f6 100644 --- a/happybase/table.py +++ b/happybase/table.py @@ -350,26 +350,30 @@ class Table(object): # Data manipulation # - def put(self, row, data, timestamp=None): + def put(self, row, data, timestamp=None, wal=True): """Store data in the table. This method stores the data in the `data` argument for the row specified by `row`. The `data` argument is dictionary that maps columns to values. Column names must include a family and qualifier part, e.g. `cf:col`, though the qualifier part may be the empty string, e.g. - `cf:`. The `timestamp` argument is optional. + `cf:`. Note that, in many situations, :py:meth:`batch()` is a more appropriate method to manipulate data. + .. versionadded:: 0.7 + `wal` parameter + :param str row: the row key :param dict data: the data to store :param int timestamp: timestamp (optional) + :param wal bool: whether to write to the WAL (optional) """ - with self.batch(timestamp=timestamp) as batch: + with self.batch(timestamp=timestamp, wal=wal) as batch: batch.put(row, data) - def delete(self, row, columns=None, timestamp=None): + def delete(self, row, columns=None, timestamp=None, wal=True): """Delete data from the table. This method deletes all columns for the row specified by `row`, or only @@ -378,14 +382,19 @@ class Table(object): Note that, in many situations, :py:meth:`batch()` is a more appropriate method to manipulate data. + .. versionadded:: 0.7 + `wal` parameter + :param str row: the row key :param list_or_tuple columns: list of columns (optional) :param int timestamp: timestamp (optional) + :param wal bool: whether to write to the WAL (optional) """ - with self.batch(timestamp=timestamp) as batch: + with self.batch(timestamp=timestamp, wal=wal) as batch: batch.delete(row, columns) - def batch(self, timestamp=None, batch_size=None, transaction=False): + def batch(self, timestamp=None, batch_size=None, transaction=False, + wal=True): """Create a new batch operation for this table. This method returns a new :py:class:`Batch` instance that can be used @@ -401,11 +410,23 @@ class Table(object): used as context manager in a ``with`` block of code. The `transaction` flag cannot be used in combination with `batch_size`. + The `wal` argument determines whether mutations should be + written to the HBase Write Ahead Log (WAL). This flag can only + be used with recent HBase versions. If specified, it provides + a default for all the put and delete operations on this batch. + This default value can be overridden for individual operations + using the `wal` argument to :py:meth:`Batch.put` and + :py:meth:`Batch.delete`. + + .. versionadded:: 0.7 + `wal` parameter + :param bool transaction: whether this batch should behave like a transaction (only useful when used as a context manager) :param int batch_size: batch size (optional) :param int timestamp: timestamp (optional) + :param wal bool: whether to write to the WAL (optional) :return: Batch instance :rtype: :py:class:`Batch` |