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/gae/service/info" | 9 "github.com/luci/gae/service/info" |
10 "github.com/luci/luci-go/common/errors" | 10 "github.com/luci/luci-go/common/errors" |
11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
12 "google.golang.org/appengine/datastore" | 12 "google.golang.org/appengine/datastore" |
13 ) | 13 ) |
14 | 14 |
15 // useRDS adds a gae.RawDatastore implementation to context, accessible | 15 // useRDS adds a gae.RawDatastore implementation to context, accessible |
16 // by gae.GetDS(c) | 16 // by gae.GetDS(c) |
17 func useRDS(c context.Context) context.Context { | 17 func useRDS(c context.Context) context.Context { |
18 » return ds.SetRawFactory(c, func(ci context.Context) ds.RawInterface { | 18 » return ds.SetRawFactory(c, func(ci context.Context, wantTxn bool) ds.Raw
Interface { |
19 » » return rdsImpl{ci, AEContext(ci), info.Get(ci).GetNamespace()} | 19 » » ns := info.Get(ci).GetNamespace() |
| 20 » » maybeTxnCtx := AEContext(ci) |
| 21 |
| 22 » » if wantTxn { |
| 23 » » » return rdsImpl{ci, maybeTxnCtx, ns} |
| 24 » » } |
| 25 » » aeCtx := AEContextNoTX(ci) |
| 26 » » if maybeTxnCtx != aeCtx { |
| 27 » » » ci = context.WithValue(ci, prodContextKey, aeCtx) |
| 28 » » » var err error |
| 29 » » » ci, err = info.Get(ci).Namespace(ns) |
| 30 » » » if err != nil { |
| 31 » » » » panic(err) |
| 32 » » » } |
| 33 » » } |
| 34 » » return rdsImpl{ci, aeCtx, ns} |
20 }) | 35 }) |
21 } | 36 } |
22 | 37 |
23 ////////// Datastore | 38 ////////// Datastore |
24 | 39 |
25 type rdsImpl struct { | 40 type rdsImpl struct { |
26 // userCtx is the context that has the luci/gae services and user object
s in | 41 // userCtx is the context that has the luci/gae services and user object
s in |
27 // it. | 42 // it. |
28 userCtx context.Context | 43 userCtx context.Context |
29 | 44 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 q, err := d.fixQuery(fq) | 231 q, err := d.fixQuery(fq) |
217 if err != nil { | 232 if err != nil { |
218 return 0, err | 233 return 0, err |
219 } | 234 } |
220 ret, err := q.Count(d.aeCtx) | 235 ret, err := q.Count(d.aeCtx) |
221 return int64(ret), err | 236 return int64(ret), err |
222 } | 237 } |
223 | 238 |
224 func (d rdsImpl) RunInTransaction(f func(c context.Context) error, opts *ds.Tran
sactionOptions) error { | 239 func (d rdsImpl) RunInTransaction(f func(c context.Context) error, opts *ds.Tran
sactionOptions) error { |
225 ropts := (*datastore.TransactionOptions)(opts) | 240 ropts := (*datastore.TransactionOptions)(opts) |
226 » return datastore.RunInTransaction(d.aeCtx, func(aeCtx context.Context) e
rror { | 241 » return datastore.RunInTransaction(d.aeCtx, func(c context.Context) error
{ |
227 » » return f(context.WithValue(d.userCtx, prodContextKey, aeCtx)) | 242 » » return f(context.WithValue(d.userCtx, prodContextKey, c)) |
228 }, ropts) | 243 }, ropts) |
229 } | 244 } |
230 | 245 |
231 func (d rdsImpl) Testable() ds.Testable { | 246 func (d rdsImpl) Testable() ds.Testable { |
232 return nil | 247 return nil |
233 } | 248 } |
OLD | NEW |