Skip to content

Commit

Permalink
Merge branch 'pr/44'
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelnicolay committed Jun 11, 2014
2 parents 01ef176 + ca5fbbd commit 9baa0a8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
30 changes: 15 additions & 15 deletions mongotor/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ def insert(self, doc_or_docs, safe=True, check_keys=True, callback=None):
assert isinstance(doc_or_docs, list)

message_insert = message.insert(self._collection_name, doc_or_docs,
check_keys, safe, {})
check_keys, safe, {})

log.debug("mongo: db.{0}.insert({1})".format(self._collection_name, doc_or_docs))

node = yield gen.Task(self._database.get_node, ReadPreference.PRIMARY)
connection = yield gen.Task(node.connection)

response, error = yield gen.Task(connection.send_message,
message_insert, safe)
message_insert, safe)

if callback:
callback((response, error))
Expand All @@ -80,21 +80,21 @@ def remove(self, spec_or_id={}, safe=True, callback=None):
assert isinstance(spec_or_id, dict)

message_delete = message.delete(self._collection_name, spec_or_id,
safe, {})
safe, {})

log.debug("mongo: db.{0}.remove({1})".format(self._collection_name, spec_or_id))
node = yield gen.Task(self._database.get_node, ReadPreference.PRIMARY)
connection = yield gen.Task(node.connection)

response, error = yield gen.Task(connection.send_message,
message_delete, safe)
message_delete, safe)

if callback:
callback((response, error))

@gen.engine
def update(self, spec, document, upsert=False, safe=True,
multi=False, callback=None):
multi=False, callback=None):
"""Update a document(s) in this collection.
:Parameters:
Expand All @@ -120,7 +120,7 @@ def update(self, spec, document, upsert=False, safe=True,
assert isinstance(safe, bool), "safe must be an instance of bool"

message_update = message.update(self._collection_name, upsert,
multi, spec, document, safe, {})
multi, spec, document, safe, {})

log.debug("mongo: db.{0}.update({1}, {2}, {3}, {4})".format(
self._collection_name, spec, document, upsert, multi))
Expand All @@ -129,7 +129,7 @@ def update(self, spec, document, upsert=False, safe=True,
connection = yield gen.Task(node.connection)

response, error = yield gen.Task(connection.send_message,
message_update, safe)
message_update, safe)

callback((response, error))

Expand Down Expand Up @@ -207,8 +207,7 @@ def find(self, *args, **kwargs):
sort=kwargs.get('sort', {}),
limit=kwargs.get('limit', '')
))
cursor = Cursor(self._database, self._collection,
*args, **kwargs)
cursor = Cursor(self._database, self._collection, *args, **kwargs)

if 'callback' in kwargs:
cursor.find(callback=kwargs['callback'])
Expand Down Expand Up @@ -258,15 +257,15 @@ def aggregate(self, pipeline, read_preference=None, callback=None):
if isinstance(pipeline, dict):
pipeline = [pipeline]

response, error = yield gen.Task(self._database.command,
"aggregate", self._collection, pipeline=pipeline,
read_preference=read_preference)
response, error = yield gen.Task(self._database.command, "aggregate",
self._collection, pipeline=pipeline,
read_preference=read_preference)

callback(response)

@gen.engine
def group(self, key, condition, initial, reduce, finalize=None,
read_preference=None, callback=None):
read_preference=None, callback=None):
"""Perform a query similar to an SQL *group by* operation.
Returns an array of grouped items.
Expand Down Expand Up @@ -304,7 +303,8 @@ def group(self, key, condition, initial, reduce, finalize=None,
if finalize is not None:
group["finalize"] = Code(finalize)

response, error = yield gen.Task(self._database.command,
"group", group, read_preference=read_preference)
response, error = yield gen.Task(self._database.command, "group",
group,
read_preference=read_preference)

callback(response)
4 changes: 2 additions & 2 deletions mongotor/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from __future__ import with_statement
from tornado import iostream
from tornado import stack_context
from mongotor.errors import (InterfaceError,
IntegrityError, ProgrammingError, DatabaseError)
from mongotor.errors import InterfaceError, IntegrityError, \
ProgrammingError, DatabaseError
from mongotor import helpers
import socket
import logging
Expand Down
6 changes: 3 additions & 3 deletions mongotor/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def disconnect(cls):
@gen.engine
@initialized
def send_message(self, message, read_preference=None,
with_response=True, callback=None):
with_response=True, callback=None):
node = yield gen.Task(self.get_node, read_preference)

connection = yield gen.Task(node.connection)
Expand Down Expand Up @@ -194,7 +194,7 @@ def get_node(self, read_preference=None, callback=None):

@initialized
def command(self, command, value=1, read_preference=None,
callback=None, check=True, allowable_errors=[], **kwargs):
callback=None, check=True, allowable_errors=[], **kwargs):
"""Issue a MongoDB command.
Send command `command` to the database and return the
Expand Down Expand Up @@ -248,7 +248,7 @@ def command(self, command, value=1, read_preference=None,
self._command(command, read_preference=read_preference, callback=callback)

def _command(self, command, read_preference=None,
connection=None, callback=None):
connection=None, callback=None):

if read_preference is None:
read_preference = self._read_preference
Expand Down
10 changes: 5 additions & 5 deletions mongotor/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, host, port, database, pool_kargs=None):
self.initialized = False

self.pool = ConnectionPool(self.host, self.port, self.database.dbname,
**self.pool_kargs)
**self.pool_kargs)

@gen.engine
def config(self, callback=None):
Expand All @@ -62,12 +62,12 @@ def config(self, callback=None):
# create a connection on the fly if pool is full
connection = Connection(host=self.host, port=self.port)
response, error = yield gen.Task(self.database._command, ismaster,
connection=connection)
connection=connection)
if not connection._pool: # if connection is created on the fly
connection.close()
except InterfaceError, ie:
logger.error('oops, database node {host}:{port} is unavailable: {error}' \
.format(host=self.host, port=self.port, error=ie))
logger.error('oops, database node {host}:{port} is unavailable: {error}'
.format(host=self.host, port=self.port, error=ie))

if response:
self.is_primary = response.get('ismaster', True)
Expand All @@ -87,7 +87,7 @@ def disconnect(self):
def __repr__(self):
return """MongoDB node {host}:{port} ({primary}, {secondary})""" \
.format(host=self.host, port=self.port, primary=self.is_primary,
secondary=self.is_secondary)
secondary=self.is_secondary)

def connection(self, callback):
"""Return one connection from pool
Expand Down
4 changes: 2 additions & 2 deletions mongotor/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ConnectionPool(object):
"""
def __init__(self, host, port, dbname, maxconnections=0, maxusage=0,
autoreconnect=True):
autoreconnect=True):

assert isinstance(host, (str, unicode))
assert isinstance(port, int)
Expand Down Expand Up @@ -65,7 +65,7 @@ def __repr__(self):
def _create_connection(self):
log.debug('{0} creating new connection'.format(self))
return Connection(host=self._host, port=self._port, pool=self,
autoreconnect=self._autoreconnect)
autoreconnect=self._autoreconnect)

def connection(self, callback=None, retries=0):
"""Get a connection from pool
Expand Down

0 comments on commit 9baa0a8

Please sign in to comment.