Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(608)

Side by Side Diff: impl/memory/datastore_data.go

Issue 2007123002: datastore: Update AllocateIDs to take keys. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/gae@master
Patch Set: Add empty arg/key short-circuits for other varidic methods. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 memory 5 package memory
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "fmt" 9 "fmt"
10 "strings" 10 "strings"
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 if amt <= 0 { 208 if amt <= 0 {
209 panic(fmt.Errorf("incrementLocked called with bad `amt`: %d", am t)) 209 panic(fmt.Errorf("incrementLocked called with bad `amt`: %d", am t))
210 } 210 }
211 ret := curVersion(ents, key) + 1 211 ret := curVersion(ents, key) + 1
212 ents.Set(key, serialize.ToBytes(ds.PropertyMap{ 212 ents.Set(key, serialize.ToBytes(ds.PropertyMap{
213 "__version__": {ds.MkPropertyNI(ret + int64(amt-1))}, 213 "__version__": {ds.MkPropertyNI(ret + int64(amt-1))},
214 })) 214 }))
215 return ret 215 return ret
216 } 216 }
217 217
218 func (d *dataStoreData) allocateIDs(incomplete *ds.Key, n int) (int64, error) { 218 func (d *dataStoreData) allocateIDs(keys []*ds.Key, cb ds.PutMultiCB) error {
219 » d.Lock() 219 » // Map keys by entity type.
220 » defer d.Unlock() 220 » entityMap := make(map[string][]int)
221 » for i, key := range keys {
222 » » ks := key.String()
223 » » entityMap[ks] = append(entityMap[ks], i)
224 » }
221 225
222 » ents := d.head.GetOrCreateCollection("ents:" + incomplete.Namespace()) 226 » // Allocate IDs for our keys. We use an inline function so we can ensure that
223 » return d.allocateIDsLocked(ents, incomplete, n) 227 » // the lock is released.
228 » err := func() error {
229 » » d.Lock()
230 » » defer d.Unlock()
231
232 » » for _, idxs := range entityMap {
233 » » » baseKey := keys[idxs[0]]
234
235 » » » ents := d.head.GetOrCreateCollection("ents:" + baseKey.N amespace())
236
237 » » » // Allocate IDs. The only possible error is when disable SpecialEntities is
238 » » » // true, in which case we will return a full method erro r instead of
239 » » » // individual callback errors.
240 » » » start, err := d.allocateIDsLocked(ents, baseKey, len(idx s))
241 » » » if err != nil {
242 » » » » return err
243 » » » }
244
245 » » » for i, idx := range idxs {
246 » » » » keys[idx] = baseKey.WithID("", start+int64(i))
247 » » » }
248 » » }
249 » » return nil
250 » }()
251 » if err != nil {
252 » » return err
253 » }
254
255 » // Execute Callbacks.
256 » for _, key := range keys {
257 » » cb(key, nil)
258 » }
259 » return nil
224 } 260 }
225 261
226 func (d *dataStoreData) allocateIDsLocked(ents memCollection, incomplete *ds.Key , n int) (int64, error) { 262 func (d *dataStoreData) allocateIDsLocked(ents memCollection, incomplete *ds.Key , n int) (int64, error) {
227 if d.disableSpecialEntities { 263 if d.disableSpecialEntities {
228 » » return 0, errors.New("disableSpecialEntities is true so allocate IDs is disabled") 264 » » return -1, errors.New("disableSpecialEntities is true so allocat eIDs is disabled")
iannucci 2016/06/14 01:46:04 0 is an invalid id, this shouldn't need to be -1.
dnj (Google) 2016/06/14 04:24:18 Done.
229 } 265 }
230 266
231 idKey := []byte(nil) 267 idKey := []byte(nil)
232 if incomplete.Parent() == nil { 268 if incomplete.Parent() == nil {
233 idKey = rootIDsKey(incomplete.Kind()) 269 idKey = rootIDsKey(incomplete.Kind())
234 } else { 270 } else {
235 idKey = groupIDsKey(incomplete) 271 idKey = groupIDsKey(incomplete)
236 } 272 }
237 return incrementLocked(ents, idKey, n), nil 273 return incrementLocked(ents, idKey, n), nil
238 } 274 }
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 } 620 }
585 return namespaces 621 return namespaces
586 } 622 }
587 623
588 func trimPrefix(v, p string) (string, bool) { 624 func trimPrefix(v, p string) (string, bool) {
589 if strings.HasPrefix(v, p) { 625 if strings.HasPrefix(v, p) {
590 return v[len(p):], true 626 return v[len(p):], true
591 } 627 }
592 return v, false 628 return v, false
593 } 629 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698