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

Side by Side Diff: filter/txnBuf/state.go

Issue 2007123002: datastore: Update AllocateIDs to take keys. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/gae@master
Patch Set: Created 4 years, 7 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 txnBuf 5 package txnBuf
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "sync" 9 "sync"
10 10
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 for range keys { 356 for range keys {
357 if err := cb(nil); err != nil { 357 if err := cb(nil); err != nil {
358 return err 358 return err
359 } 359 }
360 } 360 }
361 361
362 return nil 362 return nil
363 } 363 }
364 364
365 func (t *txnBufState) fixKeys(keys []*datastore.Key) ([]*datastore.Key, error) { 365 func (t *txnBufState) fixKeys(keys []*datastore.Key) ([]*datastore.Key, error) {
366 » lme := errors.NewLazyMultiError(len(keys)) 366 » // Identify any incomplete keys and allocate IDs for them.
367 » realKeys := []*datastore.Key(nil) 367 » //
368 » // In order to facilitate this, we will maintain a mapping of the
369 » // incompleteKeys index to the key's corresponding index in the keys arr ay.
370 » // Any errors or allocations on incompleteKeys operations will be propag ated
371 » // to the correct keys index using this map.
372 » var incompleteKeys []*datastore.Key
373 » incompleteMap := make(map[int]int)
374
368 for i, key := range keys { 375 for i, key := range keys {
369 if key.Incomplete() { 376 if key.Incomplete() {
370 » » » // intentionally call AllocateIDs without lock. 377 » » » incompleteMap[len(incompleteKeys)] = i
371 » » » start, err := t.parentDS.AllocateIDs(key, 1) 378 » » » incompleteKeys = append(incompleteKeys, key)
372 » » » if !lme.Assign(i, err) {
373 » » » » if realKeys == nil {
374 » » » » » realKeys = make([]*datastore.Key, len(ke ys))
375 » » » » » copy(realKeys, keys)
376 » » » » }
377
378 » » » » aid, ns, toks := key.Split()
379 » » » » toks[len(toks)-1].IntID = start
380 » » » » realKeys[i] = datastore.NewKeyToks(aid, ns, toks )
381 » » » }
382 } 379 }
383 } 380 }
384 » err := lme.Get() 381 » if len(incompleteKeys) == 0 {
382 » » return keys, nil
383 » }
385 384
386 » if realKeys != nil { 385 » // Intentionally call AllocateIDs without lock.
387 » » return realKeys, err 386 » if err := t.parentDS.AllocateIDs(incompleteKeys); err != nil {
387 » » // If we received a MultiError, map the errors back to our outer
388 » » // LazyMultiError.
389 » » if merr, ok := err.(errors.MultiError); ok {
390 » » » outerErr := make(errors.MultiError, len(keys))
391 » » » for i, e := range merr {
392 » » » » if e != nil {
393 » » » » » outerErr[incompleteMap[i]] = e
394 » » » » }
395 » » » }
396 » » » err = outerErr
397 » » }
398 » » return nil, err
388 } 399 }
389 » return keys, err 400
401 » // Allocate a new keys slice, copy all initially complete keys into it, and
402 » // load the newly-allocated keys into it.
403 » keys, origKeys := make([]*datastore.Key, len(keys)), keys
404 » copy(keys, origKeys)
405 » for i, k := range incompleteKeys {
406 » » keys[incompleteMap[i]] = k
407 » }
408 » return keys, nil
390 } 409 }
391 410
392 func (t *txnBufState) putMulti(keys []*datastore.Key, vals []datastore.PropertyM ap, cb datastore.PutMultiCB, haveLock bool) error { 411 func (t *txnBufState) putMulti(keys []*datastore.Key, vals []datastore.PropertyM ap, cb datastore.PutMultiCB, haveLock bool) error {
393 keys, err := t.fixKeys(keys) 412 keys, err := t.fixKeys(keys)
394 if err != nil { 413 if err != nil {
395 for _, e := range err.(errors.MultiError) { 414 for _, e := range err.(errors.MultiError) {
396 if err := cb(nil, e); err != nil { 415 if err := cb(nil, e); err != nil {
397 return err 416 return err
398 } 417 }
399 } 418 }
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 // plus a stringset of all the encoded root keys that `keys` represents. 561 // plus a stringset of all the encoded root keys that `keys` represents.
543 func toEncoded(keys []*datastore.Key) (full []string, roots stringset.Set) { 562 func toEncoded(keys []*datastore.Key) (full []string, roots stringset.Set) {
544 roots = stringset.New(len(keys)) 563 roots = stringset.New(len(keys))
545 full = make([]string, len(keys)) 564 full = make([]string, len(keys))
546 for i, k := range keys { 565 for i, k := range keys {
547 roots.Add(string(serialize.ToBytes(k.Root()))) 566 roots.Add(string(serialize.ToBytes(k.Root())))
548 full[i] = string(serialize.ToBytes(k)) 567 full[i] = string(serialize.ToBytes(k))
549 } 568 }
550 return 569 return
551 } 570 }
OLDNEW
« no previous file with comments | « filter/txnBuf/ds_txn.go ('k') | filter/txnBuf/txnbuf_test.go » ('j') | impl/dummy/dummy.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698