OLD | NEW |
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 datastore | 5 package datastore |
6 | 6 |
7 import ( | 7 import ( |
8 "github.com/luci/gae/service/info" | 8 "github.com/luci/gae/service/info" |
| 9 |
9 "golang.org/x/net/context" | 10 "golang.org/x/net/context" |
10 ) | 11 ) |
11 | 12 |
12 type key int | 13 type key int |
13 | 14 |
14 var ( | 15 var ( |
15 rawDatastoreKey key | 16 rawDatastoreKey key |
16 rawDatastoreFilterKey key = 1 | 17 rawDatastoreFilterKey key = 1 |
17 ) | 18 ) |
18 | 19 |
19 // RawFactory is the function signature for factory methods compatible with | 20 // RawFactory is the function signature for factory methods compatible with |
20 // SetRawFactory. wantTxn is true if the Factory should return the datastore in | 21 // SetRawFactory. |
21 // the current transaction, and false if the Factory should return the | 22 type RawFactory func(c context.Context) RawInterface |
22 // non-transactional (root) datastore. | |
23 type RawFactory func(c context.Context, wantTxn bool) RawInterface | |
24 | 23 |
25 // RawFilter is the function signature for a RawFilter implementation. It | 24 // RawFilter is the function signature for a RawFilter implementation. It |
26 // gets the current RDS implementation, and returns a new RDS implementation | 25 // gets the current RDS implementation, and returns a new RDS implementation |
27 // backed by the one passed in. | 26 // backed by the one passed in. |
28 type RawFilter func(context.Context, RawInterface) RawInterface | 27 type RawFilter func(context.Context, RawInterface) RawInterface |
29 | 28 |
30 // getUnfiltered gets gets the RawInterface implementation from context without | 29 // getUnfiltered gets gets the RawInterface implementation from context without |
31 // any of the filters applied. | 30 // any of the filters applied. |
32 func getUnfiltered(c context.Context, wantTxn bool) RawInterface { | 31 func getUnfiltered(c context.Context) RawInterface { |
33 if f, ok := c.Value(rawDatastoreKey).(RawFactory); ok && f != nil { | 32 if f, ok := c.Value(rawDatastoreKey).(RawFactory); ok && f != nil { |
34 » » return f(c, wantTxn) | 33 » » return f(c) |
35 } | 34 } |
36 return nil | 35 return nil |
37 } | 36 } |
38 | 37 |
39 // getFiltered gets the datastore (transactional or not), and applies all of | 38 // getFiltered gets the datastore (transactional or not), and applies all of |
40 // the currently installed filters to it. | 39 // the currently installed filters to it. |
41 func getFiltered(c context.Context, wantTxn bool) RawInterface { | 40 func getFiltered(c context.Context) RawInterface { |
42 » ret := getUnfiltered(c, wantTxn) | 41 » ret := getUnfiltered(c) |
43 if ret == nil { | 42 if ret == nil { |
44 return nil | 43 return nil |
45 } | 44 } |
46 for _, f := range getCurFilters(c) { | 45 for _, f := range getCurFilters(c) { |
47 ret = f(c, ret) | 46 ret = f(c, ret) |
48 } | 47 } |
49 return applyCheckFilter(c, ret) | 48 return applyCheckFilter(c, ret) |
50 } | 49 } |
51 | 50 |
52 // GetRaw gets the RawInterface implementation from context. | 51 // Raw gets the RawInterface implementation from context. |
53 func GetRaw(c context.Context) RawInterface { | 52 func Raw(c context.Context) RawInterface { |
54 » return getFiltered(c, true) | 53 » return getFiltered(c) |
55 } | |
56 | |
57 // GetRawNoTxn gets the RawInterface implementation from context. If there's a | |
58 // currently active transaction, this will return a non-transactional connection | |
59 // to the datastore, otherwise this is the same as GetRaw. | |
60 func GetRawNoTxn(c context.Context) RawInterface { | |
61 » return getFiltered(c, false) | |
62 } | |
63 | |
64 // Get gets the Interface implementation from context. | |
65 func Get(c context.Context) Interface { | |
66 » inf := info.Get(c) | |
67 » ns, _ := inf.GetNamespace() | |
68 » return &datastoreImpl{ | |
69 » » GetRaw(c), | |
70 » » inf.FullyQualifiedAppID(), | |
71 » » ns, | |
72 » } | |
73 } | |
74 | |
75 // GetNoTxn gets the Interface implementation from context. If there's a | |
76 // currently active transaction, this will return a non-transactional connection | |
77 // to the datastore, otherwise this is the same as GetRaw. | |
78 // Get gets the Interface implementation from context. | |
79 func GetNoTxn(c context.Context) Interface { | |
80 » inf := info.Get(c) | |
81 » ns, _ := inf.GetNamespace() | |
82 » return &datastoreImpl{ | |
83 » » GetRawNoTxn(c), | |
84 » » inf.FullyQualifiedAppID(), | |
85 » » ns, | |
86 » } | |
87 } | 54 } |
88 | 55 |
89 // SetRawFactory sets the function to produce Datastore instances, as returned b
y | 56 // SetRawFactory sets the function to produce Datastore instances, as returned b
y |
90 // the GetRaw method. | 57 // the Raw method. |
91 func SetRawFactory(c context.Context, rdsf RawFactory) context.Context { | 58 func SetRawFactory(c context.Context, rdsf RawFactory) context.Context { |
92 return context.WithValue(c, rawDatastoreKey, rdsf) | 59 return context.WithValue(c, rawDatastoreKey, rdsf) |
93 } | 60 } |
94 | 61 |
95 // SetRaw sets the current Datastore object in the context. Useful for testing | 62 // SetRaw sets the current Datastore object in the context. Useful for testing |
96 // with a quick mock. This is just a shorthand SetRawFactory invocation to set | 63 // with a quick mock. This is just a shorthand SetRawFactory invocation to set |
97 // a factory which always returns the same object. | 64 // a factory which always returns the same object. |
98 func SetRaw(c context.Context, rds RawInterface) context.Context { | 65 func SetRaw(c context.Context, rds RawInterface) context.Context { |
99 » return SetRawFactory(c, func(context.Context, bool) RawInterface { retur
n rds }) | 66 » return SetRawFactory(c, func(context.Context) RawInterface { return rds
}) |
100 } | 67 } |
101 | 68 |
102 func getCurFilters(c context.Context) []RawFilter { | 69 func getCurFilters(c context.Context) []RawFilter { |
103 curFiltsI := c.Value(rawDatastoreFilterKey) | 70 curFiltsI := c.Value(rawDatastoreFilterKey) |
104 if curFiltsI != nil { | 71 if curFiltsI != nil { |
105 return curFiltsI.([]RawFilter) | 72 return curFiltsI.([]RawFilter) |
106 } | 73 } |
107 return nil | 74 return nil |
108 } | 75 } |
109 | 76 |
110 // AddRawFilters adds RawInterface filters to the context. | 77 // AddRawFilters adds RawInterface filters to the context. |
111 func AddRawFilters(c context.Context, filts ...RawFilter) context.Context { | 78 func AddRawFilters(c context.Context, filts ...RawFilter) context.Context { |
112 if len(filts) == 0 { | 79 if len(filts) == 0 { |
113 return c | 80 return c |
114 } | 81 } |
115 cur := getCurFilters(c) | 82 cur := getCurFilters(c) |
116 newFilts := make([]RawFilter, 0, len(cur)+len(filts)) | 83 newFilts := make([]RawFilter, 0, len(cur)+len(filts)) |
117 newFilts = append(newFilts, getCurFilters(c)...) | 84 newFilts = append(newFilts, getCurFilters(c)...) |
118 newFilts = append(newFilts, filts...) | 85 newFilts = append(newFilts, filts...) |
119 return context.WithValue(c, rawDatastoreFilterKey, newFilts) | 86 return context.WithValue(c, rawDatastoreFilterKey, newFilts) |
120 } | 87 } |
| 88 |
| 89 // GetKeyContext returns the KeyContext whose AppID and Namespace match those |
| 90 // installed in the supplied Context. |
| 91 func GetKeyContext(c context.Context) KeyContext { |
| 92 ri := info.Raw(c) |
| 93 return KeyContext{ri.FullyQualifiedAppID(), ri.GetNamespace()} |
| 94 } |
OLD | NEW |