Esempio n. 1
0
 def _convert_and_save_entity(self, key, document, object_type,
                              nested_object_types):
     if key not in self._entities_by_key:
         entity, metadata, original_metadata = Utils.convert_to_entity(
             document, object_type, self.conventions, nested_object_types)
         self.save_entity(key, entity, original_metadata, metadata,
                          document)
    def _execute_query(self):
        self.session.increment_requests_count()
        conventions = self.session.conventions
        end_time = time.time() + conventions.timeout
        while True:
            index_query = self.get_index_query()
            if self._page_size is not None:
                index_query.page_size = self._page_size
            response = self.session.database_commands.query(self.index_name, index_query, includes=self.includes)
            if response["IsStale"] and self.wait_for_non_stale_results:
                if time.time() > end_time:
                    raise ErrorResponseException("The index is still stale after reached the timeout")
                    time.sleep(0.1)
                continue
            break

        results = []
        response_results = response.pop("Results")
        response_includes = response.pop("Includes")

        for result in response_results:
            entity, metadata, original_metadata = Utils.convert_to_entity(result, self.object_type, conventions,
                                                                          self.nested_object_types,
                                                                          fetch=False if not self.fetch else True)
            if self.object_type != dict and not self.fetch:
                self.session.save_entity(key=original_metadata.get("@id", None), entity=entity,
                                         original_metadata=original_metadata,
                                         metadata=metadata, document=result)
            results.append(entity)
        self.session.save_includes(response_includes)
        if self._with_statistics:
            return results, response
        return results
    def stream(self, query, object_type=None, nested_object_types=None):
        index_query = query.get_index_query()
        if index_query.wait_for_non_stale_results:
            raise exceptions.NotSupportedException(
                "Since stream() does not wait for indexing (by design), "
                "streaming query with wait_for_non_stale_results is not supported."
            )
        self.session.increment_requests_count()
        response = self.session.database_commands.stream_query(
            query.index_name, index_query)
        basic_parse = IncrementalJsonParser.basic_parse(response)
        parser = ijson.backend.common.parse(basic_parse)

        results = ijson.backend.common.items(parser, "Results")
        for result in next(results, None):
            document, metadata, _ = Utils.convert_to_entity(
                result, object_type, self.session.conventions,
                nested_object_types)
            yield {
                "document": document,
                "metadata": metadata,
                "key": metadata.get("@id", None),
                "etag": metadata.get("@etag", None)
            }