Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #14 from smacintyre/master
Browse files Browse the repository at this point in the history
Adding support querying slave nodes
  • Loading branch information
jehiah committed Sep 8, 2011
2 parents 3595297 + b59201c commit 288a794
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion asyncmongo/client.py
Expand Up @@ -36,7 +36,7 @@ class Client(object):
- `**kwargs`: passed to `connection.Connection` - `**kwargs`: passed to `connection.Connection`
- `host`: hostname or ip of mongo host - `host`: hostname or ip of mongo host
- `port`: port to connect to - `port`: port to connect to
- `slave_ok` (optional): is it okay to connect directly to and perform queries on a slave instance - `slave_okay` (optional): is it okay to connect directly to and perform queries on a slave instance
- `autoreconnect` (optional): auto reconnect on interface errors - `autoreconnect` (optional): auto reconnect on interface errors
@returns a `Client` instance that wraps a `pool.ConnectionPool` @returns a `Client` instance that wraps a `pool.ConnectionPool`
Expand Down
6 changes: 1 addition & 5 deletions asyncmongo/connection.py
Expand Up @@ -35,19 +35,16 @@ class Connection(object):
:Parameters: :Parameters:
- `host`: hostname or ip of mongo host - `host`: hostname or ip of mongo host
- `port`: port to connect to - `port`: port to connect to
- `slave_ok` (optional): is it okay to connect directly to and perform queries on a slave instance
- `autoreconnect` (optional): auto reconnect on interface errors - `autoreconnect` (optional): auto reconnect on interface errors
""" """
def __init__(self, host, port, slave_ok=False, autoreconnect=True, pool=None): def __init__(self, host, port, autoreconnect=True, pool=None):
assert isinstance(host, (str, unicode)) assert isinstance(host, (str, unicode))
assert isinstance(port, int) assert isinstance(port, int)
assert isinstance(slave_ok, bool)
assert isinstance(autoreconnect, bool) assert isinstance(autoreconnect, bool)
assert pool assert pool
self.__host = host self.__host = host
self.__port = port self.__port = port
self.__slave_ok = slave_ok
self.__stream = None self.__stream = None
self.__callback = None self.__callback = None
self.__alive = False self.__alive = False
Expand Down Expand Up @@ -159,4 +156,3 @@ def _parse_response(self, response):
# logging.info('response: %s' % response) # logging.info('response: %s' % response)
callback(response) callback(response)



8 changes: 6 additions & 2 deletions asyncmongo/cursor.py
Expand Up @@ -40,6 +40,7 @@ def __init__(self, dbname, collection, pool):
self.__dbname = dbname self.__dbname = dbname
self.__collection = collection self.__collection = collection
self.__pool = pool self.__pool = pool
self.__slave_okay = False


@property @property
def full_collection_name(self): def full_collection_name(self):
Expand Down Expand Up @@ -267,7 +268,7 @@ def find_one(self, spec_or_id, **kwargs):


def find(self, spec=None, fields=None, skip=0, limit=0, def find(self, spec=None, fields=None, skip=0, limit=0,
timeout=True, snapshot=False, tailable=False, sort=None, timeout=True, snapshot=False, tailable=False, sort=None,
max_scan=None, max_scan=None, slave_okay=False,
_must_use_master=False, _is_command=False, _must_use_master=False, _is_command=False,
callback=None): callback=None):
"""Query the database. """Query the database.
Expand Down Expand Up @@ -322,6 +323,8 @@ def find(self, spec=None, fields=None, skip=0, limit=0,
:meth:`~pymongo.cursor.Cursor.sort` for details. :meth:`~pymongo.cursor.Cursor.sort` for details.
- `max_scan` (optional): limit the number of documents - `max_scan` (optional): limit the number of documents
examined when performing the query examined when performing the query
- `slave_okay` (optional): is it okay to connect directly
to and perform queries on a slave instance
.. mongodoc:: find .. mongodoc:: find
""" """
Expand Down Expand Up @@ -361,6 +364,7 @@ def find(self, spec=None, fields=None, skip=0, limit=0,
self.__snapshot = snapshot self.__snapshot = snapshot
self.__ordering = sort and helpers._index_document(sort) or None self.__ordering = sort and helpers._index_document(sort) or None
self.__max_scan = max_scan self.__max_scan = max_scan
self.__slave_okay = slave_okay
self.__explain = False self.__explain = False
self.__hint = None self.__hint = None
# self.__as_class = as_class # self.__as_class = as_class
Expand Down Expand Up @@ -394,7 +398,7 @@ def __query_options(self):
options = 0 options = 0
if self.__tailable: if self.__tailable:
options |= _QUERY_OPTIONS["tailable_cursor"] options |= _QUERY_OPTIONS["tailable_cursor"]
if False: #self.__collection.database.connection.slave_okay: if self.__slave_okay or self.__pool.slave_okay:
options |= _QUERY_OPTIONS["slave_okay"] options |= _QUERY_OPTIONS["slave_okay"]
if not self.__timeout: if not self.__timeout:
options |= _QUERY_OPTIONS["no_timeout"] options |= _QUERY_OPTIONS["no_timeout"]
Expand Down
13 changes: 13 additions & 0 deletions asyncmongo/pool.py
Expand Up @@ -66,12 +66,14 @@ def __init__(self,
maxconnections=0, maxconnections=0,
maxusage=0, maxusage=0,
dbname=None, dbname=None,
slave_okay=False,
*args, **kwargs): *args, **kwargs):
assert isinstance(mincached, int) assert isinstance(mincached, int)
assert isinstance(maxcached, int) assert isinstance(maxcached, int)
assert isinstance(maxconnections, int) assert isinstance(maxconnections, int)
assert isinstance(maxusage, int) assert isinstance(maxusage, int)
assert isinstance(dbname, (str, unicode, None.__class__)) assert isinstance(dbname, (str, unicode, None.__class__))
assert isinstance(slave_okay, bool)
if mincached and maxcached: if mincached and maxcached:
assert mincached <= maxcached assert mincached <= maxcached
if maxconnections: if maxconnections:
Expand All @@ -85,6 +87,7 @@ def __init__(self,
self._idle_cache = [] # the actual connections that can be used self._idle_cache = [] # the actual connections that can be used
self._condition = Condition() self._condition = Condition()
self._dbname = dbname self._dbname = dbname
self._slave_okay = slave_okay
self._connections = 0 self._connections = 0


# Establish an initial number of idle database connections: # Establish an initial number of idle database connections:
Expand Down Expand Up @@ -153,4 +156,14 @@ def close(self):
finally: finally:
self._condition.release() self._condition.release()


def __get_slave_okay(self):
"""Is it OK to perform queries on a secondary or slave?
"""
return self._slave_okay

def __set_slave_okay(self, value):
"""Property setter for slave_okay"""
assert isinstance(value, bool)
self._slave_okay = value


slave_okay = property(__get_slave_okay, __set_slave_okay)

0 comments on commit 288a794

Please sign in to comment.