-
Notifications
You must be signed in to change notification settings - Fork 27
/
nds.go
225 lines (193 loc) · 5.33 KB
/
nds.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
217
218
219
220
221
222
223
224
225
package nds
import (
"bytes"
"crypto/sha1"
"encoding/gob"
"encoding/hex"
"errors"
"reflect"
"time"
"cloud.google.com/go/datastore"
)
const (
// cachePrefix is the namespace the cache uses to store entities.
cachePrefix = "NDS1:"
// cacheLockTime is the maximum length of time a cache lock will be
// held for. 32 seconds is chosen as 30 seconds is the maximum amount of
// time an underlying datastore call will retry even if the API reports a
// success to the user.
// TODO: Is this still accurate?
cacheLockTime = 32 * time.Second
// cacheMaxKeySize is the maximum size an item key will be. Keys
// greater than this size are automatically hashed to a smaller size.
cacheMaxKeySize = 250
)
var (
typeOfPropertyLoadSaver = reflect.TypeOf(
(*datastore.PropertyLoadSaver)(nil)).Elem()
typeOfPropertyList = reflect.TypeOf(datastore.PropertyList(nil))
typeOfKeyLoader = reflect.TypeOf(
(*datastore.KeyLoader)(nil)).Elem()
)
// The variables in this block are here so that we can test all error code
// paths by substituting them with error producing ones.
var (
marshal = marshalPropertyList
unmarshal = unmarshalPropertyList
)
const (
noneItem uint32 = iota
entityItem
lockItem
)
func init() {
gob.Register(time.Time{})
gob.Register(&datastore.Key{})
gob.Register(datastore.GeoPoint{})
gob.Register(&datastore.Entity{})
gob.Register([]interface{}{})
}
type valueType int
const (
valueTypeInvalid valueType = iota
valueTypePropertyLoadSaver
valueTypeStruct
valueTypeStructPtr
valueTypeInterface
valueTypeKeyLoader
)
func checkValueType(valType reflect.Type) valueType {
if reflect.PtrTo(valType).Implements(typeOfKeyLoader) {
return valueTypeKeyLoader
}
if reflect.PtrTo(valType).Implements(typeOfPropertyLoadSaver) {
return valueTypePropertyLoadSaver
}
switch valType.Kind() {
case reflect.Struct:
return valueTypeStruct
case reflect.Interface:
return valueTypeInterface
case reflect.Ptr:
valType = valType.Elem()
if valType.Kind() == reflect.Struct {
return valueTypeStructPtr
}
}
return valueTypeInvalid
}
func checkKeysValues(keys []*datastore.Key, values reflect.Value) error {
if values.Kind() != reflect.Slice {
return errors.New("nds: values is not a slice")
}
if len(keys) != values.Len() {
return errors.New("nds: keys and values slices have different length")
}
isNilErr, nilErr := false, make(datastore.MultiError, len(keys))
for i, key := range keys {
if key == nil {
isNilErr = true
nilErr[i] = datastore.ErrInvalidKey
}
}
if isNilErr {
return nilErr
}
if values.Type() == typeOfPropertyList {
return errors.New("nds: PropertyList not supported")
}
if ty := checkValueType(values.Type().Elem()); ty == valueTypeInvalid {
return errors.New("nds: unsupported vals type")
}
return nil
}
func createCacheKey(key *datastore.Key) string {
cacheKey := cachePrefix + key.Encode()
if len(cacheKey) > cacheMaxKeySize {
hash := sha1.Sum([]byte(cacheKey))
cacheKey = hex.EncodeToString(hash[:])
}
return cacheKey
}
func marshalPropertyList(pl datastore.PropertyList) ([]byte, error) {
buf := bytes.Buffer{}
if err := gob.NewEncoder(&buf).Encode(&pl); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func unmarshalPropertyList(data []byte, pl *datastore.PropertyList) error {
return gob.NewDecoder(bytes.NewBuffer(data)).Decode(pl)
}
func setValue(val reflect.Value, pl datastore.PropertyList, key *datastore.Key) error {
valType := checkValueType(val.Type())
if valType == valueTypePropertyLoadSaver || valType == valueTypeStruct || valType == valueTypeKeyLoader {
val = val.Addr()
}
if valType == valueTypeStructPtr && val.IsNil() {
val.Set(reflect.New(val.Type().Elem()))
}
if pls, ok := val.Interface().(datastore.PropertyLoadSaver); ok {
err := pls.Load(pl)
if err != nil {
return err
}
if e, ok := val.Interface().(datastore.KeyLoader); ok {
err = e.LoadKey(key)
}
return err
}
return datastore.LoadStruct(val.Interface(), pl)
}
func isErrorsNil(errs []error) bool {
for _, err := range errs {
if err != nil {
return false
}
}
return true
}
func groupErrors(errs []error, total, limit int) error {
groupedErrs := make(datastore.MultiError, total)
for i, err := range errs {
lo := i * limit
hi := (i + 1) * limit
if hi > total {
hi = total
}
if me, ok := err.(datastore.MultiError); ok {
copy(groupedErrs[lo:hi], me)
} else if err != nil {
for j := lo; j < hi; j++ {
groupedErrs[j] = err
}
}
}
return groupedErrs
}
// getCacheLocks will create cache Items locks for the given datastore keys.
// It also removes duplicate entries.
func getCacheLocks(keys []*datastore.Key) ([]string, []*Item) {
lockCacheKeys := make([]string, 0, len(keys))
lockCacheItems := make([]*Item, 0, len(keys))
set := make(map[string]interface{})
for _, key := range keys {
// Worst case scenario is that we lock the entity for cacheLockTime.
// datastore.Delete will raise the appropriate error.
if key != nil && !key.Incomplete() {
cacheKey := createCacheKey(key)
if _, found := set[cacheKey]; !found {
item := &Item{
Key: cacheKey,
Flags: lockItem,
Value: itemLock(),
Expiration: cacheLockTime,
}
lockCacheItems = append(lockCacheItems, item)
lockCacheKeys = append(lockCacheKeys, item.Key)
set[cacheKey] = nil
}
}
}
return lockCacheKeys, lockCacheItems
}