def index(run):
    """
    Turn a Bluesky run into a dictionary of documents, indexed by
    (name, doc['uid'])
    """
    indexed = {}
    for name, doc in run:
        if name == "start":
            indexed["uid"] = doc["uid"]
        if name == "resource":
            # Check for an extraneous duplicate key in old documents.
            if "id" in doc:
                assert doc["id"] == doc["uid"]
                doc = doc.copy()
                doc.pop("id")
            indexed[(name, doc["uid"])] = doc.copy()
        elif name == "datum":
            indexed[("datum", doc["datum_id"])] = doc.copy()
        # v0 yields {'_name": 'RunStop'} if the stop doc is missing; v2 yields
        # None.
        elif name == "stop" and doc is None or "uid" not in doc:
            indexed[(name, None)] = None
        else:
            indexed[(name, doc["uid"])] = doc.copy()
    return indexed