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

Side by Side Diff: impl/prod/raw_datastore.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 prod 5 package prod
6 6
7 import ( 7 import (
8 ds "github.com/luci/gae/service/datastore" 8 ds "github.com/luci/gae/service/datastore"
9 "github.com/luci/luci-go/common/errors" 9 "github.com/luci/luci-go/common/errors"
10 "golang.org/x/net/context" 10 "golang.org/x/net/context"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 me, ok := err.(errors.MultiError) 50 me, ok := err.(errors.MultiError)
51 if ok { 51 if ok {
52 for i, err := range me { 52 for i, err := range me {
53 cb(i, err) 53 cb(i, err)
54 } 54 }
55 return nil 55 return nil
56 } 56 }
57 return err 57 return err
58 } 58 }
59 59
60 func (d rdsImpl) AllocateIDs(incomplete *ds.Key, n int) (start int64, err error) { 60 func (d rdsImpl) AllocateIDs(keys []*ds.Key, cb ds.PutMultiCB) error {
61 » par, err := dsF2R(d.aeCtx, incomplete.Parent()) 61 » // Map keys by entity type.
62 » if err != nil { 62 » entityMap := make(map[string][]int)
63 » » return 63 » for i, key := range keys {
64 » » ks := key.String()
65 » » entityMap[ks] = append(entityMap[ks], i)
64 } 66 }
65 67
66 » start, _, err = datastore.AllocateIDs(d.aeCtx, incomplete.Kind(), par, n ) 68 » // Allocate a set of IDs for each unique entity type.
67 » return 69 » errors := errors.NewLazyMultiError(len(keys))
70 » setErrs := func(idxs []int, err error) {
71 » » for _, idx := range idxs {
72 » » » errors.Assign(idx, err)
73 » » }
74 » }
75
76 » for _, idxs := range entityMap {
77 » » incomplete := keys[idxs[0]]
78 » » par, err := dsF2R(d.aeCtx, incomplete.Parent())
79 » » if err != nil {
80 » » » setErrs(idxs, err)
81 » » » continue
82 » » }
83
84 » » start, _, err := datastore.AllocateIDs(d.aeCtx, incomplete.Kind( ), par, len(idxs))
85 » » if err != nil {
86 » » » setErrs(idxs, err)
87 » » » continue
88 » » }
89
90 » » for i, idx := range idxs {
91 » » » keys[idx] = incomplete.WithID("", start+int64(i))
92 » » }
93 » }
94
95 » for i, key := range keys {
96 » » if err := errors.GetOne(i); err != nil {
97 » » » cb(nil, err)
98 » » } else {
99 » » » cb(key, nil)
100 » » }
101 » }
102 » return nil
68 } 103 }
69 104
70 func (d rdsImpl) DeleteMulti(ks []*ds.Key, cb ds.DeleteMultiCB) error { 105 func (d rdsImpl) DeleteMulti(ks []*ds.Key, cb ds.DeleteMultiCB) error {
71 keys, err := dsMF2R(d.aeCtx, ks) 106 keys, err := dsMF2R(d.aeCtx, ks)
72 if err == nil { 107 if err == nil {
73 err = datastore.DeleteMulti(d.aeCtx, keys) 108 err = datastore.DeleteMulti(d.aeCtx, keys)
74 } 109 }
75 return idxCallbacker(err, len(ks), func(_ int, err error) { 110 return idxCallbacker(err, len(ks), func(_ int, err error) {
76 cb(err) 111 cb(err)
77 }) 112 })
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 func (d rdsImpl) RunInTransaction(f func(c context.Context) error, opts *ds.Tran sactionOptions) error { 265 func (d rdsImpl) RunInTransaction(f func(c context.Context) error, opts *ds.Tran sactionOptions) error {
231 ropts := (*datastore.TransactionOptions)(opts) 266 ropts := (*datastore.TransactionOptions)(opts)
232 return datastore.RunInTransaction(d.aeCtx, func(c context.Context) error { 267 return datastore.RunInTransaction(d.aeCtx, func(c context.Context) error {
233 return f(context.WithValue(d.userCtx, prodContextKey, c)) 268 return f(context.WithValue(d.userCtx, prodContextKey, c))
234 }, ropts) 269 }, ropts)
235 } 270 }
236 271
237 func (d rdsImpl) Testable() ds.Testable { 272 func (d rdsImpl) Testable() ds.Testable {
238 return nil 273 return nil
239 } 274 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698