Skip to content

Commit

Permalink
Implementing _convert_to_time_range helper.
Browse files Browse the repository at this point in the history
This is to help convert millisecond timestamps into the "next"
TimestampRange (as a compatibility between HBase and Cloud Bigtable).
  • Loading branch information
dhermes committed Sep 10, 2015
1 parent c43dbb1 commit 96a480e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
25 changes: 25 additions & 0 deletions gcloud_bigtable/happybase/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import struct

from gcloud_bigtable._helpers import _microseconds_to_timestamp
from gcloud_bigtable.column_family import GarbageCollectionRule
from gcloud_bigtable.column_family import GarbageCollectionRuleIntersection
from gcloud_bigtable.happybase.batch import Batch
from gcloud_bigtable.happybase.batch import _WAL_SENTINEL
from gcloud_bigtable.row import TimestampRange
from gcloud_bigtable.table import Table as _LowLevelTable


Expand Down Expand Up @@ -125,6 +127,29 @@ def _gc_rule_to_dict(gc_rule):
return result


def _convert_to_time_range(timestamp=None):
"""Create a timestamp range from an HBase / HappyBase timestamp.
HBase uses timestamp as an argument to specify an inclusive end
deadline. Since Cloud Bigtable uses exclusive end times, we increment
by one millisecond (the lowest granularity allowed).
:type timestamp: int
:param timestamp: (Optional) Timestamp (in milliseconds since the
epoch). Intended to be used as the end of an HBase
time range, which is inclusive.
:rtype: :class:`.TimestampRange`, :data:`NoneType <types.NoneType>`
:returns: The timestamp range corresponding to the passed in
``timestamp``.
"""
if timestamp is None:
return None

next_timestamp = _microseconds_to_timestamp(1000 * (timestamp + 1))
return TimestampRange(end=next_timestamp)


class Table(object):
"""Representation of Cloud Bigtable table.
Expand Down
28 changes: 28 additions & 0 deletions gcloud_bigtable/happybase/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,34 @@ def test_with_intersection_two_nested_rules(self):
self.assertTrue(result is gc_rule)


class Test__convert_to_time_range(unittest2.TestCase):

def _callFUT(self, timestamp=None):
from gcloud_bigtable.happybase.table import _convert_to_time_range
return _convert_to_time_range(timestamp=timestamp)

def test_null(self):
timestamp = None
result = self._callFUT(timestamp=timestamp)
self.assertEqual(result, None)

def test_invalid_type(self):
timestamp = object()
with self.assertRaises(TypeError):
self._callFUT(timestamp=timestamp)

def test_success(self):
from gcloud_bigtable._helpers import _microseconds_to_timestamp
from gcloud_bigtable.row import TimestampRange

timestamp = 1441928298571
next_ts = _microseconds_to_timestamp(1000 * (timestamp + 1))
result = self._callFUT(timestamp=timestamp)
self.assertTrue(isinstance(result, TimestampRange))
self.assertEqual(result.start, None)
self.assertEqual(result.end, next_ts)


class TestTable(unittest2.TestCase):

def _getTargetClass(self):
Expand Down

0 comments on commit 96a480e

Please sign in to comment.