def _filter_keys(self, content_id=None, prefix=None, subtopic_id=None):
        '''Filter out-of-order labels by key tuple.

        :class:`Label` always sorts by `(cid1,cid2,sid1,sid2)`, but
        for efficient lookups on `cid2` this class also stores in
        order `(cid2,cid1,sid2,sid1)`.  Filter out things that are
        in the wrong order.  But, if an original query specified
        `content_id` or `subtopic_id`, account for the possibility
        that something might be apparently out-of-order but its
        dual will not be in the query at all.

        '''
        def accept(kvp):
            (content_id1, content_id2, subtopic_id1, subtopic_id2,
             annotator_id, inverted_epoch_ticks) = kvp[0]
            # In general we'll accept the label if
            # it's the natural order; l.content_id1 == cid1
            is_sorted = (content_id1 < content_id2 or
                         (content_id1 == content_id2 and
                          subtopic_id1 <= subtopic_id2))
            if content_id is not None:
                # We will only see tuples where content_id1 is the
                # requested content ID
                assert content_id1 == content_id
            elif prefix is not None:
                assert content_id1.startswith(prefix)
                # If we'll see both orderings of this record, only
                # accept the sorted one
                if content_id2.startswith(prefix) and not is_sorted:
                    return False
            elif not is_sorted:
                # We'll see both orderings of everything, reject the
                # unsorted ones
                return False

            # If we're not looking for subtopic IDs, then accept records
            # matching the content ID that are in natural order
            if subtopic_id is None:
                if content_id2 != content_id:
                    return True  # will never see its dual
                return subtopic_id1 <= subtopic_id2
            # The scan range doesn't include subtopic IDs (the key schema
            # is oriented towards querying content-to-content labels)
            # so we need to accept records where either part matches
            # (if both parts match then the record is its own dual)
            if subtopic_id == subtopic_id1:
                return True
            if content_id == content_id2 and subtopic_id == subtopic_id2:
                return True
            return False
        return accept
Filter out-of-order labels by key tuple.

        :class:`Label` always sorts by `(cid1,cid2,sid1,sid2)`, but
        for efficient lookups on `cid2` this class also stores in
        order `(cid2,cid1,sid2,sid1)`.  Filter out things that are
        in the wrong order.  But, if an original query specified
        `content_id` or `subtopic_id`, account for the possibility
        that something might be apparently out-of-order but its
        dual will not be in the query at all.