forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewquery.go
199 lines (177 loc) · 5.07 KB
/
viewquery.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
package gocb
import (
"encoding/json"
"net/url"
"strconv"
"strings"
)
// StaleMode specifies the consistency required for a view query.
type StaleMode int
const (
// Before indicates to update the index before querying it.
Before = StaleMode(1)
// None indicates that no special behaviour should be used.
None = StaleMode(2)
// After indicates to update the index asynchronously after querying.
After = StaleMode(3)
)
// SortOrder specifies the ordering for the view queries results.
type SortOrder int
const (
// Ascending indicates the query results should be sorted from lowest to highest.
Ascending = SortOrder(1)
// Descending indicates the query results should be sorted from highest to lowest.
Descending = SortOrder(2)
)
// ViewQuery represents a pending view query.
type ViewQuery struct {
ddoc string
name string
options url.Values
errs MultiError
}
func (vq *ViewQuery) marshalJson(value interface{}) []byte {
bytes, err := json.Marshal(value)
if err != nil {
vq.errs.add(err)
return nil
}
return bytes
}
// Stale specifies the level of consistency required for this query.
func (vq *ViewQuery) Stale(stale StaleMode) *ViewQuery {
if stale == Before {
vq.options.Set("stale", "false")
} else if stale == None {
vq.options.Set("stale", "ok")
} else if stale == After {
vq.options.Set("stale", "update_after")
} else {
panic("Unexpected stale option")
}
return vq
}
// Skip specifies how many results to skip at the beginning of the result list.
func (vq *ViewQuery) Skip(num uint) *ViewQuery {
vq.options.Set("skip", strconv.FormatUint(uint64(num), 10))
return vq
}
// Limit specifies a limit on the number of results to return.
func (vq *ViewQuery) Limit(num uint) *ViewQuery {
vq.options.Set("limit", strconv.FormatUint(uint64(num), 10))
return vq
}
// Order specifies the order to sort the view results in.
func (vq *ViewQuery) Order(order SortOrder) *ViewQuery {
if order == Ascending {
vq.options.Set("descending", "false")
} else if order == Descending {
vq.options.Set("descending", "true")
} else {
panic("Unexpected order option")
}
return vq
}
// Reduce specifies whether to run the reduce part of the map-reduce.
func (vq *ViewQuery) Reduce(reduce bool) *ViewQuery {
if reduce == true {
vq.options.Set("reduce", "true")
} else {
vq.options.Set("reduce", "false")
}
return vq
}
// Group specifies whether to group the map-reduce results.
func (vq *ViewQuery) Group(useGrouping bool) *ViewQuery {
if useGrouping {
vq.options.Set("group", "true")
} else {
vq.options.Set("group", "false")
}
return vq
}
// GroupLevel specifies at what level to group the map-reduce results.
func (vq *ViewQuery) GroupLevel(groupLevel uint) *ViewQuery {
vq.options.Set("group_level", strconv.FormatUint(uint64(groupLevel), 10))
return vq
}
// Key specifies a specific key to retrieve from the index.
func (vq *ViewQuery) Key(key interface{}) *ViewQuery {
jsonKey := vq.marshalJson(key)
vq.options.Set("key", string(jsonKey))
return vq
}
// Keys specifies a list of specific keys to retrieve from the index.
func (vq *ViewQuery) Keys(keys []interface{}) *ViewQuery {
jsonKeys := vq.marshalJson(keys)
vq.options.Set("keys", string(jsonKeys))
return vq
}
// Range specifies a value range to get results within.
func (vq *ViewQuery) Range(start, end interface{}, inclusiveEnd bool) *ViewQuery {
// TODO(brett19): Not currently handling errors due to no way to return the error
if start != nil {
jsonStartKey := vq.marshalJson(start)
vq.options.Set("startkey", string(jsonStartKey))
} else {
vq.options.Del("startkey")
}
if end != nil {
jsonEndKey := vq.marshalJson(end)
vq.options.Set("endkey", string(jsonEndKey))
} else {
vq.options.Del("endkey")
}
if start != nil || end != nil {
if inclusiveEnd {
vq.options.Set("inclusive_end", "true")
} else {
vq.options.Set("inclusive_end", "false")
}
} else {
vq.options.Del("inclusive_end")
}
return vq
}
// IdRange specifies a range of document id's to get results within.
// Usually requires Range to be specified as well.
func (vq *ViewQuery) IdRange(start, end string) *ViewQuery {
if start != "" {
vq.options.Set("startkey_docid", start)
} else {
vq.options.Del("startkey_docid")
}
if end != "" {
vq.options.Set("endkey_docid", end)
} else {
vq.options.Del("endkey_docid")
}
return vq
}
// Development specifies whether to query the production or development design document.
func (vq *ViewQuery) Development(val bool) *ViewQuery {
if val {
if !strings.HasPrefix(vq.ddoc, "dev_") {
vq.ddoc = "dev_" + vq.ddoc
}
} else {
vq.ddoc = strings.TrimPrefix(vq.ddoc, "dev_")
}
return vq
}
// Custom allows specifying custom query options.
func (vq *ViewQuery) Custom(name, value string) *ViewQuery {
vq.options.Set(name, value)
return vq
}
func (vq *ViewQuery) getInfo() (string, string, url.Values, error) {
return vq.ddoc, vq.name, vq.options, vq.errs.get()
}
// NewViewQuery creates a new ViewQuery object from a design document and view name.
func NewViewQuery(ddoc, name string) *ViewQuery {
return &ViewQuery{
ddoc: ddoc,
name: name,
options: url.Values{},
}
}