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 "errors" |
9 "sync" | 9 "sync" |
10 | 10 |
11 ds "github.com/luci/gae/service/datastore" | 11 ds "github.com/luci/gae/service/datastore" |
12 "golang.org/x/net/context" | 12 "golang.org/x/net/context" |
13 ) | 13 ) |
14 | 14 |
15 var serializationDeterministic = false | 15 var serializationDeterministic = false |
16 | 16 |
17 type memContextObj interface { | 17 type memContextObj interface { |
18 sync.Locker | 18 sync.Locker |
19 canApplyTxn(m memContextObj) bool | 19 canApplyTxn(m memContextObj) bool |
20 applyTxn(c context.Context, m memContextObj) | 20 applyTxn(c context.Context, m memContextObj) |
21 | 21 |
22 endTxn() | 22 endTxn() |
23 mkTxn(*ds.TransactionOptions) memContextObj | 23 mkTxn(*ds.TransactionOptions) memContextObj |
24 } | 24 } |
25 | 25 |
26 type memContext []memContextObj | 26 type memContext []memContextObj |
27 | 27 |
28 var _ = memContextObj((memContext)(nil)) | 28 var _ = memContextObj((memContext)(nil)) |
29 | 29 |
30 func newMemContext() memContext { | 30 func newMemContext(aid string) memContext { |
31 return memContext{ | 31 return memContext{ |
32 newTaskQueueData(), | 32 newTaskQueueData(), |
33 » » newDataStoreData(), | 33 » » newDataStoreData(aid), |
34 } | 34 } |
35 } | 35 } |
36 | 36 |
37 type memContextIdx int | 37 type memContextIdx int |
38 | 38 |
39 const ( | 39 const ( |
40 memContextTQIdx memContextIdx = iota | 40 memContextTQIdx memContextIdx = iota |
41 memContextDSIdx | 41 memContextDSIdx |
42 ) | 42 ) |
43 | 43 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 return true | 81 return true |
82 } | 82 } |
83 | 83 |
84 func (m memContext) applyTxn(c context.Context, txnCtxObj memContextObj) { | 84 func (m memContext) applyTxn(c context.Context, txnCtxObj memContextObj) { |
85 txnCtx := txnCtxObj.(memContext) | 85 txnCtx := txnCtxObj.(memContext) |
86 for i := range m { | 86 for i := range m { |
87 m[i].applyTxn(c, txnCtx[i]) | 87 m[i].applyTxn(c, txnCtx[i]) |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 // Use adds implementations for the following gae interfaces to the | 91 // Use calls UseWithAppID with the appid of "dev~app" |
| 92 func Use(c context.Context) context.Context { |
| 93 » return UseWithAppID(c, "dev~app") |
| 94 } |
| 95 |
| 96 // UseWithAppID adds implementations for the following gae interfaces to the |
92 // context: | 97 // context: |
93 // * gae.Datastore | 98 // * gae.Datastore |
94 // * gae.TaskQueue | 99 // * gae.TaskQueue |
95 // * gae.Memcache | 100 // * gae.Memcache |
96 // * gae.GlobalInfo | 101 // * gae.GlobalInfo |
97 // | 102 // |
| 103 // The application id wil be set to 'aid', and will not be modifiable in this |
| 104 // context. |
| 105 // |
98 // These can be retrieved with the gae.Get functions. | 106 // These can be retrieved with the gae.Get functions. |
99 // | 107 // |
100 // The implementations are all backed by an in-memory implementation, and start | 108 // The implementations are all backed by an in-memory implementation, and start |
101 // with an empty state. | 109 // with an empty state. |
102 // | 110 // |
103 // Using this more than once per context.Context will cause a panic. | 111 // Using this more than once per context.Context will cause a panic. |
104 func Use(c context.Context) context.Context { | 112 func UseWithAppID(c context.Context, aid string) context.Context { |
105 if c.Value(memContextKey) != nil { | 113 if c.Value(memContextKey) != nil { |
106 panic(errors.New("memory.Use: called twice on the same Context")
) | 114 panic(errors.New("memory.Use: called twice on the same Context")
) |
107 } | 115 } |
108 c = context.WithValue( | 116 c = context.WithValue( |
109 » » context.WithValue(c, memContextKey, newMemContext()), | 117 » » context.WithValue(c, memContextKey, newMemContext(aid)), |
110 » » giContextKey, &globalInfoData{}) | 118 » » giContextKey, &globalInfoData{appid: aid}) |
111 » return useTQ(useRDS(useMC(useGI(c)))) | 119 » return useTQ(useRDS(useMC(useGI(c, aid)))) |
112 } | 120 } |
113 | 121 |
114 func cur(c context.Context) (p memContext) { | 122 func cur(c context.Context) (p memContext) { |
115 p, _ = c.Value(memContextKey).(memContext) | 123 p, _ = c.Value(memContextKey).(memContext) |
116 return | 124 return |
117 } | 125 } |
118 | 126 |
119 type memContextKeyType int | 127 type memContextKeyType int |
120 | 128 |
121 var memContextKey memContextKeyType | 129 var memContextKey memContextKeyType |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 if o != nil && o.Attempts != 0 { | 180 if o != nil && o.Attempts != 0 { |
173 attempts = o.Attempts | 181 attempts = o.Attempts |
174 } | 182 } |
175 for attempt := 0; attempt < attempts; attempt++ { | 183 for attempt := 0; attempt < attempts; attempt++ { |
176 if err := loopBody(attempt >= d.data.txnFakeRetry); err != ds.Er
rConcurrentTransaction { | 184 if err := loopBody(attempt >= d.data.txnFakeRetry); err != ds.Er
rConcurrentTransaction { |
177 return err | 185 return err |
178 } | 186 } |
179 } | 187 } |
180 return ds.ErrConcurrentTransaction | 188 return ds.ErrConcurrentTransaction |
181 } | 189 } |
OLD | NEW |