|
2 | 2 | This module houses the main classes you will interact with, |
3 | 3 | :class:`.Cluster` and :class:`.Session`. |
4 | 4 | """ |
| 5 | +from __future__ import absolute_import |
5 | 6 |
|
6 | 7 | from concurrent.futures import ThreadPoolExecutor |
7 | 8 | import logging |
@@ -147,8 +148,15 @@ class Cluster(object): |
147 | 148 | server will be automatically used. |
148 | 149 | """ |
149 | 150 |
|
150 | | - # TODO: docs |
151 | 151 | protocol_version = 2 |
| 152 | + """ |
| 153 | + The version of the native protocol to use. The protocol version 2 |
| 154 | + add support for lightweight transactions, batch operations, and |
| 155 | + automatic query paging, but is only supported by Cassandra 2.0+. When |
| 156 | + working with Cassandra 1.2, this must be set to 1. You can also set |
| 157 | + this to 1 when working with Cassandra 2.0+, but features that require |
| 158 | + the version 2 protocol will not be enabled. |
| 159 | + """ |
152 | 160 |
|
153 | 161 | compression = True |
154 | 162 | """ |
@@ -941,10 +949,10 @@ class Session(object): |
941 | 949 | default_fetch_size = 5000 |
942 | 950 | """ |
943 | 951 | By default, this many rows will be fetched at a time. This can be |
944 | | - specified per-query through :attr:`~Statement.fetch_size`. |
| 952 | + specified per-query through :attr:`.Statement.fetch_size`. |
945 | 953 |
|
946 | 954 | This only takes effect when protocol version 2 or higher is used. |
947 | | - See :attr:`~Cluster.protocol_version` for details. |
| 955 | + See :attr:`.Cluster.protocol_version` for details. |
948 | 956 | """ |
949 | 957 |
|
950 | 958 | _lock = None |
@@ -1970,9 +1978,21 @@ def _query(self, host, message=None, cb=None): |
1970 | 1978 |
|
1971 | 1979 | @property |
1972 | 1980 | def has_more_pages(self): |
| 1981 | + """ |
| 1982 | + Returns :const:`True` if there are more pages left in the |
| 1983 | + query results, :const:`False` otherwise. This should only |
| 1984 | + be checked after the first page has been returned. |
| 1985 | + """ |
1973 | 1986 | return self._paging_state is not None |
1974 | 1987 |
|
1975 | 1988 | def start_fetching_next_page(self): |
| 1989 | + """ |
| 1990 | + If there are more pages left in the query result, this asynchronously |
| 1991 | + starts fetching the next page. If there are no pages left, :exc:`.QueryExhausted` |
| 1992 | + is raised. Also see :attr:`.has_more_pages`. |
| 1993 | +
|
| 1994 | + This should only be called after the first page has been returned. |
| 1995 | + """ |
1976 | 1996 | if not self._paging_state: |
1977 | 1997 | raise QueryExhausted() |
1978 | 1998 |
|
@@ -2388,10 +2408,33 @@ def __str__(self): |
2388 | 2408 |
|
2389 | 2409 |
|
2390 | 2410 | class QueryExhausted(Exception): |
| 2411 | + """ |
| 2412 | + Raised when :meth:`.ResultSet.start_fetching_next_page()` is called and |
| 2413 | + there are no more pages. You can check :attr:`.ResultSet.has_more_pages` |
| 2414 | + before calling to avoid this. |
| 2415 | + """ |
2391 | 2416 | pass |
2392 | 2417 |
|
2393 | 2418 |
|
2394 | 2419 | class PagedResult(object): |
| 2420 | + """ |
| 2421 | + An iterator over the rows from a paged query result. Whenever the number |
| 2422 | + of result rows for a query exceed the :attr:`~.query.Statement.fetch_size` |
| 2423 | + (or :attr:`~.Session.default_fetch_size`, if not set) an instance of this |
| 2424 | + class will be returned. |
| 2425 | +
|
| 2426 | + You can treat this as a normal iterator over rows:: |
| 2427 | +
|
| 2428 | + >>> from cassandra.query import SimpleStatement |
| 2429 | + >>> statement = SimpleStatement("SELECT * FROM users", fetch_size=10) |
| 2430 | + >>> for user_row in session.execute(statement): |
| 2431 | + ... process_user(user_row) |
| 2432 | +
|
| 2433 | + Whenever there are no more rows in the current page, the next page will |
| 2434 | + be fetched transparently. However, note that it _is_ possible for |
| 2435 | + an :class:`Exception` to be raised while fetching the next page, just |
| 2436 | + like you might see on a normal call to ``session.execute()``. |
| 2437 | + """ |
2395 | 2438 |
|
2396 | 2439 | def __init__(self, response_future, initial_response): |
2397 | 2440 | self.response_future = response_future |
|
0 commit comments