Skip to content

Commit 15b5311

Browse files
committed
Add support for TCompactProtocol (#70)
2 parents ecc53df + 4cf141e commit 15b5311

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

NEWS.rst

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ HappyBase 0.9
99

1010
Release date: *not yet released*
1111

12+
* Add support for the Thrift compact protocol (``TCompactProtocol``) in
13+
:py:class:`Connection`.
14+
1215

1316
HappyBase 0.8
1417
-------------

happybase/connection.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from thrift.transport.TSocket import TSocket
1010
from thrift.transport.TTransport import TBufferedTransport, TFramedTransport
11-
from thrift.protocol import TBinaryProtocol
11+
from thrift.protocol import TBinaryProtocol, TCompactProtocol
1212

1313
from .hbase import Hbase
1414
from .hbase.ttypes import ColumnDescriptor
@@ -22,11 +22,16 @@
2222
buffered=TBufferedTransport,
2323
framed=TFramedTransport,
2424
)
25+
THRIFT_PROTOCOLS = dict(
26+
binary=TBinaryProtocol.TBinaryProtocolAccelerated,
27+
compact=TCompactProtocol.TCompactProtocol,
28+
)
2529

2630
DEFAULT_HOST = 'localhost'
2731
DEFAULT_PORT = 9090
2832
DEFAULT_TRANSPORT = 'buffered'
2933
DEFAULT_COMPAT = '0.96'
34+
DEFAULT_PROTOCOL = 'binary'
3035

3136

3237
class Connection(object):
@@ -67,6 +72,18 @@ class Connection(object):
6772
``-hsha``, ``-nonblocking``, and ``-threadedselector`` modes use the
6873
framed transport.
6974
75+
The optional `protocol` argument specifies the Thrift transport
76+
protocol to use. Supported values for this argument are ``binary``
77+
(the default) and ``compact``. Make sure to choose the right one,
78+
since otherwise you might see non-obvious connection errors or
79+
program hangs when making a connection. ``TCompactProtocol`` is
80+
a more compact binary format that is typically more efficient to
81+
process as well. ``TBinaryAccelerated`` is the default protocol that
82+
happybase uses.
83+
84+
.. versionadded:: 0.9
85+
`protocol` argument
86+
7087
.. versionadded:: 0.5
7188
`timeout` argument
7289
@@ -88,7 +105,7 @@ class Connection(object):
88105
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, timeout=None,
89106
autoconnect=True, table_prefix=None,
90107
table_prefix_separator='_', compat=DEFAULT_COMPAT,
91-
transport=DEFAULT_TRANSPORT):
108+
transport=DEFAULT_TRANSPORT, protocol=DEFAULT_PROTOCOL):
92109

93110
if transport not in THRIFT_TRANSPORTS:
94111
raise ValueError("'transport' must be one of %s"
@@ -105,6 +122,10 @@ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, timeout=None,
105122
raise ValueError("'compat' must be one of %s"
106123
% ", ".join(COMPAT_MODES))
107124

125+
if protocol not in THRIFT_PROTOCOLS:
126+
raise ValueError("'protocol' must be one of %s"
127+
% ", ".join(THRIFT_PROTOCOLS))
128+
108129
# Allow host and port to be None, which may be easier for
109130
# applications wrapping a Connection instance.
110131
self.host = host or DEFAULT_HOST
@@ -115,6 +136,7 @@ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, timeout=None,
115136
self.compat = compat
116137

117138
self._transport_class = THRIFT_TRANSPORTS[transport]
139+
self._protocol_class = THRIFT_PROTOCOLS[protocol]
118140
self._refresh_thrift_client()
119141

120142
if autoconnect:
@@ -129,7 +151,7 @@ def _refresh_thrift_client(self):
129151
socket.setTimeout(self.timeout)
130152

131153
self.transport = self._transport_class(socket)
132-
protocol = TBinaryProtocol.TBinaryProtocolAccelerated(self.transport)
154+
protocol = self._protocol_class(self.transport)
133155
self.client = Hbase.Client(protocol)
134156

135157
def _table_name(self, name):

0 commit comments

Comments
 (0)