forked from maddyblue/goon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
176 lines (150 loc) · 4.72 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
/*
* Copyright (c) 2012 Matt Jibson <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package goon
import (
"fmt"
"reflect"
"google.golang.org/appengine/datastore"
)
// Count returns the number of results for the query.
func (g *Goon) Count(q *datastore.Query) (int, error) {
return q.Count(g.Context)
}
// GetAll runs the query and returns all the keys that match the query, as well
// as appending the values to dst, setting the goon key fields of dst, and
// caching the returned data in local memory.
//
// For "keys-only" queries dst can be nil, however if it is not, then GetAll
// appends zero value structs to dst, only setting the goon key fields.
// No data is cached with "keys-only" queries.
//
// See: https://developers.google.com/appengine/docs/go/datastore/reference#Query.GetAll
func (g *Goon) GetAll(q *datastore.Query, dst interface{}) ([]*datastore.Key, error) {
v := reflect.ValueOf(dst)
vLenBefore := 0
if dst != nil {
if v.Kind() != reflect.Ptr {
return nil, fmt.Errorf("goon: Expected dst to be a pointer to a slice or nil, got instead: %v", v.Kind())
}
v = v.Elem()
if v.Kind() != reflect.Slice {
return nil, fmt.Errorf("goon: Expected dst to be a pointer to a slice or nil, got instead: %v", v.Kind())
}
vLenBefore = v.Len()
}
keys, err := q.GetAll(g.Context, dst)
if err != nil {
if errFieldMismatch(err) {
if IgnoreFieldMismatch {
err = nil
}
} else {
g.error(err)
return keys, err
}
}
if dst == nil || len(keys) == 0 {
return keys, err
}
keysOnly := ((v.Len() - vLenBefore) != len(keys))
updateCache := !g.inTransaction && !keysOnly
// If this is a keys-only query, we need to fill the slice with zero value elements
if keysOnly {
elemType := v.Type().Elem()
ptr := false
if elemType.Kind() == reflect.Ptr {
elemType = elemType.Elem()
ptr = true
}
if elemType.Kind() != reflect.Struct {
return keys, fmt.Errorf("goon: Expected struct, got instead: %v", elemType.Kind())
}
for i := 0; i < len(keys); i++ {
ev := reflect.New(elemType)
if !ptr {
ev = ev.Elem()
}
v.Set(reflect.Append(v, ev))
}
}
if updateCache {
g.cacheLock.Lock()
defer g.cacheLock.Unlock()
}
for i, k := range keys {
var e interface{}
vi := v.Index(vLenBefore + i)
if vi.Kind() == reflect.Ptr {
e = vi.Interface()
} else {
e = vi.Addr().Interface()
}
if err := g.setStructKey(e, k); err != nil {
return nil, err
}
if updateCache {
// Cache lock is handled before the for loop
g.cache[MemcacheKey(k)] = e
}
}
return keys, err
}
// Run runs the query.
func (g *Goon) Run(q *datastore.Query) *Iterator {
return &Iterator{
g: g,
i: q.Run(g.Context),
}
}
// Iterator is the result of running a query.
type Iterator struct {
g *Goon
i *datastore.Iterator
}
// Cursor returns a cursor for the iterator's current location.
func (t *Iterator) Cursor() (datastore.Cursor, error) {
return t.i.Cursor()
}
// Next returns the entity of the next result. When there are no more results,
// datastore.Done is returned as the error. If dst is null (for a keys-only
// query), nil is returned as the entity.
//
// If the query is not keys only and dst is non-nil, it also loads the entity
// stored for that key into the struct pointer dst, with the same semantics
// and possible errors as for the Get function. This result is cached in memory.
//
// If the query is keys only, dst must be passed as nil. Otherwise the cache
// will be populated with empty entities since there is no way to detect the
// case of a keys-only query.
//
// Refer to appengine/datastore.Iterator.Next:
// https://developers.google.com/appengine/docs/go/datastore/reference#Iterator.Next
func (t *Iterator) Next(dst interface{}) (*datastore.Key, error) {
k, err := t.i.Next(dst)
if err != nil && (!IgnoreFieldMismatch || !errFieldMismatch(err)) {
return k, err
}
if dst != nil {
// Update the struct to have correct key info
t.g.setStructKey(dst, k)
if !t.g.inTransaction {
t.g.cacheLock.Lock()
t.g.cache[MemcacheKey(k)] = dst
t.g.cacheLock.Unlock()
}
}
return k, nil
}