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 "fmt" | 9 "fmt" |
10 | 10 |
11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
12 | 12 |
13 ds "github.com/luci/gae/service/datastore" | 13 ds "github.com/luci/gae/service/datastore" |
14 "github.com/luci/gae/service/datastore/dskey" | 14 "github.com/luci/gae/service/datastore/dskey" |
15 ) | 15 ) |
16 | 16 |
17 //////////////////////////////////// public //////////////////////////////////// | 17 //////////////////////////////////// public //////////////////////////////////// |
18 | 18 |
19 // useRDS adds a gae.Datastore implementation to context, accessible | 19 // useRDS adds a gae.Datastore implementation to context, accessible |
20 // by gae.GetDS(c) | 20 // by gae.GetDS(c) |
21 func useRDS(c context.Context) context.Context { | 21 func useRDS(c context.Context) context.Context { |
22 return ds.SetRawFactory(c, func(ic context.Context) ds.RawInterface { | 22 return ds.SetRawFactory(c, func(ic context.Context) ds.RawInterface { |
23 dsd := cur(ic).Get(memContextDSIdx) | 23 dsd := cur(ic).Get(memContextDSIdx) |
24 | 24 |
25 ns := curGID(ic).namespace | 25 ns := curGID(ic).namespace |
26 if x, ok := dsd.(*dataStoreData); ok { | 26 if x, ok := dsd.(*dataStoreData); ok { |
27 » » » return &dsImpl{x, ns, 0, ic} | 27 » » » return &dsImpl{x, ns, ic} |
28 } | 28 } |
29 return &txnDsImpl{dsd.(*txnDataStoreData), ns} | 29 return &txnDsImpl{dsd.(*txnDataStoreData), ns} |
30 }) | 30 }) |
31 } | 31 } |
32 | 32 |
33 //////////////////////////////////// dsImpl //////////////////////////////////// | 33 //////////////////////////////////// dsImpl //////////////////////////////////// |
34 | 34 |
35 // dsImpl exists solely to bind the current c to the datastore data. | 35 // dsImpl exists solely to bind the current c to the datastore data. |
36 type dsImpl struct { | 36 type dsImpl struct { |
37 » data *dataStoreData | 37 » data *dataStoreData |
38 » ns string | 38 » ns string |
39 » txnFakeRetry int | 39 » c context.Context |
40 » c context.Context | |
41 } | 40 } |
42 | 41 |
43 var _ ds.RawInterface = (*dsImpl)(nil) | 42 var _ ds.RawInterface = (*dsImpl)(nil) |
44 | 43 |
45 func (d *dsImpl) DecodeKey(encoded string) (ds.Key, error) { | 44 func (d *dsImpl) DecodeKey(encoded string) (ds.Key, error) { |
46 return dskey.NewFromEncoded(encoded) | 45 return dskey.NewFromEncoded(encoded) |
47 } | 46 } |
48 | 47 |
49 func (d *dsImpl) NewKey(kind, stringID string, intID int64, parent ds.Key) ds.Ke
y { | 48 func (d *dsImpl) NewKey(kind, stringID string, intID int64, parent ds.Key) ds.Ke
y { |
50 return dskey.New(globalAppID, d.ns, kind, stringID, intID, parent) | 49 return dskey.New(globalAppID, d.ns, kind, stringID, intID, parent) |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 101 |
103 func (d *dsImpl) SetIndexSnapshot(snap ds.TestingSnapshot) { | 102 func (d *dsImpl) SetIndexSnapshot(snap ds.TestingSnapshot) { |
104 d.data.setSnapshot(snap.(*memStore)) | 103 d.data.setSnapshot(snap.(*memStore)) |
105 } | 104 } |
106 | 105 |
107 func (d *dsImpl) CatchupIndexes() { | 106 func (d *dsImpl) CatchupIndexes() { |
108 d.data.catchupIndexes() | 107 d.data.catchupIndexes() |
109 } | 108 } |
110 | 109 |
111 func (d *dsImpl) SetTransactionRetryCount(count int) { | 110 func (d *dsImpl) SetTransactionRetryCount(count int) { |
112 » d.txnFakeRetry = count | 111 » d.data.txnFakeRetry = count |
113 } | 112 } |
114 | 113 |
115 func (d *dsImpl) Testable() ds.Testable { | 114 func (d *dsImpl) Testable() ds.Testable { |
116 return d | 115 return d |
117 } | 116 } |
118 | 117 |
119 ////////////////////////////////// txnDsImpl /////////////////////////////////// | 118 ////////////////////////////////// txnDsImpl /////////////////////////////////// |
120 | 119 |
121 type txnDsImpl struct { | 120 type txnDsImpl struct { |
122 data *txnDataStoreData | 121 data *txnDataStoreData |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 return errors.New("datastore: nested transactions are not supported") | 163 return errors.New("datastore: nested transactions are not supported") |
165 } | 164 } |
166 | 165 |
167 func (d *txnDsImpl) NewQuery(kind string) ds.Query { | 166 func (d *txnDsImpl) NewQuery(kind string) ds.Query { |
168 return &queryImpl{ns: d.ns, kind: kind} | 167 return &queryImpl{ns: d.ns, kind: kind} |
169 } | 168 } |
170 | 169 |
171 func (*txnDsImpl) Testable() ds.Testable { | 170 func (*txnDsImpl) Testable() ds.Testable { |
172 return nil | 171 return nil |
173 } | 172 } |
OLD | NEW |