-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
177 lines (164 loc) · 4.47 KB
/
query.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
package riaken_struct
import (
"errors"
"reflect"
)
import (
core "github.com/riaken/riaken-core"
"github.com/riaken/riaken-core/rpb"
)
type Query struct {
session *Session
coreQuery *core.Query
out interface{}
key string
}
func (q *Query) reset() {
q.out = nil
q.key = ""
}
// CoreQuery returns the underlying riaken-core Query.
func (q *Query) CoreQuery() *core.Query {
return q.coreQuery
}
func (q *Query) Do(opts interface{}) *Query {
q.coreQuery.Do(opts)
return q
}
func (q *Query) Out(out interface{}) *Query {
q.out = out
return q
}
// Key sets the special case struct member to write the key value out to in searches.
func (q *Query) Key(key string) *Query {
q.key = key
return q
}
func (q *Query) MapReduce(req, ct []byte) (*rpb.RpbMapRedResp, error) {
return q.coreQuery.MapReduce(req, ct)
}
// SecondaryIndexes is based loosely on the logic in http://godoc.org/labix.org/v2/mgo#Query.All
//
// Chain call this method with Key() to describe the struct member to write the key value out to,
// and Out() to pass the []struct to output the queried values to.
//
// type Results struct {
// Id string
// Value string
// }
//
// var foo []results
// query.Key("Id").Out(&results).SecondaryIndexes(...)
func (q *Query) SecondaryIndexes(bucket, index, key, start, end []byte, maxResults uint32, continuation []byte) (*rpb.RpbIndexResp, error) {
defer q.reset()
if q.out == nil {
return nil, errors.New("out must be set via Out()")
}
rv := reflect.ValueOf(q.out)
if rv.Kind() != reflect.Ptr || rv.Elem().Kind() != reflect.Slice {
return nil, errors.New("out must be of type slice")
}
data, err := q.coreQuery.SecondaryIndexes(bucket, index, key, start, end, maxResults, continuation)
if err != nil {
return nil, err
}
if len(data.GetKeys()) == 0 {
return data, nil
}
i := 0
sv := rv.Elem()
sv = sv.Slice(0, sv.Cap())
et := sv.Type().Elem()
b := q.session.GetBucket(string(bucket))
for _, k := range data.GetKeys() {
e := reflect.New(et)
object := b.Object(string(k))
if _, err := object.Fetch(e.Interface()); err != nil {
return nil, err
}
if q.key != "" {
ek := reflect.ValueOf(e.Interface()).Elem()
fk := ek.FieldByName(q.key)
if fk.Kind() == reflect.String {
fk.SetString(string(k))
}
if fk.Kind() == reflect.Slice {
fk.SetBytes(k)
}
}
sv = reflect.Append(sv, e.Elem())
sv = sv.Slice(0, sv.Cap())
i++
}
rv.Elem().Set(sv.Slice(0, i))
return data, nil
}
// Search is based loosely on the logic in http://godoc.org/labix.org/v2/mgo#Query.All
//
// Chain call this method with Key() to describe the struct member to write the key value out to,
// and Out() to pass the []struct to output the queried values to.
//
// WARNING: Searches that result in a lot of results can potentially run the application out of memory.
// If this occurs fall back to fetching the keys with the riaken-core search and manually fetching the data.
func (q *Query) Search(index, query []byte) (*rpb.RpbSearchQueryResp, error) {
defer q.reset()
if q.out == nil {
return nil, errors.New("out must be set via Out()")
}
rv := reflect.ValueOf(q.out)
if rv.Kind() != reflect.Ptr || rv.Elem().Kind() != reflect.Slice {
return nil, errors.New("out must be of type slice")
}
data, err := q.coreQuery.Search(index, query)
if err != nil {
return nil, err
}
if data.GetNumFound() == 0 {
return data, nil
}
// Since a key can be returned multiple times, check if it's already been stored.
found := map[string]bool{}
check := func(key string) bool {
_, ok := found[key]
if !ok {
found[key] = true
}
return ok
}
i := 0
sv := rv.Elem()
sv = sv.Slice(0, sv.Cap())
et := sv.Type().Elem()
bucket := q.session.GetBucket(string(index))
for _, d := range data.GetDocs() {
for _, v := range d.GetFields() {
// Riak seems to default the key to "id"
if string(v.GetKey()) == "id" {
// Check if key has been stored.
if check(string(v.GetValue())) {
continue
}
e := reflect.New(et)
object := bucket.Object(string(v.GetValue()))
if _, err := object.Fetch(e.Interface()); err != nil {
return nil, err
}
if q.key != "" {
ek := reflect.ValueOf(e.Interface()).Elem()
fk := ek.FieldByName(q.key)
if fk.Kind() == reflect.String {
fk.SetString(string(v.GetValue()))
}
if fk.Kind() == reflect.Slice {
fk.SetBytes(v.GetValue())
}
}
sv = reflect.Append(sv, e.Elem())
sv = sv.Slice(0, sv.Cap())
i++
}
}
}
rv.Elem().Set(sv.Slice(0, i))
return data, nil
}