/
viewquery_options.go
216 lines (183 loc) · 5.44 KB
/
viewquery_options.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
package gocb
import (
"bytes"
"context"
"encoding/json"
"net/url"
"strconv"
"time"
)
// ViewScanConsistency specifies the consistency required for a view query.
type ViewScanConsistency uint
const (
// ViewScanConsistencyNotBounded indicates that no special behaviour should be used.
ViewScanConsistencyNotBounded ViewScanConsistency = iota + 1
// ViewScanConsistencyRequestPlus indicates to update the index before querying it.
ViewScanConsistencyRequestPlus
// ViewScanConsistencyUpdateAfter indicates to update the index asynchronously after querying.
ViewScanConsistencyUpdateAfter
)
// ViewOrdering specifies the ordering for the view queries results.
type ViewOrdering uint
const (
// ViewOrderingAscending indicates the query results should be sorted from lowest to highest.
ViewOrderingAscending ViewOrdering = iota + 1
// ViewOrderingDescending indicates the query results should be sorted from highest to lowest.
ViewOrderingDescending
)
// ViewErrorMode pecifies the behaviour of the query engine should an error occur during the gathering of
// view index results which would result in only partial results being available.
type ViewErrorMode uint
const (
// ViewErrorModeContinue indicates to continue gathering results on error.
ViewErrorModeContinue ViewErrorMode = iota + 1
// ViewErrorModeStop indicates to stop gathering results on error
ViewErrorModeStop
)
// ViewOptions represents the options available when executing view query.
type ViewOptions struct {
ScanConsistency ViewScanConsistency
Skip uint32
Limit uint32
Order ViewOrdering
Reduce bool
Group bool
GroupLevel uint32
Key interface{}
Keys []interface{}
StartKey interface{}
EndKey interface{}
InclusiveEnd bool
StartKeyDocID string
EndKeyDocID string
OnError ViewErrorMode
Debug bool
ParentSpan RequestSpan
// Raw provides a way to provide extra parameters in the request body for the query.
Raw map[string]string
Namespace DesignDocumentNamespace
Timeout time.Duration
RetryStrategy RetryStrategy
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
func (opts *ViewOptions) toURLValues() (*url.Values, error) {
options := &url.Values{}
if opts.ScanConsistency != 0 {
if opts.ScanConsistency == ViewScanConsistencyRequestPlus {
options.Set("stale", "false")
} else if opts.ScanConsistency == ViewScanConsistencyNotBounded {
options.Set("stale", "ok")
} else if opts.ScanConsistency == ViewScanConsistencyUpdateAfter {
options.Set("stale", "update_after")
} else {
return nil, makeInvalidArgumentsError("unexpected stale option")
}
}
if opts.Skip != 0 {
options.Set("skip", strconv.FormatUint(uint64(opts.Skip), 10))
}
if opts.Limit != 0 {
options.Set("limit", strconv.FormatUint(uint64(opts.Limit), 10))
}
if opts.Order != 0 {
if opts.Order == ViewOrderingAscending {
options.Set("descending", "false")
} else if opts.Order == ViewOrderingDescending {
options.Set("descending", "true")
} else {
return nil, makeInvalidArgumentsError("unexpected order option")
}
}
options.Set("reduce", "false") // is this line necessary?
if opts.Reduce {
options.Set("reduce", "true")
// Only set group if a reduce view
options.Set("group", "false") // is this line necessary?
if opts.Group {
options.Set("group", "true")
}
if opts.GroupLevel != 0 {
options.Set("group_level", strconv.FormatUint(uint64(opts.GroupLevel), 10))
}
}
if opts.Key != nil {
jsonKey, err := opts.marshalJSON(opts.Key)
if err != nil {
return nil, err
}
options.Set("key", string(jsonKey))
}
if len(opts.Keys) > 0 {
jsonKeys, err := opts.marshalJSON(opts.Keys)
if err != nil {
return nil, err
}
options.Set("keys", string(jsonKeys))
}
if opts.StartKey != nil {
jsonStartKey, err := opts.marshalJSON(opts.StartKey)
if err != nil {
return nil, err
}
options.Set("startkey", string(jsonStartKey))
} else {
options.Del("startkey")
}
if opts.EndKey != nil {
jsonEndKey, err := opts.marshalJSON(opts.EndKey)
if err != nil {
return nil, err
}
options.Set("endkey", string(jsonEndKey))
} else {
options.Del("endkey")
}
if opts.StartKey != nil || opts.EndKey != nil {
if opts.InclusiveEnd {
options.Set("inclusive_end", "true")
} else {
options.Set("inclusive_end", "false")
}
}
if opts.StartKeyDocID == "" {
options.Del("startkey_docid")
} else {
options.Set("startkey_docid", opts.StartKeyDocID)
}
if opts.EndKeyDocID == "" {
options.Del("endkey_docid")
} else {
options.Set("endkey_docid", opts.EndKeyDocID)
}
if opts.OnError > 0 {
if opts.OnError == ViewErrorModeContinue {
options.Set("on_error", "continue")
} else if opts.OnError == ViewErrorModeStop {
options.Set("on_error", "stop")
} else {
return nil, makeInvalidArgumentsError("unexpected onerror option")
}
}
if opts.Debug {
options.Set("debug", "true")
}
if opts.Raw != nil {
for k, v := range opts.Raw {
options.Set(k, v)
}
}
return options, nil
}
func (opts *ViewOptions) marshalJSON(value interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(value)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}