Allow table names to be specified as binary or text

In HBase, the name is a byte string, but transparently encoding text
using UTF-8 allows for cleaner code on the Python side. This makes code
like

  connection.table("sometable")

work on both Python 3 and 2.
This commit is contained in:
Wouter Bolsterlee 2016-03-28 20:13:42 +02:00 committed by Wouter Bolsterlee
parent 76ed812f5f
commit c788d830b1
1 changed files with 10 additions and 7 deletions

View File

@ -13,7 +13,7 @@ from thriftpy.protocol import TBinaryProtocol, TCompactProtocol
from .Hbase_thrift import Hbase, ColumnDescriptor
from .table import Table
from .util import pep8_to_camel_case
from .util import ensure_bytes, pep8_to_camel_case
logger = logging.getLogger(__name__)
@ -106,19 +106,21 @@ class Connection(object):
"""
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, timeout=None,
autoconnect=True, table_prefix=None,
table_prefix_separator='_', compat=DEFAULT_COMPAT,
table_prefix_separator=b'_', compat=DEFAULT_COMPAT,
transport=DEFAULT_TRANSPORT, protocol=DEFAULT_PROTOCOL):
if transport not in THRIFT_TRANSPORTS:
raise ValueError("'transport' must be one of %s"
% ", ".join(THRIFT_TRANSPORTS.keys()))
if table_prefix is not None \
and not isinstance(table_prefix, STRING_OR_BINARY):
raise TypeError("'table_prefix' must be a string")
if table_prefix is not None:
if not isinstance(table_prefix, STRING_OR_BINARY):
raise TypeError("'table_prefix' must be a string")
table_prefix = ensure_bytes(table_prefix)
if not isinstance(table_prefix_separator, STRING_OR_BINARY):
raise TypeError("'table_prefix_separator' must be a string")
table_prefix_separator = ensure_bytes(table_prefix_separator)
if compat not in COMPAT_MODES:
raise ValueError("'compat' must be one of %s"
@ -158,9 +160,9 @@ class Connection(object):
def _table_name(self, name):
"""Construct a table name by optionally adding a table name prefix."""
name = ensure_bytes(name)
if self.table_prefix is None:
return name
return self.table_prefix + self.table_prefix_separator + name
def open(self):
@ -220,6 +222,7 @@ class Connection(object):
:return: Table instance
:rtype: :py:class:`Table`
"""
name = ensure_bytes(name)
if use_prefix:
name = self._table_name(name)
return Table(name, self)
@ -241,7 +244,7 @@ class Connection(object):
# Filter using prefix, and strip prefix from names
if self.table_prefix is not None:
prefix = self._table_name('')
prefix = self._table_name(b'')
offset = len(prefix)
names = [n[offset:] for n in names if n.startswith(prefix)]