'Find the course_indexes which have the specified branch and search_targets. An optional org_target
can be specified to apply an ORG filter to return only the courses that are part of
that ORG.
Returns:
a Cursor if there are no changes in flight or a list if some have changed in current bulk op'

def find_matching_course_indexes(self, branch=None, search_targets=None, org_target=None):

    indexes = self.db_connection.find_matching_course_indexes(branch, search_targets, org_target)
    def _replace_or_append_index(altered_index):
        '\n                                                If    the    index    is    already    in    indexes,    replace    it.    Otherwise,    append    it.\n                                                '
        for (index, existing) in enumerate(indexes):
            if all(((existing[attr] == altered_index[attr]) for attr in ['org', 'course', 'run'])):
                indexes[index] = altered_index
                return
        indexes.append(altered_index)
    for (_, record) in self._active_records:
        if (branch and (branch not in record.index.get('versions', {}))):
            continue
        if search_targets:
            if any(((('search_targets' not in record.index) or (field not in record.index['search_targets']) or (record.index['search_targets'][field] != value)) for (field, value) in search_targets.iteritems())):
                continue
        if org_target:
            if (record.index['org'] != org_target):
                continue
        if (not hasattr(indexes, 'append')):
            indexes = list(indexes)
        _replace_or_append_index(record.index)
    return indexes
