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 memory | 5 package memory |
6 | 6 |
7 import ( | 7 import ( |
| 8 "errors" |
8 "infra/gae/libs/wrapper" | 9 "infra/gae/libs/wrapper" |
9 "math/rand" | 10 "math/rand" |
10 "sync" | 11 "sync" |
11 | 12 |
12 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
13 | 14 |
14 "appengine/datastore" | 15 "appengine/datastore" |
15 ) | 16 ) |
16 | 17 |
17 type memContextObj interface { | 18 type memContextObj interface { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 return true | 86 return true |
86 } | 87 } |
87 | 88 |
88 func (m memContext) applyTxn(rnd *rand.Rand, txnCtxObj memContextObj) { | 89 func (m memContext) applyTxn(rnd *rand.Rand, txnCtxObj memContextObj) { |
89 txnCtx := txnCtxObj.(memContext) | 90 txnCtx := txnCtxObj.(memContext) |
90 for i := range m { | 91 for i := range m { |
91 m[i].applyTxn(rnd, txnCtx[i]) | 92 m[i].applyTxn(rnd, txnCtx[i]) |
92 } | 93 } |
93 } | 94 } |
94 | 95 |
95 // Enable adds a new memory context to c. This new memory context will have | 96 // Use adds implementations for the following gae/wrapper interfaces to the |
96 // a zeroed state. | 97 // context: |
97 func Enable(c context.Context) context.Context { | 98 // * wrapper.Datastore |
98 » return context.WithValue( | 99 // * wrapper.TaskQueue |
| 100 // * wrapper.Memcache |
| 101 // * wrapper.GlobalInfo |
| 102 // |
| 103 // These can be retrieved with the "gae/wrapper".Get functions. |
| 104 // |
| 105 // The implementations are all backed by an in-memory implementation, and start |
| 106 // with an empty state. |
| 107 // |
| 108 // Using this more than once per context.Context will cause a panic. |
| 109 func Use(c context.Context) context.Context { |
| 110 » if c.Value(memContextKey) != nil { |
| 111 » » panic(errors.New("memory.Use: called twice on the same Context")
) |
| 112 » } |
| 113 » c = context.WithValue( |
99 context.WithValue(c, memContextKey, newMemContext()), | 114 context.WithValue(c, memContextKey, newMemContext()), |
100 giContextKey, &globalInfoData{}) | 115 giContextKey, &globalInfoData{}) |
101 } | 116 » return useTQ(useDS(useMC(useGI(c)))) |
102 | |
103 // Use calls ALL of this packages Use* methods on c. This enables all | |
104 // gae/wrapper Get* api's. | |
105 func Use(c context.Context) context.Context { | |
106 » return UseTQ(UseDS(UseMC(UseGI(c)))) | |
107 } | 117 } |
108 | 118 |
109 func cur(c context.Context) (p memContext) { | 119 func cur(c context.Context) (p memContext) { |
110 p, _ = c.Value(memContextKey).(memContext) | 120 p, _ = c.Value(memContextKey).(memContext) |
111 return | 121 return |
112 } | 122 } |
113 | 123 |
114 type memContextKeyType int | 124 type memContextKeyType int |
115 | 125 |
116 var memContextKey memContextKeyType | 126 var memContextKey memContextKeyType |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 txnMC.Lock() | 161 txnMC.Lock() |
152 defer txnMC.Unlock() | 162 defer txnMC.Unlock() |
153 | 163 |
154 if curMC.canApplyTxn(txnMC) { | 164 if curMC.canApplyTxn(txnMC) { |
155 curMC.applyTxn(wrapper.GetMathRand(d.c), txnMC) | 165 curMC.applyTxn(wrapper.GetMathRand(d.c), txnMC) |
156 } else { | 166 } else { |
157 return datastore.ErrConcurrentTransaction | 167 return datastore.ErrConcurrentTransaction |
158 } | 168 } |
159 return nil | 169 return nil |
160 } | 170 } |
OLD | NEW |