forked from python-happybase/happybase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request python-happybase#1 from TAKEALOT/reverse_table_scans
Reverse table scans
- Loading branch information
Showing
4 changed files
with
77 additions
and
2 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
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 |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
setup.py. | ||
""" | ||
|
||
__version__ = '0.9' | ||
__version__ = '0.9-takealot' |
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,51 @@ | ||
from happybase.hbase.ttypes import TIncrement | ||
|
||
|
||
class CounterBatch: | ||
def __init__(self, table, batch_size=None): | ||
self.table = table | ||
self.batch_size = batch_size | ||
self.batch = [] | ||
|
||
def counter_inc(self, row, column, value=1): | ||
self.batch.append({'row': row, 'column': column, 'value': value}) | ||
self._check_send() | ||
|
||
def counter_dec(self, row, column, value=1): | ||
self.batch.append({'row': row, 'column': column, 'value': -value}) | ||
self._check_send() | ||
|
||
def send(self): | ||
increment_rows = [] | ||
for increment in self.batch: | ||
increment_rows.append( | ||
TIncrement( | ||
table=self.table.name, | ||
row=increment['row'], | ||
column=increment['column'], | ||
ammount=increment.get('value', 1), | ||
) | ||
) | ||
self.table.connection.client.incrementRows(increment_rows) | ||
|
||
def _check_send(self): | ||
if self.batch_size and (len(self.batch) >= self.batch_size): | ||
self.send() | ||
self.batch = [] | ||
|
||
# | ||
# Context manager methods | ||
# | ||
|
||
def __enter__(self): | ||
"""Called upon entering a ``with`` block""" | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
"""Called upon exiting a ``with`` block""" | ||
# TODO: Examine the exception and decide whether or not to send | ||
# For now we always send | ||
if exc_type is not None: | ||
pass | ||
|
||
self.send() |
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