Skip to content

Commit

Permalink
db: create indexes on fields we are going to use in filters
Browse files Browse the repository at this point in the history
Indexes will be created in the "background": read / write operations
are allowed on the documents of the collection, while the new indexes
are being built.

At the same time, one can't drop a collection, while an index build
is in progress, thus, replace all drop()'s in tests with remove()'s,
so that we don't drop the collection between tests and don't have
to rebuild indexes again.
  • Loading branch information
Roman Podoliaka committed Feb 28, 2016
1 parent 09e359a commit 95e5b68
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
16 changes: 13 additions & 3 deletions tests/resources/test_snippets.py
Expand Up @@ -46,7 +46,7 @@ class TestSnippets(metaclass=AIOTestMeta):
async def test_get_no_snippets(self):
app = create_app(self.conf)

await app['db'].snippets.drop()
await app['db'].snippets.remove()

async with AIOTestApp(app) as testapp:
resp = await testapp.get('/snippets')
Expand All @@ -57,7 +57,7 @@ async def test_get_no_snippets(self):
async def test_get_snippets(self):
app = create_app(self.conf)

await app['db'].snippets.drop()
await app['db'].snippets.remove()
await app['db'].snippets.insert(self.snippets)

async with AIOTestApp(app) as testapp:
Expand All @@ -69,7 +69,7 @@ async def test_get_snippets(self):
async def test_post_snippet(self):
app = create_app(self.conf)

await app['db'].snippets.drop()
await app['db'].snippets.remove()

async with AIOTestApp(app) as testapp:
snippet = copy.deepcopy(self.snippets[0])
Expand All @@ -91,3 +91,13 @@ async def test_post_snippet(self):
)

assert snippet_resp == snippet_db

async def test_data_model_indexes_exist(self):
app = create_app(self.conf)

res = await app['db'].snippets.index_information()

assert res['author_idx']['key'] == [('author_id', 1)]
assert res['tags_idx']['key'] == [('tags', 1)]
assert res['updated_idx']['key'] == [('updated_at', -1)]
assert res['created_idx']['key'] == [('created_at', -1)]
16 changes: 16 additions & 0 deletions xsnippet_api/database.py
Expand Up @@ -13,6 +13,7 @@

from motor.motor_asyncio import AsyncIOMotorClient
from pymongo.son_manipulator import SONManipulator
import pymongo


def create_connection(conf, loop=None):
Expand Down Expand Up @@ -44,6 +45,21 @@ def create_connection(conf, loop=None):
# is doing this implicitly for us on application level.
db.add_son_manipulator(_AutoincrementId())

# ensure necessary indexes exist. background=True allows operations
# read/write operations on collections while indexes are being built
db.snippets.create_index('author_id',
name='author_idx',
background=True)
db.snippets.create_index('tags',
name='tags_idx',
background=True),
db.snippets.create_index([('created_at', pymongo.DESCENDING)],
name='created_idx',
background=True)
db.snippets.create_index([('updated_at', pymongo.DESCENDING)],
name='updated_idx',
background=True)

return db


Expand Down

0 comments on commit 95e5b68

Please sign in to comment.