Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement #123 arbitrary Mongo URL & collection name #124

Merged
merged 3 commits into from Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions datasketch/experimental/aio/storage.py
Expand Up @@ -128,10 +128,17 @@ def __init__(self, config, name=None):
self._mongo_param = self._parse_config(self._config['mongo'])

self._name = name if name else _random_name(11).decode('utf-8')
self._collection_name = 'lsh_' + self._name
if 'collection_name' in self.mongo_param:
self._collection_name = self.mongo_param['collection_name']
elif 'collection_prefix' in self.mongo_param:
self._collection_name = self.mongo_param['collection_prefix'] + self._name
else:
self._collection_name = 'lsh_' + self._name

db_lsh = self.mongo_param['db'] if 'db' in self.mongo_param else 'db_0'
if 'replica_set' in self.mongo_param:
if 'url' in self.mongo_param:
dsn = self.mongo_param['url']
elif 'replica_set' in self.mongo_param:
dsn = 'mongodb://{replica_set_nodes}/?replicaSet={replica_set}'.format(**self.mongo_param)
elif 'username' in self.mongo_param or 'password' in self.mongo_param:
dsn = 'mongodb://{username}:{password}@{host}:{port}'.format(**self.mongo_param)
Expand All @@ -142,7 +149,7 @@ def __init__(self, config, name=None):

self._batch_size = 1000
self._mongo_client = motor.motor_asyncio.AsyncIOMotorClient(dsn, **additional_args)
self._collection = self._mongo_client[db_lsh][self._collection_name]
self._collection = self._mongo_client.get_default_database(db_lsh).get_collection(self._collection_name)
self._initialized = True
self._buffer = AsyncMongoBuffer(self._collection, self._batch_size)

Expand Down
6 changes: 6 additions & 0 deletions docs/_sources/lsh.rst.txt
Expand Up @@ -291,6 +291,12 @@ To configure Asynchronous MongoDB storage that will connect to a `replica set <h

_storage = {'type': 'aiomongo', 'mongo': {'replica_set': 'rs0', 'replica_set_nodes': 'node1:port1,node2:port2,node3:port3'}}

To connect to a cloud Mongo Atlas cluster (or any other arbitrary ``mongodb`` URI):

.. code:: python

_storage = {'type': 'aiomongo', 'mongo': {'url': 'mongodb+srv://user:pass@server-ybq4y.mongodb.net/db'}}

If you want to pass additional params to the `Mongo client <http://api.mongodb.com/python/current/api/pymongo/mongo_client.html>` constructor, just put them in the ``mongo.args`` object in the storage config (example usage to configure X509 authentication):

.. code:: python
Expand Down