Skip to content

Commit d4c380c

Browse files
committed
Make ResultSet handle more than just list results
for use with custom ProtocolHandlers providing different ResultMessage deserialization PYTHON-430
1 parent 385baa1 commit d4c380c

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

cassandra/cluster.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
BatchMessage, RESULT_KIND_PREPARED,
6262
RESULT_KIND_SET_KEYSPACE, RESULT_KIND_ROWS,
6363
RESULT_KIND_SCHEMA_CHANGE, MIN_SUPPORTED_VERSION,
64-
ProtocolHandler)
64+
ProtocolHandler, _RESULT_SEQUENCE_TYPES)
6565
from cassandra.metadata import Metadata, protect_name, murmur3
6666
from cassandra.policies import (TokenAwarePolicy, DCAwareRoundRobinPolicy, SimpleConvictionPolicy,
6767
ExponentialReconnectionPolicy, HostDistance,
@@ -3345,7 +3345,7 @@ class ResultSet(object):
33453345

33463346
def __init__(self, response_future, initial_response):
33473347
self.response_future = response_future
3348-
self._current_rows = initial_response or []
3348+
self._set_current_rows(initial_response)
33493349
self._page_iter = None
33503350
self._list_mode = False
33513351

@@ -3386,10 +3386,16 @@ def fetch_next_page(self):
33863386
if self.response_future.has_more_pages:
33873387
self.response_future.start_fetching_next_page()
33883388
result = self.response_future.result()
3389-
self._current_rows = result._current_rows
3389+
self._current_rows = result._current_rows # ResultSet has already _set_current_rows to the appropriate form
33903390
else:
33913391
self._current_rows = []
33923392

3393+
def _set_current_rows(self, result):
3394+
if isinstance(result, _RESULT_SEQUENCE_TYPES):
3395+
self._current_rows = result
3396+
else:
3397+
self._current_rows = [result] if result else []
3398+
33933399
def _fetch_all(self):
33943400
self._current_rows = list(self)
33953401
self._page_iter = None

cassandra/obj_parser.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ cdef class LazyParser(ColumnParser):
3838
# supported in cpdef methods
3939
return parse_rows_lazy(reader, desc)
4040

41+
cpdef get_cython_generator_type(self):
42+
return get_cython_generator_type()
4143

4244
def parse_rows_lazy(BytesIOReader reader, ParseDesc desc):
4345
cdef Py_ssize_t i, rowcount
4446
rowcount = read_int(reader)
4547
cdef RowParser rowparser = TupleRowParser()
4648
return (rowparser.unpack_row(reader, desc) for i in range(rowcount))
4749

50+
def get_cython_generator_type():
51+
return type((i for i in range(0)))
4852

4953
cdef class TupleRowParser(RowParser):
5054
"""

cassandra/protocol.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ def decode_message(cls, protocol_version, user_type_map, stream_id, flags, opcod
10001000

10011001
return msg
10021002

1003+
_RESULT_SEQUENCE_TYPES = (list, tuple) # types retuned by ResultMessages
10031004

10041005
def cython_protocol_handler(colparser):
10051006
"""
@@ -1045,6 +1046,9 @@ class CythonProtocolHandler(ProtocolHandler):
10451046
if HAVE_CYTHON:
10461047
from cassandra.obj_parser import ListParser, LazyParser
10471048
ProtocolHandler = cython_protocol_handler(ListParser())
1049+
1050+
lazy_parser = LazyParser()
1051+
_RESULT_SEQUENCE_TYPES += (lazy_parser.get_cython_generator_type(),)
10481052
LazyProtocolHandler = cython_protocol_handler(LazyParser())
10491053
else:
10501054
# Use Python-based ProtocolHandler

0 commit comments

Comments
 (0)