/
GAEDatastoreInterface.go
198 lines (163 loc) · 6.97 KB
/
GAEDatastoreInterface.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
package Datastore
// Copyright 2012-2014 ThePiachu. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import (
"github.com/ThePiachu/Go/Log"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
)
func PutInDatastoreFull(c context.Context, kind, stringID string, intID int64, parent *datastore.Key, variable interface{}) (*datastore.Key, error) {
k := datastore.NewKey(c, kind, stringID, intID, parent)
key, err := datastore.Put(c, k, variable)
return key, err
}
func PutInDatastoreSimple(c context.Context, kind, stringID string, variable interface{}) (*datastore.Key, error) {
return PutInDatastoreFull(c, kind, stringID, 0, nil, variable)
}
func PutInDatastoreSimpleWithParent(c context.Context, kind, stringID string, parent *datastore.Key, variable interface{}) (*datastore.Key, error) {
return PutInDatastoreFull(c, kind, stringID, 0, parent, variable)
}
func PutInDatastore(c context.Context, kind string, variable interface{}) (*datastore.Key, error) {
return PutInDatastoreFull(c, kind, "", 0, nil, variable)
}
func GetFromDatastoreFull(c context.Context, kind, stringID string, intID int64, parent *datastore.Key, dst interface{}) error {
k := datastore.NewKey(c, kind, stringID, intID, parent)
return datastore.Get(c, k, dst)
}
func GetFromDatastoreSimple(c context.Context, kind, stringID string, dst interface{}) error {
return GetFromDatastoreFull(c, kind, stringID, 0, nil, dst)
}
func GetFromDatastoreSimpleWithParent(c context.Context, kind, stringID string, parent *datastore.Key, dst interface{}) error {
return GetFromDatastoreFull(c, kind, stringID, 0, parent, dst)
}
// A function that either loads a variable from datastore, or if it is not present, sets it and then loads it
func GetFromDatastoreOrSetDefaultFull(c context.Context, kind, stringID string, intID int64, parent *datastore.Key, dst interface{}, def interface{}) error {
key := datastore.NewKey(c, kind, stringID, intID, parent)
if err := datastore.Get(c, key, dst); err != nil {
if err == datastore.ErrNoSuchEntity {
_, err2 := datastore.Put(c, key, def)
if err2 != nil {
return err2
} else {
if err3 := datastore.Get(c, key, dst); err3 != nil {
return err3
}
}
} else {
return err
}
}
return nil
}
func GetFromDatastoreOrSetDefaultSimple(c context.Context, kind, stringID string, dst interface{}, def interface{}) error {
return GetFromDatastoreOrSetDefaultFull(c, kind, stringID, 0, nil, dst, def)
}
func IsVariableInDatastoreSimple(c context.Context, kind, stringID string, dst interface{}) bool {
err := GetFromDatastoreSimple(c, kind, stringID, dst)
if err == nil {
return true
}
if err == datastore.ErrNoSuchEntity {
return false
}
Log.Errorf(c, "IsVariableInDatastoreSimple - %s", err)
return false
}
func QueryGetFirstBy(c context.Context, kind string, order string, limit int, dst interface{}) ([]*datastore.Key, error) {
q := datastore.NewQuery(kind).Order(order).Limit(limit)
return q.GetAll(c, dst)
}
func QueryGetFirstKeysBy(c context.Context, kind string, order string, limit int, dst interface{}) ([]*datastore.Key, error) {
q := datastore.NewQuery(kind).Order(order).Limit(limit).KeysOnly()
return q.GetAll(c, dst)
}
func QueryGetAllWithFilter(c context.Context, kind string, filterStr string, filterValue interface{}, dst interface{}) ([]*datastore.Key, error) {
return QueryGetAllWithFilterAndLimit(c, kind, filterStr, filterValue, -1, dst)
}
func QueryGetAllWithFilterAndLimit(c context.Context, kind string, filterStr string, filterValue interface{}, limit int, dst interface{}) ([]*datastore.Key, error) {
q := datastore.NewQuery(kind).Filter(filterStr, filterValue).Limit(limit)
return q.GetAll(c, dst)
}
func QueryGetAllWithLimit(c context.Context, kind string, limit int, dst interface{}) ([]*datastore.Key, error) {
q := datastore.NewQuery(kind).Limit(limit)
return q.GetAll(c, dst)
}
func QueryGetAll(c context.Context, kind string, dst interface{}) ([]*datastore.Key, error) {
q := datastore.NewQuery(kind)
return q.GetAll(c, dst)
}
func QueryGetAllKeysWithFilterAndOrder(c context.Context, kind string, filterStr string, filterValue interface{}, orderStr string, dst interface{}) ([]*datastore.Key, error) {
return QueryGetAllKeysWithFilterLimitAndOrder(c, kind, filterStr, filterValue, -1, orderStr, dst)
}
func QueryGetAllKeysWithFilterLimitAndOrder(c context.Context, kind string, filterStr string, filterValue interface{}, limit int, orderStr string, dst interface{}) ([]*datastore.Key, error) {
q := datastore.NewQuery(kind).Filter(filterStr, filterValue).Limit(limit).Order(orderStr).KeysOnly()
return q.GetAll(c, dst)
}
func QueryGetAllKeysWithFilterLimitOffsetAndOrder(c context.Context, kind string, filterStr string, filterValue interface{}, limit int, offset int, orderStr string, dst interface{}) ([]*datastore.Key, error) {
q := datastore.NewQuery(kind).Filter(filterStr, filterValue).Limit(limit).Offset(offset).Order(orderStr).KeysOnly()
return q.GetAll(c, dst)
}
func QueryGetAllKeysWithFilter(c context.Context, kind string, filterStr string, filterValue interface{}, dst interface{}) []*datastore.Key {
return QueryGetAllKeysWithFilterAndLimit(c, kind, filterStr, filterValue, -1, dst)
}
func QueryGetAllKeysWithFilterAndLimit(c context.Context, kind string, filterStr string, filterValue interface{}, limit int, dst interface{}) []*datastore.Key {
q := datastore.NewQuery(kind).Filter(filterStr, filterValue).Limit(limit).KeysOnly()
keys, err := q.GetAll(c, dst)
if err != nil {
panic(err)
}
return keys
}
func QueryGetAllKeys(c context.Context, kind string, dst interface{}) ([]*datastore.Key, error) {
q := datastore.NewQuery(kind).KeysOnly()
return q.GetAll(c, dst)
}
func CountQueryWithFilter(c context.Context, kind string, filterStr string, filterValue interface{}) int {
q := datastore.NewQuery(kind).Filter(filterStr, filterValue)
count, err := q.Count(c)
if err != nil {
Log.Errorf(c, "CountQueryWithFilter - %s", err)
return -1
}
return count
}
func ClearNamespace(c context.Context, kind string) error {
q := datastore.NewQuery(kind)
q = q.KeysOnly()
keys, err := q.GetAll(c, nil)
if err != nil {
Log.Errorf(c, "Clear Namespace - %v", err)
return err
}
for {
toDelete := keys[:]
if len(keys) > 500 {
toDelete = keys[:500]
keys = keys[500:]
}
err = datastore.DeleteMulti(c, toDelete)
if err != nil {
Log.Errorf(c, "ClearNamespace - %v", err)
return err
}
if len(keys) < 500 {
break
}
}
return nil
}
func DeleteFromDatastoreFull(c context.Context, kind, stringID string, intID int64, parent *datastore.Key) error {
k := datastore.NewKey(c, kind, stringID, intID, parent)
return datastore.Delete(c, k)
}
func DeleteFromDatastoreSimple(c context.Context, kind, stringID string) error {
return DeleteFromDatastoreFull(c, kind, stringID, 0, nil)
}
func KeysToStringIDs(keys []*datastore.Key) []string {
answer := make([]string, len(keys))
for i := 0; i < len(keys); i++ {
answer[i] = keys[i].StringID()
}
return answer
}