Skip to content
Snippets Groups Projects
Commit 853a4526 authored by Calen Pennington's avatar Calen Pennington
Browse files

Use appropriate indexes in mongo modulestore

parent f0a049a2
No related branches found
Tags release-2021-02-03-12.16
No related merge requests found
......@@ -128,6 +128,10 @@ class Location(_LocationBase):
return "-".join(str(v) for v in self.list() if v is not None).replace('.', '_')
def dict(self):
"""
Return an OrderedDict of this locations keys and values. The order is
tag, org, course, category, name, revision
"""
return self._asdict()
def list(self):
......
......@@ -4,18 +4,21 @@ from importlib import import_module
from xmodule.x_module import XModuleDescriptor
from xmodule.mako_module import MakoDescriptorSystem
from mitxmako.shortcuts import render_to_string
from bson.son import SON
from itertools import repeat
from . import ModuleStore, Location
from .exceptions import ItemNotFoundError, InsufficientSpecificationError
# TODO (cpennington): This code currently operates under the assumption that
# there is only one revision for each item. Once we start versioning inside the CMS,
# that assumption will have to change
def location_to_query(loc):
query = {}
query = SON()
# Location dict is ordered by specificity, and SON
# will preserve that order for queries
for key, val in Location(loc).dict().iteritems():
if val is not None:
query['_id.{key}'.format(key=key)] = val
......@@ -35,6 +38,10 @@ class MongoModuleStore(ModuleStore):
# Force mongo to report errors, at the expense of performance
self.collection.safe = True
# Force mongo to maintain an index over _id.* that is in the same order
# that is used when querying by a location
self.collection.ensure_index(zip(('_id.' + field for field in Location._fields), repeat(1)))
module_path, _, class_name = default_class.rpartition('.')
class_ = getattr(import_module(module_path), class_name)
self.default_class = class_
......@@ -77,7 +84,6 @@ class MongoModuleStore(ModuleStore):
return self._load_item(item)
def get_items(self, location, default_class=None):
print location_to_query(location)
items = self.collection.find(
location_to_query(location),
sort=[('revision', pymongo.ASCENDING)],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment