nodes: 0, module; 1, function_definition; 2, function_name:_filter_keys; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, function_definition; 11, return_statement; 12, identifier:content_id; 13, None; 14, identifier:prefix; 15, None; 16, identifier:subtopic_id; 17, None; 18, string; 19, function_name:accept; 20, parameters; 21, block; 22, identifier:accept; 23, string_content: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.; 24, identifier:kvp; 25, expression_statement; 26, comment:# In general we'll accept the label if; 27, comment:# it's the natural order; l.content_id1 == cid1; 28, expression_statement; 29, if_statement; 30, comment:# If we're not looking for subtopic IDs, then accept records; 31, comment:# matching the content ID that are in natural order; 32, if_statement; 33, comment:# The scan range doesn't include subtopic IDs (the key schema; 34, comment:# is oriented towards querying content-to-content labels); 35, comment:# so we need to accept records where either part matches; 36, comment:# (if both parts match then the record is its own dual); 37, if_statement; 38, if_statement; 39, return_statement; 40, assignment; 41, assignment; 42, comparison_operator:content_id is not None; 43, comment:# We will only see tuples where content_id1 is the; 44, comment:# requested content ID; 45, block; 46, elif_clause; 47, elif_clause; 48, comparison_operator:subtopic_id is None; 49, block; 50, comparison_operator:subtopic_id == subtopic_id1; 51, block; 52, boolean_operator; 53, block; 54, False; 55, tuple_pattern; 56, subscript; 57, identifier:is_sorted; 58, parenthesized_expression; 59, identifier:content_id; 60, None; 61, assert_statement; 62, comparison_operator:prefix is not None; 63, block; 64, not_operator; 65, comment:# We'll see both orderings of everything, reject the; 66, comment:# unsorted ones; 67, block; 68, identifier:subtopic_id; 69, None; 70, if_statement; 71, return_statement; 72, identifier:subtopic_id; 73, identifier:subtopic_id1; 74, return_statement; 75, comparison_operator:content_id == content_id2; 76, comparison_operator:subtopic_id == subtopic_id2; 77, return_statement; 78, identifier:content_id1; 79, identifier:content_id2; 80, identifier:subtopic_id1; 81, identifier:subtopic_id2; 82, identifier:annotator_id; 83, identifier:inverted_epoch_ticks; 84, identifier:kvp; 85, integer:0; 86, boolean_operator; 87, comparison_operator:content_id1 == content_id; 88, identifier:prefix; 89, None; 90, assert_statement; 91, comment:# If we'll see both orderings of this record, only; 92, comment:# accept the sorted one; 93, if_statement; 94, identifier:is_sorted; 95, return_statement; 96, comparison_operator:content_id2 != content_id; 97, block; 98, comparison_operator:subtopic_id1 <= subtopic_id2; 99, True; 100, identifier:content_id; 101, identifier:content_id2; 102, identifier:subtopic_id; 103, identifier:subtopic_id2; 104, True; 105, comparison_operator:content_id1 < content_id2; 106, parenthesized_expression; 107, identifier:content_id1; 108, identifier:content_id; 109, call; 110, boolean_operator; 111, block; 112, False; 113, identifier:content_id2; 114, identifier:content_id; 115, return_statement; 116, comment:# will never see its dual; 117, identifier:subtopic_id1; 118, identifier:subtopic_id2; 119, identifier:content_id1; 120, identifier:content_id2; 121, boolean_operator; 122, attribute; 123, argument_list; 124, call; 125, not_operator; 126, return_statement; 127, True; 128, comparison_operator:content_id1 == content_id2; 129, comparison_operator:subtopic_id1 <= subtopic_id2; 130, identifier:content_id1; 131, identifier:startswith; 132, identifier:prefix; 133, attribute; 134, argument_list; 135, identifier:is_sorted; 136, False; 137, identifier:content_id1; 138, identifier:content_id2; 139, identifier:subtopic_id1; 140, identifier:subtopic_id2; 141, identifier:content_id2; 142, identifier:startswith; 143, identifier:prefix
code: 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