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