Chromium Code Reviews| Index: service/datastore/context.go |
| diff --git a/service/datastore/context.go b/service/datastore/context.go |
| index 717797bb3eb94694fad9b80dfda7efb8a1c4d889..339b54c02614c84edd4dd94c16359d922423d99f 100644 |
| --- a/service/datastore/context.go |
| +++ b/service/datastore/context.go |
| @@ -6,6 +6,7 @@ package datastore |
| import ( |
| "github.com/luci/gae/service/info" |
| + |
| "golang.org/x/net/context" |
| ) |
| @@ -17,10 +18,8 @@ var ( |
| ) |
| // RawFactory is the function signature for factory methods compatible with |
| -// SetRawFactory. wantTxn is true if the Factory should return the datastore in |
| -// the current transaction, and false if the Factory should return the |
| -// non-transactional (root) datastore. |
| -type RawFactory func(c context.Context, wantTxn bool) RawInterface |
| +// SetRawFactory. |
| +type RawFactory func(c context.Context) RawInterface |
| // RawFilter is the function signature for a RawFilter implementation. It |
| // gets the current RDS implementation, and returns a new RDS implementation |
| @@ -29,17 +28,17 @@ type RawFilter func(context.Context, RawInterface) RawInterface |
| // getUnfiltered gets gets the RawInterface implementation from context without |
| // any of the filters applied. |
| -func getUnfiltered(c context.Context, wantTxn bool) RawInterface { |
| +func getUnfiltered(c context.Context) RawInterface { |
| if f, ok := c.Value(rawDatastoreKey).(RawFactory); ok && f != nil { |
| - return f(c, wantTxn) |
| + return f(c) |
| } |
| return nil |
| } |
| // getFiltered gets the datastore (transactional or not), and applies all of |
| // the currently installed filters to it. |
| -func getFiltered(c context.Context, wantTxn bool) RawInterface { |
| - ret := getUnfiltered(c, wantTxn) |
| +func getFiltered(c context.Context) RawInterface { |
| + ret := getUnfiltered(c) |
| if ret == nil { |
| return nil |
| } |
| @@ -49,45 +48,13 @@ func getFiltered(c context.Context, wantTxn bool) RawInterface { |
| return applyCheckFilter(c, ret) |
| } |
| -// GetRaw gets the RawInterface implementation from context. |
| -func GetRaw(c context.Context) RawInterface { |
| - return getFiltered(c, true) |
| -} |
| - |
| -// GetRawNoTxn gets the RawInterface implementation from context. If there's a |
| -// currently active transaction, this will return a non-transactional connection |
| -// to the datastore, otherwise this is the same as GetRaw. |
| -func GetRawNoTxn(c context.Context) RawInterface { |
| - return getFiltered(c, false) |
| -} |
| - |
| -// Get gets the Interface implementation from context. |
| -func Get(c context.Context) Interface { |
| - inf := info.Get(c) |
| - ns, _ := inf.GetNamespace() |
| - return &datastoreImpl{ |
| - GetRaw(c), |
| - inf.FullyQualifiedAppID(), |
| - ns, |
| - } |
| -} |
| - |
| -// GetNoTxn gets the Interface implementation from context. If there's a |
| -// currently active transaction, this will return a non-transactional connection |
| -// to the datastore, otherwise this is the same as GetRaw. |
| -// Get gets the Interface implementation from context. |
| -func GetNoTxn(c context.Context) Interface { |
| - inf := info.Get(c) |
| - ns, _ := inf.GetNamespace() |
| - return &datastoreImpl{ |
| - GetRawNoTxn(c), |
| - inf.FullyQualifiedAppID(), |
| - ns, |
| - } |
| +// Raw gets the RawInterface implementation from context. |
| +func Raw(c context.Context) RawInterface { |
| + return getFiltered(c) |
| } |
| // SetRawFactory sets the function to produce Datastore instances, as returned by |
| -// the GetRaw method. |
| +// the Raw method. |
| func SetRawFactory(c context.Context, rdsf RawFactory) context.Context { |
| return context.WithValue(c, rawDatastoreKey, rdsf) |
| } |
| @@ -96,7 +63,7 @@ func SetRawFactory(c context.Context, rdsf RawFactory) context.Context { |
| // with a quick mock. This is just a shorthand SetRawFactory invocation to set |
| // a factory which always returns the same object. |
| func SetRaw(c context.Context, rds RawInterface) context.Context { |
| - return SetRawFactory(c, func(context.Context, bool) RawInterface { return rds }) |
| + return SetRawFactory(c, func(context.Context) RawInterface { return rds }) |
| } |
| func getCurFilters(c context.Context) []RawFilter { |
| @@ -118,3 +85,9 @@ func AddRawFilters(c context.Context, filts ...RawFilter) context.Context { |
| newFilts = append(newFilts, filts...) |
| return context.WithValue(c, rawDatastoreFilterKey, newFilts) |
| } |
| + |
| +// GetKeyContext returns the KeyContext whose AppID and Namespace match those |
| +// installed in the supplied Context. |
| +func GetKeyContext(c context.Context) KeyContext { |
| + return KeyContext{info.FullyQualifiedAppID(c), info.GetNamespace(c)} |
|
dnj
2016/09/01 15:25:40
I'll replace this with a "RawInterface" Get so we
iannucci
2016/09/16 01:01:13
sgtm
dnj
2016/09/16 05:44:43
Done.
|
| +} |