1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2014-2018 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #include <libcouchbase/couchbase.h>
19 #include <libcouchbase/views.h>
20 #include <libcouchbase/pktfwd.h>
21 #include <jsparse/parser.h>
22 #include <string>
23 #include "docreq/docreq.h"
24 
25 namespace lcb {
26 namespace views {
27 
28 struct ViewRequest;
29 struct VRDocRequest : docreq::DocRequest {
30     ViewRequest *parent;
31     lcb_IOV key;
32     lcb_IOV value;
33     lcb_IOV geo;
34     std::string rowbuf;
35 };
36 
37 struct ViewRequest : lcb::jsparse::Parser::Actions {
38     ViewRequest(lcb_t, const void*, const lcb_CMDVIEWQUERY*);
39     ~ViewRequest();
40     void invoke_last(lcb_error_t err);
invoke_lastViewRequest41     void invoke_last() { invoke_last(lasterr); }
42     void invoke_row(lcb_RESPVIEWQUERY*);
unrefViewRequest43     void unref() {if(!--refcount){delete this;}}
refViewRequest44     void ref() {refcount++;}
45     void cancel();
46 
47     /**
48      * Perform the actual HTTP request
49      * @param cmd User's command
50      */
51     inline lcb_error_t request_http(const lcb_CMDVIEWQUERY* cmd);
52 
is_include_docsViewRequest53     bool is_include_docs() const {
54         return cmdflags & LCB_CMDVIEWQUERY_F_INCLUDE_DOCS;
55     }
is_no_rowparseViewRequest56     bool is_no_rowparse() const {
57         return cmdflags & LCB_CMDVIEWQUERY_F_NOROWPARSE;
58     }
is_spatialViewRequest59     bool is_spatial() const {
60         return cmdflags & LCB_CMDVIEWQUERY_F_SPATIAL;
61     }
62 
63     void JSPARSE_on_row(const lcb::jsparse::Row&);
64     void JSPARSE_on_error(const std::string&);
65     void JSPARSE_on_complete(const std::string&);
66 
67     /** Current HTTP response to provide in callbacks */
68     const lcb_RESPHTTP *cur_htresp;
69     /** HTTP request object, in case we need to cancel prematurely */
70     struct lcb_http_request_st *htreq;
71     lcb::jsparse::Parser *parser;
72     const void *cookie;
73     docreq::Queue *docq;
74     lcb_VIEWQUERYCALLBACK callback;
75     lcb_t instance;
76 
77     unsigned refcount;
78     uint32_t cmdflags;
79     lcb_error_t lasterr;
80 #ifdef LCB_TRACING
81     lcbtrace_SPAN *span;
82 #endif
83 
84 };
85 
86 }
87 }
88