-
Notifications
You must be signed in to change notification settings - Fork 104
/
cluster_analyticsquery.go
280 lines (229 loc) · 6.77 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package gocb
import (
"encoding/json"
"errors"
"time"
)
// AnalyticsMetrics encapsulates various metrics gathered during a queries execution.
type AnalyticsMetrics struct {
ElapsedTime time.Duration
ExecutionTime time.Duration
ResultCount uint64
ResultSize uint64
MutationCount uint64
SortCount uint64
ErrorCount uint64
WarningCount uint64
ProcessedObjects uint64
}
func (metrics *AnalyticsMetrics) fromData(data jsonAnalyticsMetrics) error {
elapsedTime, err := time.ParseDuration(data.ElapsedTime)
if err != nil {
logDebugf("Failed to parse query metrics elapsed time: %s", err)
}
executionTime, err := time.ParseDuration(data.ExecutionTime)
if err != nil {
logDebugf("Failed to parse query metrics execution time: %s", err)
}
metrics.ElapsedTime = elapsedTime
metrics.ExecutionTime = executionTime
metrics.ResultCount = data.ResultCount
metrics.ResultSize = data.ResultSize
metrics.MutationCount = data.MutationCount
metrics.SortCount = data.SortCount
metrics.ErrorCount = data.ErrorCount
metrics.WarningCount = data.WarningCount
metrics.ProcessedObjects = data.ProcessedObjects
return nil
}
// AnalyticsWarning encapsulates any warnings returned by a query.
type AnalyticsWarning struct {
Code uint32
Message string
}
func (warning *AnalyticsWarning) fromData(data jsonAnalyticsWarning) error {
warning.Code = data.Code
warning.Message = data.Message
return nil
}
// AnalyticsMetaData provides access to the meta-data properties of a query result.
type AnalyticsMetaData struct {
RequestID string
ClientContextID string
Metrics AnalyticsMetrics
Signature interface{}
Warnings []AnalyticsWarning
}
func (meta *AnalyticsMetaData) fromData(data jsonAnalyticsResponse) error {
metrics := AnalyticsMetrics{}
if err := metrics.fromData(data.Metrics); err != nil {
return err
}
warnings := make([]AnalyticsWarning, len(data.Warnings))
for wIdx, jsonWarning := range data.Warnings {
err := warnings[wIdx].fromData(jsonWarning)
if err != nil {
return err
}
}
meta.RequestID = data.RequestID
meta.ClientContextID = data.ClientContextID
meta.Metrics = metrics
meta.Signature = data.Signature
meta.Warnings = warnings
return nil
}
// AnalyticsResultRaw provides raw access to analytics query data.
// VOLATILE: This API is subject to change at any time.
type AnalyticsResultRaw struct {
reader analyticsRowReader
}
// NextBytes returns the next row as bytes.
func (arr *AnalyticsResultRaw) NextBytes() []byte {
return arr.reader.NextRow()
}
// Err returns any errors that have occurred on the stream
func (arr *AnalyticsResultRaw) Err() error {
err := arr.reader.Err()
if err != nil {
return maybeEnhanceAnalyticsError(err)
}
return nil
}
// Close marks the results as closed, returning any errors that occurred during reading the results.
func (arr *AnalyticsResultRaw) Close() error {
err := arr.reader.Close()
if err != nil {
return maybeEnhanceAnalyticsError(err)
}
return nil
}
// MetaData returns any meta-data that was available from this query as bytes.
func (arr *AnalyticsResultRaw) MetaData() ([]byte, error) {
return arr.reader.MetaData()
}
// AnalyticsResult allows access to the results of a query.
type AnalyticsResult struct {
reader analyticsRowReader
rowBytes []byte
}
func newAnalyticsResult(reader analyticsRowReader) *AnalyticsResult {
return &AnalyticsResult{
reader: reader,
}
}
// Raw returns a AnalyticsResult which can be used to access the raw byte data from search queries.
// Calling this function invalidates the underlying AnalyticsResult which will no longer be able to be used.
// VOLATILE: This API is subject to change at any time.
func (r *AnalyticsResult) Raw() *AnalyticsResultRaw {
vr := &AnalyticsResultRaw{
reader: r.reader,
}
r.reader = nil
return vr
}
// Next assigns the next result from the results into the value pointer, returning whether the read was successful.
func (r *AnalyticsResult) Next() bool {
if r.reader == nil {
return false
}
rowBytes := r.reader.NextRow()
if rowBytes == nil {
return false
}
r.rowBytes = rowBytes
return true
}
// Row returns the value of the current row
func (r *AnalyticsResult) Row(valuePtr interface{}) error {
if r.reader == nil {
return r.Err()
}
if r.rowBytes == nil {
return ErrNoResult
}
if bytesPtr, ok := valuePtr.(*json.RawMessage); ok {
*bytesPtr = r.rowBytes
return nil
}
return json.Unmarshal(r.rowBytes, valuePtr)
}
// Err returns any errors that have occurred on the stream
func (r *AnalyticsResult) Err() error {
if r.reader == nil {
return errors.New("result object is no longer valid")
}
err := r.reader.Err()
if err != nil {
return maybeEnhanceAnalyticsError(err)
}
return nil
}
// Close marks the results as closed, returning any errors that occurred during reading the results.
func (r *AnalyticsResult) Close() error {
if r.reader == nil {
return r.Err()
}
err := r.reader.Close()
if err != nil {
return maybeEnhanceAnalyticsError(err)
}
return nil
}
// One assigns the first value from the results into the value pointer.
// It will close the results but not before iterating through all remaining
// results, as such this should only be used for very small resultsets - ideally
// of, at most, length 1.
func (r *AnalyticsResult) One(valuePtr interface{}) error {
if r.reader == nil {
return r.Err()
}
// Read the bytes from the first row
valueBytes := r.reader.NextRow()
if valueBytes == nil {
return ErrNoResult
}
// Skip through the remaining rows
for r.reader.NextRow() != nil {
// do nothing with the row
}
return json.Unmarshal(valueBytes, valuePtr)
}
// MetaData returns any meta-data that was available from this query. Note that
// the meta-data will only be available once the object has been closed (either
// implicitly or explicitly).
func (r *AnalyticsResult) MetaData() (*AnalyticsMetaData, error) {
if r.reader == nil {
return nil, r.Err()
}
metaDataBytes, err := r.reader.MetaData()
if err != nil {
return nil, err
}
var jsonResp jsonAnalyticsResponse
err = json.Unmarshal(metaDataBytes, &jsonResp)
if err != nil {
return nil, err
}
var metaData AnalyticsMetaData
err = metaData.fromData(jsonResp)
if err != nil {
return nil, err
}
return &metaData, nil
}
// AnalyticsQuery executes the analytics query statement on the server.
func (c *Cluster) AnalyticsQuery(statement string, opts *AnalyticsOptions) (*AnalyticsResult, error) {
return autoOpControl(c.analyticsController(), "analytics", func(provider analyticsProvider) (*AnalyticsResult, error) {
if opts == nil {
opts = &AnalyticsOptions{}
}
return provider.AnalyticsQuery(statement, nil, opts)
})
}
func maybeGetAnalyticsOption(options map[string]interface{}, name string) string {
if value, ok := options[name].(string); ok {
return value
}
return ""
}