(changes, domain=None)
return bad_changes, docs
Take a set of changes and populate them with the documents if necessary.
    :returns: tuple(<changes with missing or out of date documents>, <document list>)
Take a set of changes and populate them with the documents if necessary.
    :returns: tuple(<changes with missing or out of date documents>, <document list>)
Take a set of changes and populate them with the documents if necessary . : returns : tuple ( <changes with missing or out of date documents > <document list > )
def bulk_fetch_changes_docs(changes, domain=None):
    """Take a set of changes and populate them with the documents if necessary.
    :returns: tuple(<changes with missing or out of date documents>, <document list>)
    """
    # break up by doctype
    changes_by_doctype = defaultdict(list)
    for change in changes:
        if domain and change.metadata.domain != domain:
            raise ValueError("Domain does not match change")
        changes_by_doctype[change.metadata.data_source_name].append(change)

    # query
    docs = []
    for _, _changes in changes_by_doctype.items():
        doc_store = _changes[0].document_store
        doc_ids_to_query = [change.id for change in _changes if change.should_fetch_document()]
        new_docs = list(doc_store.iter_documents(doc_ids_to_query))
        docs_queried_prior = [change.document for change in _changes if change.document]
        docs.extend(new_docs + docs_queried_prior)

    # catch missing docs
    bad_changes = set()
    docs_by_id = {doc['_id']: doc for doc in docs}
    for change in changes:
        if change.id not in docs_by_id:
            # we need to capture DocumentMissingError which is not possible in bulk
            #   so let pillow fall back to serial mode to capture the error for missing docs
            bad_changes.add(change)
            continue
        else:
            # set this, so that subsequent doc lookups are avoided
            change.set_document(docs_by_id[change.id])
        try:
            ensure_matched_revisions(change, docs_by_id.get(change.id))
        except DocumentMismatchError:
            bad_changes.add(change)
    return bad_changes, docs
def bulk_fetch_changes_docs ( changes , domain = None ) : # break up by doctype changes_by_doctype = defaultdict ( list ) for change in changes : if domain and change . metadata . domain != domain : raise ValueError ( "Domain does not match change" ) changes_by_doctype [ change . metadata . data_source_name ] . append ( change ) # query docs = [ ] for _ , _changes in changes_by_doctype . items ( ) : doc_store = _changes [ 0 ] . document_store doc_ids_to_query = [ change . id for change in _changes if change . should_fetch_document ( ) ] new_docs = list ( doc_store . iter_documents ( doc_ids_to_query ) ) docs_queried_prior = [ change . document for change in _changes if change . document ] docs . extend ( new_docs + docs_queried_prior ) # catch missing docs bad_changes = set ( ) docs_by_id = { doc [ '_id' ] : doc for doc in docs } for change in changes : if change . id not in docs_by_id : # we need to capture DocumentMissingError which is not possible in bulk #   so let pillow fall back to serial mode to capture the error for missing docs bad_changes . add ( change ) continue else : # set this, so that subsequent doc lookups are avoided change . set_document ( docs_by_id [ change . id ] ) try : ensure_matched_revisions ( change , docs_by_id . get ( change . id ) ) except DocumentMismatchError : bad_changes . add ( change ) return bad_changes , docs