Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package memory | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 "math/rand" | |
| 10 "sync" | |
| 11 | |
| 12 "golang.org/x/net/context" | |
| 13 | |
| 14 "appengine/datastore" | |
| 15 | |
| 16 "infra/gae/libs/wrapper" | |
| 17 ) | |
| 18 | |
| 19 type memContextObj interface { | |
| 20 sync.Locker | |
| 21 canApplyTxn(m memContextObj) bool | |
| 22 applyTxn(rnd *rand.Rand, m memContextObj) | |
| 23 | |
| 24 endTxn() | |
| 25 mkTxn(*datastore.TransactionOptions) (memContextObj, error) | |
| 26 } | |
| 27 | |
| 28 type memContext []memContextObj | |
|
Vadim Sh.
2015/05/24 19:43:26
memContext implements memContextObj too?
iannucci
2015/05/24 20:33:54
yeah it does (plus some extra stuff)
| |
| 29 | |
| 30 func newMemContext() memContext { | |
| 31 return memContext{ | |
| 32 newTaskQueueData(), | |
| 33 newDataStoreData(), | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 var memContextIndices = map[string]int{ | |
| 38 "TQ": 0, | |
| 39 "DS": 1, | |
| 40 } | |
| 41 | |
| 42 func (m memContext) Get(item string) memContextObj { | |
| 43 if i, ok := memContextIndices[item]; !ok { | |
| 44 panic(fmt.Errorf("wrapper/memory: cannot get context item %q", i tem)) | |
| 45 } else { | |
| 46 return m[i] | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 func (m memContext) Lock() { | |
| 51 for _, itm := range m { | |
| 52 itm.Lock() | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 func (m memContext) Unlock() { | |
| 57 for i := len(m) - 1; i >= 0; i-- { | |
| 58 m[i].Unlock() | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 func (m memContext) endTxn() { | |
| 63 for _, itm := range m { | |
| 64 itm.endTxn() | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 func (m memContext) mkTxn(o *datastore.TransactionOptions) (ret memContext, err error) { | |
| 69 for _, itm := range m { | |
| 70 newItm, err := itm.mkTxn(o) | |
| 71 if err != nil { | |
| 72 return nil, err | |
| 73 } | |
| 74 ret = append(ret, newItm) | |
| 75 } | |
| 76 return ret, nil | |
| 77 } | |
| 78 | |
| 79 func (m memContext) canApplyTxn(txnCtx memContext) bool { | |
| 80 for i := range m { | |
| 81 if !m[i].canApplyTxn(txnCtx[i]) { | |
| 82 return false | |
| 83 } | |
| 84 } | |
| 85 return true | |
| 86 } | |
| 87 | |
| 88 func (m memContext) applyTxn(rnd *rand.Rand, txnCtx memContext) { | |
| 89 for i := range m { | |
| 90 m[i].applyTxn(rnd, txnCtx[i]) | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 // Enable adds a new memory context to c. This new memory context will have | |
| 95 // a zeroed state. | |
| 96 func Enable(c context.Context) context.Context { | |
| 97 return context.WithValue( | |
| 98 context.WithValue(c, memContextKey, newMemContext()), | |
| 99 giContextKey, newGlobalInfoData()) | |
| 100 } | |
| 101 | |
| 102 // Use calls ALL of this packages Use* methods on c. This enables all | |
| 103 // wrapper/memory Get* api's. | |
| 104 func Use(c context.Context) context.Context { | |
| 105 return UseTQ(UseDS(UseMC(UseGI(c)))) | |
| 106 } | |
| 107 | |
| 108 func cur(c context.Context) (p memContext) { | |
| 109 p, _ = c.Value(memContextKey).(memContext) | |
| 110 return | |
| 111 } | |
| 112 | |
| 113 type memContextKeyType int | |
| 114 | |
| 115 var memContextKey memContextKeyType | |
| 116 | |
| 117 // weird stuff | |
| 118 | |
| 119 // RunInTransaction is here because it's really a service-wide transaction, not just in | |
| 120 // the datastore. TaskQueue behaves differently in a transaction in a couple way s, for | |
| 121 // example. | |
| 122 // | |
| 123 // It really should have been appengine.Context.RunInTransaction(func(tc...)), b ut | |
| 124 // because it's not, this method is on dsImpl instead to mirror the official API . | |
| 125 // | |
| 126 // The fake implementation also differs from the real implementation because | |
| 127 // the fake TaskQueue is NOT backed by the fake Datastore. This is done to make | |
| 128 // the test-access API for TaskQueue better (instead of trying to reconstitute | |
| 129 // the state of the task queue from a bunch of datastore accesses). | |
| 130 func (d *dsImpl) RunInTransaction(f func(context.Context) error, o *datastore.Tr ansactionOptions) (err error) { | |
| 131 curMC := cur(d.c) | |
| 132 | |
| 133 txnMC, err := curMC.mkTxn(o) | |
| 134 if err != nil { | |
| 135 return err | |
| 136 } | |
| 137 | |
| 138 defer func() { | |
| 139 txnMC.Lock() | |
| 140 defer txnMC.Unlock() | |
| 141 | |
| 142 txnMC.endTxn() | |
| 143 }() | |
| 144 | |
| 145 err = f(context.WithValue(d.c, memContextKey, txnMC)) | |
| 146 if err != nil { | |
| 147 return err | |
| 148 } | |
| 149 | |
| 150 return func() error { | |
| 151 txnMC.Lock() | |
| 152 defer txnMC.Unlock() | |
| 153 | |
| 154 if curMC.canApplyTxn(txnMC) { | |
| 155 curMC.applyTxn(wrapper.GetMathRand(d.c), txnMC) | |
| 156 } else { | |
| 157 return datastore.ErrConcurrentTransaction | |
| 158 } | |
| 159 return nil | |
| 160 }() | |
| 161 } | |
| OLD | NEW |