'In mongo some course_keys are used without runs. This helper function returns
a course_key with the run filled in, if the course does actually exist.'

@autoretry_read()
def fill_in_run(self, course_key):

    if (course_key.run is not None):
        return course_key
    cache_key = (course_key.org, course_key.course)
    if (cache_key not in self._course_run_cache):
        matching_courses = list(self.collection.find(SON([('_id.tag', 'i4x'), ('_id.org', course_key.org), ('_id.course', course_key.course), ('_id.category', 'course')])).limit(1))
        if (not matching_courses):
            return course_key
        self._course_run_cache[cache_key] = matching_courses[0]['_id']['name']
    return course_key.replace(run=self._course_run_cache[cache_key])
