Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: impl/memory/context.go

Issue 1243323002: Refactor a bit. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: fix golint Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « impl/memory/README.md ('k') | impl/memory/doc.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 » "github.com/luci/gae" 11 » rds "github.com/luci/gae/service/rawdatastore"
12 "golang.org/x/net/context" 12 "golang.org/x/net/context"
13 ) 13 )
14 14
15 type memContextObj interface { 15 type memContextObj interface {
16 sync.Locker 16 sync.Locker
17 canApplyTxn(m memContextObj) bool 17 canApplyTxn(m memContextObj) bool
18 applyTxn(c context.Context, m memContextObj) 18 applyTxn(c context.Context, m memContextObj)
19 19
20 endTxn() 20 endTxn()
21 » mkTxn(*gae.DSTransactionOptions) memContextObj 21 » mkTxn(*rds.TransactionOptions) memContextObj
22 } 22 }
23 23
24 type memContext []memContextObj 24 type memContext []memContextObj
25 25
26 var _ = memContextObj((memContext)(nil)) 26 var _ = memContextObj((memContext)(nil))
27 27
28 func newMemContext() memContext { 28 func newMemContext() memContext {
29 return memContext{ 29 return memContext{
30 newTaskQueueData(), 30 newTaskQueueData(),
31 newDataStoreData(), 31 newDataStoreData(),
(...skipping 22 matching lines...) Expand all
54 m[i].Unlock() 54 m[i].Unlock()
55 } 55 }
56 } 56 }
57 57
58 func (m memContext) endTxn() { 58 func (m memContext) endTxn() {
59 for _, itm := range m { 59 for _, itm := range m {
60 itm.endTxn() 60 itm.endTxn()
61 } 61 }
62 } 62 }
63 63
64 func (m memContext) mkTxn(o *gae.DSTransactionOptions) memContextObj { 64 func (m memContext) mkTxn(o *rds.TransactionOptions) memContextObj {
65 ret := make(memContext, len(m)) 65 ret := make(memContext, len(m))
66 for i, itm := range m { 66 for i, itm := range m {
67 ret[i] = itm.mkTxn(o) 67 ret[i] = itm.mkTxn(o)
68 } 68 }
69 return ret 69 return ret
70 } 70 }
71 71
72 func (m memContext) canApplyTxn(txnCtxObj memContextObj) bool { 72 func (m memContext) canApplyTxn(txnCtxObj memContextObj) bool {
73 txnCtx := txnCtxObj.(memContext) 73 txnCtx := txnCtxObj.(memContext)
74 for i := range m { 74 for i := range m {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 // a couple ways, for example. 125 // a couple ways, for example.
126 // 126 //
127 // It really should have been appengine.Context.RunInTransaction(func(tc...)), 127 // It really should have been appengine.Context.RunInTransaction(func(tc...)),
128 // but because it's not, this method is on dsImpl instead to mirror the official 128 // but because it's not, this method is on dsImpl instead to mirror the official
129 // API. 129 // API.
130 // 130 //
131 // The fake implementation also differs from the real implementation because the 131 // The fake implementation also differs from the real implementation because the
132 // fake TaskQueue is NOT backed by the fake Datastore. This is done to make the 132 // fake TaskQueue is NOT backed by the fake Datastore. This is done to make the
133 // test-access API for TaskQueue better (instead of trying to reconstitute the 133 // test-access API for TaskQueue better (instead of trying to reconstitute the
134 // state of the task queue from a bunch of datastore accesses). 134 // state of the task queue from a bunch of datastore accesses).
135 func (d *dsImpl) RunInTransaction(f func(context.Context) error, o *gae.DSTransa ctionOptions) error { 135 func (d *dsImpl) RunInTransaction(f func(context.Context) error, o *rds.Transact ionOptions) error {
136 curMC := cur(d.c) 136 curMC := cur(d.c)
137 137
138 txnMC := curMC.mkTxn(o) 138 txnMC := curMC.mkTxn(o)
139 139
140 defer func() { 140 defer func() {
141 txnMC.Lock() 141 txnMC.Lock()
142 defer txnMC.Unlock() 142 defer txnMC.Unlock()
143 143
144 txnMC.endTxn() 144 txnMC.endTxn()
145 }() 145 }()
146 146
147 if err := f(context.WithValue(d.c, memContextKey, txnMC)); err != nil { 147 if err := f(context.WithValue(d.c, memContextKey, txnMC)); err != nil {
148 return err 148 return err
149 } 149 }
150 150
151 txnMC.Lock() 151 txnMC.Lock()
152 defer txnMC.Unlock() 152 defer txnMC.Unlock()
153 153
154 if curMC.canApplyTxn(txnMC) { 154 if curMC.canApplyTxn(txnMC) {
155 curMC.applyTxn(d.c, txnMC) 155 curMC.applyTxn(d.c, txnMC)
156 } else { 156 } else {
157 » » return gae.ErrDSConcurrentTransaction 157 » » return rds.ErrConcurrentTransaction
158 } 158 }
159 return nil 159 return nil
160 } 160 }
OLDNEW
« no previous file with comments | « impl/memory/README.md ('k') | impl/memory/doc.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698