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

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: Rebase, comments. 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
« no previous file with comments | « impl/prod/everything_test.go ('k') | service/datastore/checkfilter.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be 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.NewKeyCB) 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 10 matching lines...) Expand all
88 } 123 }
89 return idxCallbacker(err, len(keys), func(idx int, err error) { 124 return idxCallbacker(err, len(keys), func(idx int, err error) {
90 if pls := vals[idx]; pls != nil { 125 if pls := vals[idx]; pls != nil {
91 cb(pls.(*typeFilter).pm, err) 126 cb(pls.(*typeFilter).pm, err)
92 } else { 127 } else {
93 cb(nil, err) 128 cb(nil, err)
94 } 129 }
95 }) 130 })
96 } 131 }
97 132
98 func (d rdsImpl) PutMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.PutMultiC B) error { 133 func (d rdsImpl) PutMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.NewKeyCB) error {
99 rkeys, err := dsMF2R(d.aeCtx, keys) 134 rkeys, err := dsMF2R(d.aeCtx, keys)
100 if err == nil { 135 if err == nil {
101 rvals := make([]datastore.PropertyLoadSaver, len(vals)) 136 rvals := make([]datastore.PropertyLoadSaver, len(vals))
102 for i, val := range vals { 137 for i, val := range vals {
103 rvals[i] = &typeFilter{d.aeCtx, val} 138 rvals[i] = &typeFilter{d.aeCtx, val}
104 } 139 }
105 rkeys, err = datastore.PutMulti(d.aeCtx, rkeys, rvals) 140 rkeys, err = datastore.PutMulti(d.aeCtx, rkeys, rvals)
106 } 141 }
107 return idxCallbacker(err, len(keys), func(idx int, err error) { 142 return idxCallbacker(err, len(keys), func(idx int, err error) {
108 k := (*ds.Key)(nil) 143 k := (*ds.Key)(nil)
(...skipping 121 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
« no previous file with comments | « impl/prod/everything_test.go ('k') | service/datastore/checkfilter.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698