def _Dynamic_ListIndexes(self, request, response):
        """A local implementation of SearchService.ListIndexes RPC.

    Args:
      request: A search_service_pb.ListIndexesRequest.
      response: An search_service_pb.ListIndexesResponse.
    """

        if request.has_app_id():
            if random.choice([True] + [False] * 9):
                raise apiproxy_errors.ResponseTooLargeError()

            for _ in xrange(random.randint(0, 2) * random.randint(5, 15)):
                new_index_spec = response.add_index_metadata(
                ).mutable_index_spec()
                new_index_spec.set_name(
                    random.choice(list(search._ASCII_PRINTABLE - set('!'))) +
                    ''.join(
                        random.choice(list(search._ASCII_PRINTABLE))
                        for _ in xrange(
                            random.randint(
                                0, search._MAXIMUM_INDEX_NAME_LENGTH))))
                new_index_spec.set_consistency(
                    random.choice([
                        search_service_pb.IndexSpec.GLOBAL,
                        search_service_pb.IndexSpec.PER_DOCUMENT
                    ]))
            response.mutable_status().set_status(
                random.choice(
                    [search_service_pb.SearchServiceError.OK] * 10 +
                    [search_service_pb.SearchServiceError.TRANSIENT_ERROR] +
                    [search_service_pb.SearchServiceError.INTERNAL_ERROR]))
            return

        for index in self.__indexes.values():
            index_spec = index.IndexSpec
            new_index_spec = response.add_index_metadata().mutable_index_spec()
            new_index_spec.set_name(index_spec.name())
            new_index_spec.set_consistency(index_spec.consistency())
        response.mutable_status().set_status(
            search_service_pb.SearchServiceError.OK)
    def _RandomSearchResponse(self, request, response):

        random.seed()
        if random.random() < 0.03:
            raise apiproxy_errors.ResponseTooLargeError()
        response.mutable_status().set_code(
            random.choice(
                [search_service_pb.SearchServiceError.OK] * 30 +
                [search_service_pb.SearchServiceError.TRANSIENT_ERROR] +
                [search_service_pb.SearchServiceError.INTERNAL_ERROR]))

        params = request.params()
        random.seed(params.query())
        total = random.randint(0, 100)

        if random.random() < 0.3:
            total = 0

        offset = 0
        if params.has_offset():
            offset = params.offset()

        remaining = max(0, total - offset)
        nresults = min(remaining, params.limit())
        matched_count = offset + nresults
        if remaining > nresults:
            matched_count += random.randint(1, 100)

        def RandomText(charset, min_len, max_len):
            return ''.join(
                random.choice(charset)
                for _ in xrange(random.randint(min_len, max_len)))

        for i in xrange(nresults):
            seed = '%s:%s' % (params.query(), i + offset)
            random.seed(seed)
            result = response.add_result()
            doc = result.mutable_document()
            doc_id = RandomText(string.letters + string.digits, 8, 10)
            doc.set_id(doc_id)
            random.seed(doc_id)
            for _ in params.sort_spec_list():
                result.add_score(random.random())

            for name, probability in [('creator', 0.90),
                                      ('last_change', 0.40)]:
                if random.random() < probability:
                    field = doc.add_field()
                    field.set_name(name)
                    value = field.mutable_value()
                    value.set_type(document_pb.FieldValue.TEXT)
                    value.set_string_value(
                        RandomText(string.letters + string.digits, 2, 10) +
                        '@google.com')

            field = doc.add_field()
            field.set_name('content')
            value = field.mutable_value()
            value.set_type(document_pb.FieldValue.TEXT)
            value.set_string_value(
                RandomText(string.printable, 0, 15) + params.query() +
                RandomText(string.printable + 10 * string.whitespace, 5, 5000))

            for i in xrange(random.randint(0, 2)):
                field = doc.add_field()
                field.set_name(RandomText(string.letters, 3, 7))
                value = field.mutable_value()
                value.set_type(document_pb.FieldValue.TEXT)
                value.set_string_value(RandomText(string.printable, 0, 100))

        response.set_matched_count(matched_count)
    def _Dynamic_ListIndexes(self, request, response):
        """A local implementation of SearchService.ListIndexes RPC.

    Args:
      request: A search_service_pb.ListIndexesRequest.
      response: An search_service_pb.ListIndexesResponse.

    Raises:
      ResponseTooLargeError: raised for testing admin console.
    """

        if request.has_app_id():
            if random.choice([True] + [False] * 9):
                raise apiproxy_errors.ResponseTooLargeError()

            for _ in xrange(random.randint(0, 2) * random.randint(5, 15)):
                new_index_spec = response.add_index_metadata(
                ).mutable_index_spec()
                new_index_spec.set_name(
                    random.choice(list(_VISIBLE_PRINTABLE_ASCII - set('!'))) +
                    ''.join(
                        random.choice(list(_VISIBLE_PRINTABLE_ASCII))
                        for _ in xrange(
                            random.randint(0,
                                           search.MAXIMUM_INDEX_NAME_LENGTH))))
            response.mutable_status().set_code(
                random.choice(
                    [search_service_pb.SearchServiceError.OK] * 10 +
                    [search_service_pb.SearchServiceError.TRANSIENT_ERROR] +
                    [search_service_pb.SearchServiceError.INTERNAL_ERROR]))
            return

        response.mutable_status().set_code(
            search_service_pb.SearchServiceError.OK)

        namespace = self._GetNamespace(request.params().namespace())
        if namespace not in self.__indexes or not self.__indexes[namespace]:
            return

        keys, indexes = zip(
            *sorted(self.__indexes[namespace].iteritems(), key=lambda v: v[0]))
        position = 0
        params = request.params()
        if params.has_start_index_name():
            position = bisect.bisect_left(keys, params.start_index_name())
            if (not params.include_start_index() and position < len(keys)
                    and keys[position] == params.start_index_name()):
                position += 1
        elif params.has_index_name_prefix():
            position = bisect.bisect_left(keys, params.index_name_prefix())
        if params.has_offset():
            position += params.offset()
        end_position = position + params.limit()
        prefix = params.index_name_prefix()
        for index in indexes[min(position, len(keys)
                                 ):min(end_position, len(keys))]:
            index_spec = index.index_spec
            if prefix and not index_spec.name().startswith(prefix):
                break
            metadata = response.add_index_metadata()
            new_index_spec = metadata.mutable_index_spec()
            new_index_spec.set_name(index_spec.name())
            new_index_spec.set_namespace(index_spec.namespace())
            if params.fetch_schema():
                self._AddSchemaInformation(index, metadata)
            self._AddStorageInformation(index, metadata)
Example #4
0
    def _Dynamic_ListIndexes(self, request, response):
        """A local implementation of SearchService.ListIndexes RPC.

    Args:
      request: A search_service_pb.ListIndexesRequest.
      response: An search_service_pb.ListIndexesResponse.
    """

        if request.has_app_id():
            if random.choice([True] + [False] * 9):
                raise apiproxy_errors.ResponseTooLargeError()

            for _ in xrange(random.randint(0, 2) * random.randint(5, 15)):
                new_index_spec = response.add_index_metadata(
                ).mutable_index_spec()
                new_index_spec.set_name(
                    random.choice(list(search._ASCII_PRINTABLE - set('!'))) +
                    ''.join(
                        random.choice(list(search._ASCII_PRINTABLE))
                        for _ in xrange(
                            random.randint(
                                0, search._MAXIMUM_INDEX_NAME_LENGTH))))
                new_index_spec.set_consistency(
                    random.choice([
                        search_service_pb.IndexSpec.GLOBAL,
                        search_service_pb.IndexSpec.PER_DOCUMENT
                    ]))
            response.mutable_status().set_code(
                random.choice(
                    [search_service_pb.SearchServiceError.OK] * 10 +
                    [search_service_pb.SearchServiceError.TRANSIENT_ERROR] +
                    [search_service_pb.SearchServiceError.INTERNAL_ERROR]))
            return

        response.mutable_status().set_code(
            search_service_pb.SearchServiceError.OK)
        if not len(self.__indexes):
            return
        keys, indexes = zip(
            *sorted(self.__indexes.iteritems(), key=lambda v: v[0]))
        position = 0
        params = request.params()
        if params.has_start_index_name():
            position = bisect.bisect_left(keys, params.start_index_name())
            if (not params.include_start_index() and position < len(keys)
                    and keys[position] == params.start_index_name()):
                position += 1
        elif params.has_index_name_prefix():
            position = bisect.bisect_left(keys, params.index_name_prefix())
        if params.has_offset():
            position += params.offset()
        end_position = position + params.limit()
        prefix = params.index_name_prefix()
        for index in indexes[min(position, len(keys)
                                 ):min(end_position, len(keys))]:
            index_spec = index.IndexSpec
            if prefix and not index_spec.name().startswith(prefix):
                break
            new_index_spec = response.add_index_metadata().mutable_index_spec()
            new_index_spec.set_name(index_spec.name())
            new_index_spec.set_consistency(index_spec.consistency())