Ejemplo n.º 1
0
 def next(self, doc: dict) -> Tuple[str, str, ViewQuery]:
     ddoc_name, view_name = next(self.view_sequence)
     params = self.generate_params(**doc)[view_name]
     params = dict(self.params, **params)
     return ddoc_name, view_name, ViewQuery(**params)
Ejemplo n.º 2
0
    def __init__(self,
                 parent,
                 design,
                 view,
                 row_processor=None,
                 include_docs=False,
                 query=None,
                 streaming=True,
                 **params):
        """
        Construct a iterable which can be used to iterate over view query
        results.

        :param parent: The parent Bucket object
        :type parent: :class:`~couchbase.bucket.Bucket`
        :param string design: The design document
        :param string view: The name of the view within the design document
        :param callable row_processor: See :attr:`row_processor` for more
            details.

        :param boolean include_docs: If set, the document itself will be
            retrieved for each row in the result. The default algorithm
            uses :meth:`~couchbase.bucket.Bucket.get_multi` for each
            page (i.e. every :attr:`streaming` results).

            The :attr:`~couchbase.views.params.Query.reduce`
            family of attributes must not be active, as results fro
            ``reduce`` views do not have corresponding
            doc IDs (as these are aggregation functions).

        :param query: If set, should be a :class:`~.Query` or
            :class:`~.SpatialQuery` object. It is illegal to use
            this in conjunction with additional ``params``

        :param params: Extra view options. This may be used to pass view
            arguments (as defined in :class:`~couchbase.views.params.Query`)
            without explicitly constructing a
            :class:`~couchbase.views.params.Query` object.
            It is illegal to use this together with the ``query`` argument.
            If you wish to 'inline' additional arguments to the provided
            ``query`` object, use the
            query's :meth:`~couchbase.views.params.Query.update` method
            instead.

        This object is an iterator - it does not send out the request until
        the first item from the iterator is request. See :meth:`__iter__` for
        more details on what this object returns.


        Simple view query, with no extra options::

            # c is the Bucket object.

            for result in View(c, "beer", "brewery_beers"):
                print("emitted key: {0}, doc_id: {1}"
                        .format(result.key, result.docid))


        Execute a view with extra query options::

            # Implicitly creates a Query object

            view = View(c, "beer", "by_location",
                        limit=4,
                        reduce=True,
                        group_level=2)

        Execute a spatial view::

            from couchbase.views.params import SpatialQuery
            # ....
            q = SpatialQuery()
            q.start_range = [ -119.9556, 38.7056 ]
            q.end_range = [ -118.8122, 39.7086 ]
            view = View(c, 'geodesign', 'spatialview', query=q)
            for row in view:
                print('Location is {0}'.format(row.geometry))

        Pass a Query object::

            q = Query(
                stale=False,
                inclusive_end=True,
                mapkey_range=[
                    ["21st_ammendment_brewery_cafe"],
                    ["21st_ammendment_brewery_cafe", Query.STRING_RANGE_END]
                ]
            )

            view = View(c, "beer", "brewery_beer", query=q)

        Add extra parameters to query object for single call::

            view = View(c, "beer", "brewery_beer",
                        query=q.update(debug=True, copy=True))


        Include documents with query::

            view = View(c, "beer", "brewery_beer",
                        query=q, include_docs=True)

            for result in view:
                print("Emitted key: {0}, Document: {1}".format(
                    result.key, result.doc.value))
        """

        self._parent = parent
        self.design = design
        self.view = view
        self._errors = []
        self.rows_returned = 0
        self._indexed_rows = 0

        # Sentinel used to ensure confusing metadata properties don't get
        # returned unless metadata is actually parsed (or query is complete)
        self.__meta_received = False

        if query and params:
            raise ArgumentError.pyexc(
                "Extra parameters are mutually exclusive with the "
                "'query' argument. Use query.update() to add extra arguments")

        if query:
            if isinstance(query, basestring):
                self._query = ViewQuery.from_string(query)
            else:
                self._query = deepcopy(query)
        else:
            self._query = QueryBase.from_any(params)

        self._flags = 0
        if include_docs:
            self._flags |= C.LCB_CMDVIEWQUERY_F_INCLUDE_DOCS
        if isinstance(self._query, SpatialQuery):
            self._flags |= C.LCB_CMDVIEWQUERY_F_SPATIAL

        if include_docs and self._query.reduce:
            raise ArgumentError.pyexc(
                "include_docs is only applicable for map-only views, but "
                "'reduce', 'group', or 'group_level' was specified; or "
                "this is a spatial query", self._query)

        # The original 'limit' parameter, passed to the query.
        self._do_iter = True
        self._mres = None

        if not row_processor:
            if self._spatial:
                row_processor = SpatialRowProcessor()
            else:
                row_processor = RowProcessor()
        self.row_processor = row_processor
Ejemplo n.º 3
0
    def __init__(self, parent, design, view, row_processor=None,
                 include_docs=False, query=None, streaming=True, **params):
        """
        Construct a iterable which can be used to iterate over view query
        results.

        :param parent: The parent Bucket object
        :type parent: :class:`~couchbase.bucket.Bucket`
        :param string design: The design document
        :param string view: The name of the view within the design document
        :param callable row_processor: See :attr:`row_processor` for more
            details.

        :param boolean include_docs: If set, the document itself will be
            retrieved for each row in the result. The default algorithm
            uses :meth:`~couchbase.bucket.Bucket.get_multi` for each
            page (i.e. every :attr:`streaming` results).

            The :attr:`~couchbase.views.params.Query.reduce`
            family of attributes must not be active, as results fro
            ``reduce`` views do not have corresponding
            doc IDs (as these are aggregation functions).

        :param query: If set, should be a :class:`~couchbase.views.params.Query` or
            :class:`~.SpatialQuery` object. It is illegal to use
            this in conjunction with additional ``params``

        :param params: Extra view options. This may be used to pass view
            arguments (as defined in :class:`~couchbase.views.params.Query`)
            without explicitly constructing a
            :class:`~couchbase.views.params.Query` object.
            It is illegal to use this together with the ``query`` argument.
            If you wish to 'inline' additional arguments to the provided
            ``query`` object, use the
            query's :meth:`~couchbase.views.params.Query.update` method
            instead.

        This object is an iterator - it does not send out the request until
        the first item from the iterator is request. See :meth:`__iter__` for
        more details on what this object returns.


        Simple view query, with no extra options::

            # c is the Bucket object.

            for result in View(c, "beer", "brewery_beers"):
                print("emitted key: {0}, doc_id: {1}"
                        .format(result.key, result.docid))


        Execute a view with extra query options::

            # Implicitly creates a Query object

            view = View(c, "beer", "by_location",
                        limit=4,
                        reduce=True,
                        group_level=2)

        Execute a spatial view::

            from couchbase.views.params import SpatialQuery
            # ....
            q = SpatialQuery()
            q.start_range = [ -119.9556, 38.7056 ]
            q.end_range = [ -118.8122, 39.7086 ]
            view = View(c, 'geodesign', 'spatialview', query=q)
            for row in view:
                print('Location is {0}'.format(row.geometry))

        Pass a Query object::

            q = Query(
                stale=False,
                inclusive_end=True,
                mapkey_range=[
                    ["21st_ammendment_brewery_cafe"],
                    ["21st_ammendment_brewery_cafe", Query.STRING_RANGE_END]
                ]
            )

            view = View(c, "beer", "brewery_beer", query=q)

        Add extra parameters to query object for single call::

            view = View(c, "beer", "brewery_beer",
                        query=q.update(debug=True, copy=True))


        Include documents with query::

            view = View(c, "beer", "brewery_beer",
                        query=q, include_docs=True)

            for result in view:
                print("Emitted key: {0}, Document: {1}".format(
                    result.key, result.doc.value))
        """

        if parent.btype == C.LCB_BTYPE_EPHEMERAL:
            raise NotSupportedError("Ephemeral bucket")
        self._parent = parent
        self.design = design
        self.view = view
        self._errors = []
        self.rows_returned = 0
        self._indexed_rows = 0

        # Sentinel used to ensure confusing metadata properties don't get
        # returned unless metadata is actually parsed (or query is complete)
        self.__meta_received = False

        if query and params:
            raise ArgumentError.pyexc(
                "Extra parameters are mutually exclusive with the "
                "'query' argument. Use query.update() to add extra arguments")

        if query:
            if isinstance(query, basestring):
                self._query = ViewQuery.from_string(query)
            else:
                self._query = deepcopy(query)
        else:
            self._query = QueryBase.from_any(params)

        self._flags = 0
        if include_docs:
            self._flags |= C.LCB_CMDVIEWQUERY_F_INCLUDE_DOCS
        if isinstance(self._query, SpatialQuery):
            self._flags |= C.LCB_CMDVIEWQUERY_F_SPATIAL

        if include_docs and self._query.reduce:
            raise ArgumentError.pyexc(
                "include_docs is only applicable for map-only views, but "
                "'reduce', 'group', or 'group_level' was specified; or "
                "this is a spatial query",
                self._query)

        # The original 'limit' parameter, passed to the query.
        self._do_iter = True
        self._mres = None

        if not row_processor:
            if self._spatial:
                row_processor = SpatialRowProcessor()
            else:
                row_processor = RowProcessor()
        self.row_processor = row_processor