OLD | NEW |
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 21 matching lines...) Expand all Loading... |
32 | 32 |
33 type rdsImpl struct { | 33 type rdsImpl struct { |
34 // userCtx is the context that has the luci/gae services and user object
s in | 34 // userCtx is the context that has the luci/gae services and user object
s in |
35 // it. | 35 // it. |
36 userCtx context.Context | 36 userCtx context.Context |
37 | 37 |
38 // aeCtx is the context with the appengine connection information in it. | 38 // aeCtx is the context with the appengine connection information in it. |
39 aeCtx context.Context | 39 aeCtx context.Context |
40 } | 40 } |
41 | 41 |
42 func idxCallbacker(err error, amt int, cb func(idx int, err error)) error { | 42 func idxCallbacker(err error, amt int, cb func(idx int, err error) error) error
{ |
43 if err == nil { | 43 if err == nil { |
44 for i := 0; i < amt; i++ { | 44 for i := 0; i < amt; i++ { |
45 » » » cb(i, nil) | 45 » » » if err := cb(i, nil); err != nil { |
| 46 » » » » return err |
| 47 » » » } |
46 } | 48 } |
47 return nil | 49 return nil |
48 } | 50 } |
49 err = errors.Fix(err) | 51 err = errors.Fix(err) |
50 me, ok := err.(errors.MultiError) | 52 me, ok := err.(errors.MultiError) |
51 if ok { | 53 if ok { |
52 for i, err := range me { | 54 for i, err := range me { |
53 » » » cb(i, err) | 55 » » » if err := cb(i, err); err != nil { |
| 56 » » » » return err |
| 57 » » » } |
54 } | 58 } |
55 return nil | 59 return nil |
56 } | 60 } |
57 return err | 61 return err |
58 } | 62 } |
59 | 63 |
60 func (d rdsImpl) AllocateIDs(incomplete *ds.Key, n int) (start int64, err error)
{ | 64 func (d rdsImpl) AllocateIDs(incomplete *ds.Key, n int) (start int64, err error)
{ |
61 par, err := dsF2R(d.aeCtx, incomplete.Parent()) | 65 par, err := dsF2R(d.aeCtx, incomplete.Parent()) |
62 if err != nil { | 66 if err != nil { |
63 return | 67 return |
64 } | 68 } |
65 | 69 |
66 start, _, err = datastore.AllocateIDs(d.aeCtx, incomplete.Kind(), par, n
) | 70 start, _, err = datastore.AllocateIDs(d.aeCtx, incomplete.Kind(), par, n
) |
67 return | 71 return |
68 } | 72 } |
69 | 73 |
70 func (d rdsImpl) DeleteMulti(ks []*ds.Key, cb ds.DeleteMultiCB) error { | 74 func (d rdsImpl) DeleteMulti(ks []*ds.Key, cb ds.DeleteMultiCB) error { |
71 keys, err := dsMF2R(d.aeCtx, ks) | 75 keys, err := dsMF2R(d.aeCtx, ks) |
72 if err == nil { | 76 if err == nil { |
73 err = datastore.DeleteMulti(d.aeCtx, keys) | 77 err = datastore.DeleteMulti(d.aeCtx, keys) |
74 } | 78 } |
75 » return idxCallbacker(err, len(ks), func(_ int, err error) { | 79 » return idxCallbacker(err, len(ks), func(_ int, err error) error { |
76 » » cb(err) | 80 » » return cb(err) |
77 }) | 81 }) |
78 } | 82 } |
79 | 83 |
80 func (d rdsImpl) GetMulti(keys []*ds.Key, _meta ds.MultiMetaGetter, cb ds.GetMul
tiCB) error { | 84 func (d rdsImpl) GetMulti(keys []*ds.Key, _meta ds.MultiMetaGetter, cb ds.GetMul
tiCB) error { |
81 vals := make([]datastore.PropertyLoadSaver, len(keys)) | 85 vals := make([]datastore.PropertyLoadSaver, len(keys)) |
82 rkeys, err := dsMF2R(d.aeCtx, keys) | 86 rkeys, err := dsMF2R(d.aeCtx, keys) |
83 if err == nil { | 87 if err == nil { |
84 for i := range keys { | 88 for i := range keys { |
85 vals[i] = &typeFilter{d.aeCtx, ds.PropertyMap{}} | 89 vals[i] = &typeFilter{d.aeCtx, ds.PropertyMap{}} |
86 } | 90 } |
87 err = datastore.GetMulti(d.aeCtx, rkeys, vals) | 91 err = datastore.GetMulti(d.aeCtx, rkeys, vals) |
88 } | 92 } |
89 » return idxCallbacker(err, len(keys), func(idx int, err error) { | 93 » return idxCallbacker(err, len(keys), func(idx int, err error) error { |
90 if pls := vals[idx]; pls != nil { | 94 if pls := vals[idx]; pls != nil { |
91 » » » cb(pls.(*typeFilter).pm, err) | 95 » » » return cb(pls.(*typeFilter).pm, err) |
92 » » } else { | |
93 » » » cb(nil, err) | |
94 } | 96 } |
| 97 return cb(nil, err) |
95 }) | 98 }) |
96 } | 99 } |
97 | 100 |
98 func (d rdsImpl) PutMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.PutMultiC
B) error { | 101 func (d rdsImpl) PutMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.PutMultiC
B) error { |
99 rkeys, err := dsMF2R(d.aeCtx, keys) | 102 rkeys, err := dsMF2R(d.aeCtx, keys) |
100 if err == nil { | 103 if err == nil { |
101 rvals := make([]datastore.PropertyLoadSaver, len(vals)) | 104 rvals := make([]datastore.PropertyLoadSaver, len(vals)) |
102 for i, val := range vals { | 105 for i, val := range vals { |
103 rvals[i] = &typeFilter{d.aeCtx, val} | 106 rvals[i] = &typeFilter{d.aeCtx, val} |
104 } | 107 } |
105 rkeys, err = datastore.PutMulti(d.aeCtx, rkeys, rvals) | 108 rkeys, err = datastore.PutMulti(d.aeCtx, rkeys, rvals) |
106 } | 109 } |
107 » return idxCallbacker(err, len(keys), func(idx int, err error) { | 110 » return idxCallbacker(err, len(keys), func(idx int, err error) error { |
108 k := (*ds.Key)(nil) | 111 k := (*ds.Key)(nil) |
109 if err == nil { | 112 if err == nil { |
110 k = dsR2F(rkeys[idx]) | 113 k = dsR2F(rkeys[idx]) |
111 } | 114 } |
112 » » cb(k, err) | 115 » » return cb(k, err) |
113 }) | 116 }) |
114 } | 117 } |
115 | 118 |
116 func (d rdsImpl) fixQuery(fq *ds.FinalizedQuery) (*datastore.Query, error) { | 119 func (d rdsImpl) fixQuery(fq *ds.FinalizedQuery) (*datastore.Query, error) { |
117 ret := datastore.NewQuery(fq.Kind()) | 120 ret := datastore.NewQuery(fq.Kind()) |
118 | 121 |
119 start, end := fq.Bounds() | 122 start, end := fq.Bounds() |
120 if start != nil { | 123 if start != nil { |
121 ret = ret.Start(start.(datastore.Cursor)) | 124 ret = ret.Start(start.(datastore.Cursor)) |
122 } | 125 } |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 func (d rdsImpl) RunInTransaction(f func(c context.Context) error, opts *ds.Tran
sactionOptions) error { | 233 func (d rdsImpl) RunInTransaction(f func(c context.Context) error, opts *ds.Tran
sactionOptions) error { |
231 ropts := (*datastore.TransactionOptions)(opts) | 234 ropts := (*datastore.TransactionOptions)(opts) |
232 return datastore.RunInTransaction(d.aeCtx, func(c context.Context) error
{ | 235 return datastore.RunInTransaction(d.aeCtx, func(c context.Context) error
{ |
233 return f(context.WithValue(d.userCtx, prodContextKey, c)) | 236 return f(context.WithValue(d.userCtx, prodContextKey, c)) |
234 }, ropts) | 237 }, ropts) |
235 } | 238 } |
236 | 239 |
237 func (d rdsImpl) Testable() ds.Testable { | 240 func (d rdsImpl) Testable() ds.Testable { |
238 return nil | 241 return nil |
239 } | 242 } |
OLD | NEW |