forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_analyticsquery.go
218 lines (178 loc) · 4.83 KB
/
cluster_analyticsquery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package gocb
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"time"
)
type analyticsError struct {
Code uint32 `json:"code"`
Message string `json:"msg"`
}
func (e *analyticsError) Error() string {
return fmt.Sprintf("[%d] %s", e.Code, e.Message)
}
type analyticsResponse struct {
RequestId string `json:"requestID"`
ClientContextId string `json:"clientContextID"`
Results []json.RawMessage `json:"results,omitempty"`
Errors []analyticsError `json:"errors,omitempty"`
Status string `json:"status"`
}
type analyticsMultiError []analyticsError
func (e *analyticsMultiError) Error() string {
return (*e)[0].Error()
}
func (e *analyticsMultiError) Code() uint32 {
return (*e)[0].Code
}
// AnalyticsResults allows access to the results of a Analytics query.
type AnalyticsResults interface {
One(valuePtr interface{}) error
Next(valuePtr interface{}) bool
NextBytes() []byte
Close() error
RequestId() string
ClientContextId() string
}
type analyticsResults struct {
closed bool
index int
rows []json.RawMessage
err error
requestId string
clientContextId string
}
func (r *analyticsResults) Next(valuePtr interface{}) bool {
if r.err != nil {
return false
}
row := r.NextBytes()
if row == nil {
return false
}
r.err = json.Unmarshal(row, valuePtr)
if r.err != nil {
return false
}
return true
}
func (r *analyticsResults) NextBytes() []byte {
if r.err != nil {
return nil
}
if r.index+1 >= len(r.rows) {
r.closed = true
return nil
}
r.index++
return r.rows[r.index]
}
func (r *analyticsResults) Close() error {
r.closed = true
return r.err
}
func (r *analyticsResults) One(valuePtr interface{}) error {
if !r.Next(valuePtr) {
err := r.Close()
if err != nil {
return err
}
return ErrNoResults
}
// Ignore any errors occurring after we already have our result
err := r.Close()
if err != nil {
// Return no error as we got the one result already.
return nil
}
return nil
}
func (r *analyticsResults) RequestId() string {
if !r.closed {
panic("Result must be closed before accessing meta-data")
}
return r.requestId
}
func (r *analyticsResults) ClientContextId() string {
if !r.closed {
panic("Result must be closed before accessing meta-data")
}
return r.clientContextId
}
func (c *Cluster) executeAnalyticsQuery(analyticsEp string, opts map[string]interface{}, timeout time.Duration, client *http.Client) (AnalyticsResults, error) {
reqUri := fmt.Sprintf("%s/query/service", analyticsEp)
tmostr, castok := opts["timeout"].(string)
if castok {
var err error
timeout, err = time.ParseDuration(tmostr)
if err != nil {
return nil, err
}
} else {
// Set the timeout string to its default variant
opts["timeout"] = timeout.String()
}
reqJson, err := json.Marshal(opts)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", reqUri, bytes.NewBuffer(reqJson))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := doHttpWithTimeout(client, req, timeout)
if err != nil {
return nil, err
}
analyticsResp := analyticsResponse{}
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(&analyticsResp)
if err != nil {
return nil, err
}
err = resp.Body.Close()
if err != nil {
logDebugf("Failed to close socket (%s)", err)
}
if len(analyticsResp.Errors) > 0 {
return nil, (*analyticsMultiError)(&analyticsResp.Errors)
}
if resp.StatusCode != 200 {
return nil, &viewError{
Message: "HTTP Error",
Reason: fmt.Sprintf("Status code was %d.", resp.StatusCode),
}
}
return &analyticsResults{
requestId: analyticsResp.RequestId,
clientContextId: analyticsResp.ClientContextId,
index: -1,
rows: analyticsResp.Results,
}, nil
}
// EnableAnalytics allows you to specify Analytics hosts to perform queries against.
//
// Experimental: This API is only needed temporarily until full integration of the
// Analytics service into Couchbase Server has been completed.
func (c *Cluster) EnableAnalytics(hosts []string) {
c.analyticsHosts = hosts
}
// Performs a spatial query and returns a list of rows or an error.
func (c *Cluster) doAnalyticsQuery(q *AnalyticsQuery) (AnalyticsResults, error) {
numHosts := len(c.analyticsHosts)
if numHosts == 0 {
return nil, fmt.Errorf("must specify analytics hosts with EnableAnalytics first")
}
analyticsEp := c.analyticsHosts[rand.Intn(numHosts)]
return c.executeAnalyticsQuery(analyticsEp, q.options, c.analyticsTimeout, c.httpCli)
}
// ExecuteAnalyticsQuery performs an analytics query and returns a list of rows or an error.
//
// Experimental: This API is subject to change at any time.
func (c *Cluster) ExecuteAnalyticsQuery(q *AnalyticsQuery) (AnalyticsResults, error) {
return c.doAnalyticsQuery(q)
}