Commit fa94360d by Don Mitchell

Review all fs.files and modulestore indices

and fix some gridfs calls to reduce traffic
parent fecacf8e
...@@ -67,8 +67,8 @@ class MongoContentStore(ContentStore): ...@@ -67,8 +67,8 @@ class MongoContentStore(ContentStore):
def delete(self, location_or_id): def delete(self, location_or_id):
if isinstance(location_or_id, AssetLocation): if isinstance(location_or_id, AssetLocation):
location_or_id = self.asset_db_key(location_or_id) location_or_id = self.asset_db_key(location_or_id)
if self.fs.exists({"_id": location_or_id}): # Deletes of non-existent files are considered successful
self.fs.delete(location_or_id) self.fs.delete(location_or_id)
def find(self, location, throw_on_not_found=True, as_stream=False): def find(self, location, throw_on_not_found=True, as_stream=False):
content_id = self.asset_db_key(location) content_id = self.asset_db_key(location)
...@@ -104,10 +104,8 @@ class MongoContentStore(ContentStore): ...@@ -104,10 +104,8 @@ class MongoContentStore(ContentStore):
def get_stream(self, location): def get_stream(self, location):
content_id = self.asset_db_key(location) content_id = self.asset_db_key(location)
fs_pointer = self.fs_files.find_one({'_id': content_id}, fields={'_id': 1})
try: try:
handle = self.fs.get(fs_pointer['_id']) handle = self.fs.get(content_id)
except NoFile: except NoFile:
raise NotFoundError() raise NotFoundError()
...@@ -239,11 +237,10 @@ class MongoContentStore(ContentStore): ...@@ -239,11 +237,10 @@ class MongoContentStore(ContentStore):
if attr in ['_id', 'md5', 'uploadDate', 'length']: if attr in ['_id', 'md5', 'uploadDate', 'length']:
raise AttributeError("{} is a protected attribute.".format(attr)) raise AttributeError("{} is a protected attribute.".format(attr))
asset_db_key = self.asset_db_key(location) asset_db_key = self.asset_db_key(location)
# FIXME remove fetch and use a form of update which fails if doesn't exist # catch upsert error and raise NotFoundError if asset doesn't exist
item = self.fs_files.find_one({'_id': asset_db_key}) result = self.fs_files.update({'_id': asset_db_key}, {"$set": attr_dict}, upsert=False)
if item is None: if not result.get('updatedExisting', True):
raise NotFoundError(asset_db_key) raise NotFoundError(asset_db_key)
self.fs_files.update({'_id': asset_db_key}, {"$set": attr_dict})
def get_attrs(self, location): def get_attrs(self, location):
""" """
......
...@@ -9,7 +9,6 @@ import shutil ...@@ -9,7 +9,6 @@ import shutil
from tempfile import mkdtemp from tempfile import mkdtemp
from uuid import uuid4 from uuid import uuid4
import unittest import unittest
import bson.son
from xblock.core import XBlock from xblock.core import XBlock
from xblock.fields import Scope, Reference, ReferenceList, ReferenceValueDict from xblock.fields import Scope, Reference, ReferenceList, ReferenceValueDict
......
...@@ -23,11 +23,15 @@ ensureIndex({'schema': 1}) ...@@ -23,11 +23,15 @@ ensureIndex({'schema': 1})
fs.files: fs.files:
========= =========
Index needed thru 'category' by `_get_all_content_for_course` and others. That query also takes a sort
which can be `uploadDate`, `display_name`,
# again, uploadDate may also be a freq sort.
``` ```
ensureIndex({'displayname': 1}) ensureIndex({'_id.tag': 1, '_id.org': 1, '_id.course': 1, '_id.category': 1})
ensureIndex({'_id.tag': 1, '_id.org': 1, '_id.course': 1, '_id.category': 1, '_id.name': 1})
``` ```
Remove index on `displayname`
modulestore: modulestore:
============ ============
...@@ -47,6 +51,17 @@ Because we often scan for all category='course' regardless of the value of the o ...@@ -47,6 +51,17 @@ Because we often scan for all category='course' regardless of the value of the o
ensureIndex({'_id.category': 1}) ensureIndex({'_id.category': 1})
``` ```
Because lms calls get_parent_locations frequently (for path generation):
```
ensureIndex({'_id.tag': 1, '_id.org': 1, '_id.course': 1, 'definition.children': 1})
```
Remove these indices if they exist as I can find no use for them:
```
{ "_id.course": 1, "_id.org": 1, "_id.revision": 1, "definition.children": 1 }
{ "definition.children": 1 }
```
NOTE, that index will only aid queries which provide the keys in exactly that form and order. The query can NOTE, that index will only aid queries which provide the keys in exactly that form and order. The query can
omit later fields of the query but not earlier. Thus ```modulestore.find({'_id.org': 'myu'})``` will not use omit later fields of the query but not earlier. Thus ```modulestore.find({'_id.org': 'myu'})``` will not use
the index as it omits the tag. As soon as mongo comes across an index field omitted from the query, it stops the index as it omits the tag. As soon as mongo comes across an index field omitted from the query, it stops
...@@ -56,14 +71,17 @@ for matches to the category. ...@@ -56,14 +71,17 @@ for matches to the category.
To find out if any records have the wrong id structure, run To find out if any records have the wrong id structure, run
``` ```
db.modulestore.find({$where: function() { db.fs.files.find({uploadDate: {$gt: startDate, $lt: endDate},
var keys = Object.keys(this['_id']); $where: function() {
var ref = ['tag', 'org', 'course', 'category', 'name', 'revision']; var keys = ['category', 'name', 'course', 'tag', 'org', 'revision'];
for (var i=0; i < ref.length; i++) { for (var key in this._id) {
if (keys[i] != ref[i]) return true; if (key != keys.shift()) {
} return true;
return false; }}, }
{_id: 1}) }
return false;
}},
{_id: 1})
``` ```
modulestore.active_versions modulestore.active_versions
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment