Skip intermediate results page
""" A wrapper that converts Cassandra futures to Tornado futures. """
+import logging
from tornado.concurrent import Future as TornadoFuture
from tornado.ioloop import IOLoop
@@ -13,7 +14,7 @@ class TornadoCassandra(object):
"""
self._session = session
- def execute(self, *args, **kwargs):
+ def execute(self, query, parameters=None, *args, **kwargs):
""" Runs a Cassandra query asynchronously.
Returns:
@@ -21,33 +22,43 @@ class TornadoCassandra(object):
"""
tornado_future = TornadoFuture()
io_loop = IOLoop.current()
- cassandra_future = self._session.execute_async(*args, **kwargs)
+ cassandra_future = self._session.execute_async(
+ query, parameters, *args, **kwargs)
cassandra_future.add_callbacks(
- self._handle_success, self._handle_failure,
- callback_args=(io_loop, tornado_future, cassandra_future),
- errback_args=(io_loop, tornado_future)
+ self._handle_page, self._handle_failure,
+ callback_args=(io_loop, tornado_future, cassandra_future, query),
+ errback_args=(io_loop, tornado_future, query)
)
return tornado_future
@staticmethod
- def _handle_success(results, io_loop, tornado_future, cassandra_future):
+ def _handle_page(results, io_loop, tornado_future, cassandra_future, query):
""" Assigns the Cassandra result to the Tornado future.
Args:
results: A list of result rows (limited version of ResultSet).
io_loop: An instance of tornado IOLoop where execute was initially called.
tornado_future: A Tornado future.
- cassandra_future: A Cassandra future containing ResultSet
+ cassandra_future: A Cassandra future containing ResultSet.
+ query: An instance of Cassandra query.
"""
+ if cassandra_future.has_more_pages:
+ logging.debug(
+ u"Ignoring an intermediate results page for query: {}"
+ .format(query)
+ )
+ return
io_loop.add_callback(tornado_future.set_result, cassandra_future.result())
@staticmethod
- def _handle_failure(error, io_loop, tornado_future):
+ def _handle_failure(error, io_loop, tornado_future, query):
""" Assigns the Cassandra exception to the Tornado future.
Args:
error: A Python exception.
io_loop: An instance of tornado IOLoop where execute was initially called.
tornado_future: A Tornado future.
+ query: An instance of Cassandra query.
"""
+ logging.error(u"Failed to run query: {} ({})".format(query, error))
io_loop.add_callback(tornado_future.set_exception, error)
