OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package datastore | 5 package datastore |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "reflect" | 9 "reflect" |
10 | 10 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 | 55 |
56 badSig := false | 56 badSig := false |
57 mat := multiArgType{} | 57 mat := multiArgType{} |
58 isKey := false | 58 isKey := false |
59 | 59 |
60 if cbTyp.Kind() == reflect.Func && cbTyp.NumIn() == 2 && cbTyp.NumOut()
== 1 { | 60 if cbTyp.Kind() == reflect.Func && cbTyp.NumIn() == 2 && cbTyp.NumOut()
== 1 { |
61 firstArg := cbTyp.In(0) | 61 firstArg := cbTyp.In(0) |
62 if firstArg == typeOfKey { | 62 if firstArg == typeOfKey { |
63 isKey = true | 63 isKey = true |
64 } else { | 64 } else { |
65 » » » mat = parseArg(firstArg) | 65 » » » mat = parseArg(firstArg, false) |
66 » » » badSig = !mat.valid || mat.newElem == nil | 66 » » » badSig = mat.newElem == nil |
67 } | 67 } |
68 } else { | 68 } else { |
69 badSig = true | 69 badSig = true |
70 } | 70 } |
71 | 71 |
72 if badSig || cbTyp.Out(0) != typeOfBool || cbTyp.In(1) != typeOfCursorCB
{ | 72 if badSig || cbTyp.Out(0) != typeOfBool || cbTyp.In(1) != typeOfCursorCB
{ |
73 panic(fmt.Errorf( | 73 panic(fmt.Errorf( |
74 "cb does not match the required callback signature: `%T`
!= `func(TYPE, CursorCB) bool`", | 74 "cb does not match the required callback signature: `%T`
!= `func(TYPE, CursorCB) bool`", |
75 cbIface)) | 75 cbIface)) |
76 } | 76 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 return true | 136 return true |
137 }) | 137 }) |
138 } | 138 } |
139 fq, err := q.Finalize() | 139 fq, err := q.Finalize() |
140 if err != nil { | 140 if err != nil { |
141 return err | 141 return err |
142 } | 142 } |
143 | 143 |
144 slice := v.Elem() | 144 slice := v.Elem() |
145 mat := parseMultiArg(slice.Type()) | 145 mat := parseMultiArg(slice.Type()) |
146 » if !mat.valid || mat.newElem == nil { | 146 » if mat.newElem == nil { |
147 return fmt.Errorf("invalid GetAll input type: %T", dst) | 147 return fmt.Errorf("invalid GetAll input type: %T", dst) |
148 } | 148 } |
149 | 149 |
150 errs := map[int]error{} | 150 errs := map[int]error{} |
151 i := 0 | 151 i := 0 |
152 err = d.RawInterface.Run(fq, func(k *Key, pm PropertyMap, _ CursorCB) bo
ol { | 152 err = d.RawInterface.Run(fq, func(k *Key, pm PropertyMap, _ CursorCB) bo
ol { |
153 slice.Set(reflect.Append(slice, mat.newElem())) | 153 slice.Set(reflect.Append(slice, mat.newElem())) |
154 itm := slice.Index(i) | 154 itm := slice.Index(i) |
155 mat.setKey(itm, k) | 155 mat.setKey(itm, k) |
156 err := mat.setPM(itm, pm) | 156 err := mat.setPM(itm, pm) |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 return errors.SingleError(d.PutMulti([]interface{}{src})) | 219 return errors.SingleError(d.PutMulti([]interface{}{src})) |
220 } | 220 } |
221 | 221 |
222 func (d *datastoreImpl) Delete(key *Key) (err error) { | 222 func (d *datastoreImpl) Delete(key *Key) (err error) { |
223 return errors.SingleError(d.DeleteMulti([]*Key{key})) | 223 return errors.SingleError(d.DeleteMulti([]*Key{key})) |
224 } | 224 } |
225 | 225 |
226 func (d *datastoreImpl) GetMulti(dst interface{}) error { | 226 func (d *datastoreImpl) GetMulti(dst interface{}) error { |
227 slice := reflect.ValueOf(dst) | 227 slice := reflect.ValueOf(dst) |
228 mat := parseMultiArg(slice.Type()) | 228 mat := parseMultiArg(slice.Type()) |
229 if !mat.valid { | |
230 return fmt.Errorf("invalid GetMulti input type: %T", dst) | |
231 } | |
232 | 229 |
233 keys, pms, err := mat.GetKeysPMs(d.aid, d.ns, slice, true) | 230 keys, pms, err := mat.GetKeysPMs(d.aid, d.ns, slice, true) |
234 if err != nil { | 231 if err != nil { |
235 return err | 232 return err |
236 } | 233 } |
237 | 234 |
238 lme := errors.NewLazyMultiError(len(keys)) | 235 lme := errors.NewLazyMultiError(len(keys)) |
239 i := 0 | 236 i := 0 |
240 meta := NewMultiMetaGetter(pms) | 237 meta := NewMultiMetaGetter(pms) |
241 err = d.RawInterface.GetMulti(keys, meta, func(pm PropertyMap, err error
) { | 238 err = d.RawInterface.GetMulti(keys, meta, func(pm PropertyMap, err error
) { |
242 if !lme.Assign(i, err) { | 239 if !lme.Assign(i, err) { |
243 lme.Assign(i, mat.setPM(slice.Index(i), pm)) | 240 lme.Assign(i, mat.setPM(slice.Index(i), pm)) |
244 } | 241 } |
245 i++ | 242 i++ |
246 }) | 243 }) |
247 | 244 |
248 if err == nil { | 245 if err == nil { |
249 err = lme.Get() | 246 err = lme.Get() |
250 } | 247 } |
251 return err | 248 return err |
252 } | 249 } |
253 | 250 |
254 func (d *datastoreImpl) PutMulti(src interface{}) error { | 251 func (d *datastoreImpl) PutMulti(src interface{}) error { |
255 slice := reflect.ValueOf(src) | 252 slice := reflect.ValueOf(src) |
256 mat := parseMultiArg(slice.Type()) | 253 mat := parseMultiArg(slice.Type()) |
257 if !mat.valid { | |
258 return fmt.Errorf("invalid PutMulti input type: %T", src) | |
259 } | |
260 | 254 |
261 keys, vals, err := mat.GetKeysPMs(d.aid, d.ns, slice, false) | 255 keys, vals, err := mat.GetKeysPMs(d.aid, d.ns, slice, false) |
262 if err != nil { | 256 if err != nil { |
263 return err | 257 return err |
264 } | 258 } |
265 | 259 |
266 lme := errors.NewLazyMultiError(len(keys)) | 260 lme := errors.NewLazyMultiError(len(keys)) |
267 i := 0 | 261 i := 0 |
268 err = d.RawInterface.PutMulti(keys, vals, func(key *Key, err error) { | 262 err = d.RawInterface.PutMulti(keys, vals, func(key *Key, err error) { |
269 if key != keys[i] { | 263 if key != keys[i] { |
(...skipping 19 matching lines...) Expand all Loading... |
289 err = lme.Get() | 283 err = lme.Get() |
290 if err == nil { | 284 if err == nil { |
291 err = extErr | 285 err = extErr |
292 } | 286 } |
293 return | 287 return |
294 } | 288 } |
295 | 289 |
296 func (d *datastoreImpl) Raw() RawInterface { | 290 func (d *datastoreImpl) Raw() RawInterface { |
297 return d.RawInterface | 291 return d.RawInterface |
298 } | 292 } |
OLD | NEW |