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

Unified 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: Add empty arg/key short-circuits for other varidic methods. 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 side-by-side diff with in-line comments
Download patch
Index: filter/txnBuf/state.go
diff --git a/filter/txnBuf/state.go b/filter/txnBuf/state.go
index 4e22c3d12a584b3aa9b5e1531827852f9210d42b..ad23a42fabd2e87231fc459e93f6c9df765ee548 100644
--- a/filter/txnBuf/state.go
+++ b/filter/txnBuf/state.go
@@ -363,30 +363,52 @@ func (t *txnBufState) deleteMulti(keys []*datastore.Key, cb datastore.DeleteMult
}
func (t *txnBufState) fixKeys(keys []*datastore.Key) ([]*datastore.Key, error) {
- lme := errors.NewLazyMultiError(len(keys))
- realKeys := []*datastore.Key(nil)
+ // Identify any incomplete keys and allocate IDs for them.
+ //
+ // In order to facilitate this, we will maintain a mapping of the
+ // incompleteKeys index to the key's corresponding index in the keys array.
+ // Any errors or allocations on incompleteKeys operations will be propagated
+ // to the correct keys index using this map.
+ var (
+ incompleteKeys []*datastore.Key
+ incompleteMap map[int]int
+ )
+
for i, key := range keys {
if key.Incomplete() {
- // intentionally call AllocateIDs without lock.
- start, err := t.parentDS.AllocateIDs(key, 1)
- if !lme.Assign(i, err) {
- if realKeys == nil {
- realKeys = make([]*datastore.Key, len(keys))
- copy(realKeys, keys)
- }
-
- aid, ns, toks := key.Split()
- toks[len(toks)-1].IntID = start
- realKeys[i] = datastore.NewKeyToks(aid, ns, toks)
+ if incompleteMap == nil {
+ incompleteMap = make(map[int]int)
}
+ incompleteMap[len(incompleteKeys)] = i
+ incompleteKeys = append(incompleteKeys, key)
}
}
- err := lme.Get()
+ if len(incompleteKeys) == 0 {
+ return keys, nil
+ }
+
+ // We're going to update keys, so clone it.
+ keys, origKeys := make([]*datastore.Key, len(keys)), keys
+ copy(keys, origKeys)
+
+ // Intentionally call AllocateIDs without lock.
+ i := 0
+ outerErr := errors.NewLazyMultiError(len(keys))
+ err := t.parentDS.AllocateIDs(incompleteKeys, func(key *datastore.Key, err error) error {
+ outerIdx := incompleteMap[i]
+ i++
- if realKeys != nil {
- return realKeys, err
+ if err != nil {
+ outerErr.Assign(outerIdx, err)
+ } else {
+ keys[outerIdx] = key
+ }
+ return nil
+ })
+ if err != nil {
+ return nil, err
}
- return keys, err
+ return keys, outerErr.Get()
}
func (t *txnBufState) putMulti(keys []*datastore.Key, vals []datastore.PropertyMap, cb datastore.PutMultiCB, haveLock bool) error {

Powered by Google App Engine
This is Rietveld 408576698