djdb/search.py
def fetch_keys_for_query_string(query_str, entity_kind=None):
    """Find entity keys matching a single search term prefix.

    Args:
      query_str: A unicode query string.
      entity_kind: An optional string.  If given, the returned keys are
        restricted to entities of that kind.

    Returns:
      A dict mapping db.Key objects to a set of matching fields.
    """
    parsed = set(_parse_query_string(query_str))
    # The empty query is invalid.
    if not parsed:
        return None
    all_matches = {}
    is_first = True
    # We sort the parsed query so that all of the terms with
    # logic=IS_REQUIRED will be processed first.
    for logic, flavor, arg, field, end in sorted(parsed):
        # Find all of the matches related to this component of the
        # query.
        if flavor == IS_TERM:
            these_matches = fetch_keys_for_one_term(arg, entity_kind, field, end)
        elif flavor == IS_PREFIX:
            these_matches = fetch_keys_for_one_prefix(arg, entity_kind, field)
        else:
            # This should never happen.
            logging.error("Query produced unexpected results: %s", query_str)
            return None
        # Now use this component's matches to update the complete set.
        if logic == IS_REQUIRED:
            if is_first:
                for m, f in these_matches:
                    all_matches[m] = set([f])
            else:
                new_all_matches = {}
                for m, f in these_matches:
                    existing_fs = all_matches.get(m)
                    if existing_fs:
                        new_all_matches[m] = set([f]).union(existing_fs)
                all_matches = new_all_matches
        elif logic == IS_FORBIDDEN:
            if is_first:
                # The first thing we see should not be a negative query part.
                # If so, the query is invalid.
                return None
            for m, _ in these_matches:
                if m in all_matches:
                    del all_matches[m]
        else:
            # This should never happen.
            logging.error("Query produced unexpected results: %s", query_str)
            return None
        is_first = False
        # Is our set of matches empty?  If so, there is no point in
        # processing any more terms.
        if not all_matches:
            break
    return all_matches
Find entity keys matching a single search term prefix.

    Args:
      query_str: A unicode query string.
      entity_kind: An optional string.  If given, the returned keys are
        restricted to entities of that kind.

    Returns:
      A dict mapping db.Key objects to a set of matching fields.
    
Find entity keys matching a single search term prefix.
Find entity keys matching a single search term prefix .
def fetch_keys_for_query_string(query_str, entity_kind=None):
    parsed = set(_parse_query_string(query_str))
    if not parsed:
        return None
    all_matches = {}
    is_first = True
    for logic, flavor, arg, field, end in sorted(parsed):
        if flavor == IS_TERM:
            these_matches = fetch_keys_for_one_term(arg, entity_kind, field, end)
        elif flavor == IS_PREFIX:
            these_matches = fetch_keys_for_one_prefix(arg, entity_kind, field)
        else:
            logging.error("Query produced unexpected results: %s", query_str)
            return None
        if logic == IS_REQUIRED:
            if is_first:
                for m, f in these_matches:
                    all_matches[m] = set([f])
            else:
                new_all_matches = {}
                for m, f in these_matches:
                    existing_fs = all_matches.get(m)
                    if existing_fs:
                        new_all_matches[m] = set([f]).union(existing_fs)
                all_matches = new_all_matches
        elif logic == IS_FORBIDDEN:
            if is_first:
                return None
            for m, _ in these_matches:
                if m in all_matches:
                    del all_matches[m]
        else:
            logging.error("Query produced unexpected results: %s", query_str)
            return None
        is_first = False
        if not all_matches:
            break
    return all_matches
def fetch_keys_for_query_string ( query_str , entity_kind = None ) : parsed = set ( _parse_query_string ( query_str ) ) if not parsed : return None all_matches = { } is_first = True for logic , flavor , arg , field , end in sorted ( parsed ) : if flavor == IS_TERM : these_matches = fetch_keys_for_one_term ( arg , entity_kind , field , end ) elif flavor == IS_PREFIX : these_matches = fetch_keys_for_one_prefix ( arg , entity_kind , field ) else : logging . error ( "Query produced unexpected results: %s" , query_str ) return None if logic == IS_REQUIRED : if is_first : for m , f in these_matches : all_matches [ m ] = set ( [ f ] ) else : new_all_matches = { } for m , f in these_matches : existing_fs = all_matches . get ( m ) if existing_fs : new_all_matches [ m ] = set ( [ f ] ) . union ( existing_fs ) all_matches = new_all_matches elif logic == IS_FORBIDDEN : if is_first : return None for m , _ in these_matches : if m in all_matches : del all_matches [ m ] else : logging . error ( "Query produced unexpected results: %s" , query_str ) return None is_first = False if not all_matches : break return all_matches
Find entity keys matching a single search term prefix.
Find entity keys matching a single search term prefix .
"""Find entity keys matching a single search term prefix.

    Args:
      query_str: A unicode query string.
      entity_kind: An optional string.  If given, the returned keys are
        restricted to entities of that kind.

    Returns:
      A dict mapping db.Key objects to a set of matching fields.
    """ # The empty query is invalid. # We sort the parsed query so that all of the terms with # logic=IS_REQUIRED will be processed first. # Find all of the matches related to this component of the # query. # This should never happen. # Now use this component's matches to update the complete set. # The first thing we see should not be a negative query part. # If so, the query is invalid. # This should never happen. # Is our set of matches empty?  If so, there is no point in # processing any more terms.
A dict mapping db.Key objects to a set of matching fields.
A dict mapping db . Key objects to a set of matching fields .
query_str
A unicode query string.
A unicode query string .
entity_kind
An optional string.  If given, the returned keys are
restricted to entities of that kind.
An optional string . If given the returned keys are restricted to entities of that kind .