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" | |
15 ) | 14 ) |
16 | 15 |
17 //////////////////////////////////// public //////////////////////////////////// | 16 //////////////////////////////////// public //////////////////////////////////// |
18 | 17 |
19 // useRDS adds a gae.Datastore implementation to context, accessible | 18 // useRDS adds a gae.Datastore implementation to context, accessible |
20 // by gae.GetDS(c) | 19 // by gae.GetDS(c) |
21 func useRDS(c context.Context) context.Context { | 20 func useRDS(c context.Context) context.Context { |
22 return ds.SetRawFactory(c, func(ic context.Context) ds.RawInterface { | 21 return ds.SetRawFactory(c, func(ic context.Context) ds.RawInterface { |
23 dsd := cur(ic).Get(memContextDSIdx) | 22 dsd := cur(ic).Get(memContextDSIdx) |
24 | 23 |
25 ns := curGID(ic).namespace | 24 ns := curGID(ic).namespace |
26 if x, ok := dsd.(*dataStoreData); ok { | 25 if x, ok := dsd.(*dataStoreData); ok { |
27 return &dsImpl{x, ns, ic} | 26 return &dsImpl{x, ns, ic} |
28 } | 27 } |
29 return &txnDsImpl{dsd.(*txnDataStoreData), ns} | 28 return &txnDsImpl{dsd.(*txnDataStoreData), ns} |
30 }) | 29 }) |
31 } | 30 } |
32 | 31 |
33 //////////////////////////////////// dsImpl //////////////////////////////////// | 32 //////////////////////////////////// dsImpl //////////////////////////////////// |
34 | 33 |
35 // dsImpl exists solely to bind the current c to the datastore data. | 34 // dsImpl exists solely to bind the current c to the datastore data. |
36 type dsImpl struct { | 35 type dsImpl struct { |
37 data *dataStoreData | 36 data *dataStoreData |
38 ns string | 37 ns string |
39 c context.Context | 38 c context.Context |
40 } | 39 } |
41 | 40 |
42 var _ ds.RawInterface = (*dsImpl)(nil) | 41 var _ ds.RawInterface = (*dsImpl)(nil) |
43 | 42 |
44 func (d *dsImpl) DecodeKey(encoded string) (ds.Key, error) { | 43 func (d *dsImpl) PutMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.PutMultiC
B) error { |
45 » return dskey.NewFromEncoded(encoded) | |
46 } | |
47 | |
48 func (d *dsImpl) NewKey(kind, stringID string, intID int64, parent ds.Key) ds.Ke
y { | |
49 » return dskey.New(globalAppID, d.ns, kind, stringID, intID, parent) | |
50 } | |
51 | |
52 func (d *dsImpl) PutMulti(keys []ds.Key, vals []ds.PropertyMap, cb ds.PutMultiCB
) error { | |
53 d.data.putMulti(keys, vals, cb) | 44 d.data.putMulti(keys, vals, cb) |
54 return nil | 45 return nil |
55 } | 46 } |
56 | 47 |
57 func (d *dsImpl) GetMulti(keys []ds.Key, _meta ds.MultiMetaGetter, cb ds.GetMult
iCB) error { | 48 func (d *dsImpl) GetMulti(keys []*ds.Key, _meta ds.MultiMetaGetter, cb ds.GetMul
tiCB) error { |
58 » d.data.getMulti(keys, cb) | 49 » return d.data.getMulti(keys, cb) |
59 » return nil | |
60 } | 50 } |
61 | 51 |
62 func (d *dsImpl) DeleteMulti(keys []ds.Key, cb ds.DeleteMultiCB) error { | 52 func (d *dsImpl) DeleteMulti(keys []*ds.Key, cb ds.DeleteMultiCB) error { |
63 d.data.delMulti(keys, cb) | 53 d.data.delMulti(keys, cb) |
64 return nil | 54 return nil |
65 } | 55 } |
66 | 56 |
67 func (d *dsImpl) NewQuery(kind string) ds.Query { | |
68 return &queryImpl{ns: d.ns, kind: kind} | |
69 } | |
70 | |
71 func (d *dsImpl) DecodeCursor(s string) (ds.Cursor, error) { | 57 func (d *dsImpl) DecodeCursor(s string) (ds.Cursor, error) { |
72 return newCursor(s) | 58 return newCursor(s) |
73 } | 59 } |
74 | 60 |
75 func (d *dsImpl) Run(qi ds.Query, cb ds.RawRunCB) error { | 61 func (d *dsImpl) Run(fq *ds.FinalizedQuery, cb ds.RawRunCB) error { |
76 » q := qi.(*queryImpl) | 62 » idx, head := d.data.getQuerySnaps(!fq.EventuallyConsistent()) |
77 » consistent := q.eqFilters["__ancestor__"] != nil && !q.eventualConsisten
cy | 63 » return executeQuery(fq, d.ns, false, idx, head, cb) |
78 » idx, head := d.data.getQuerySnaps(consistent) | |
79 » return executeQuery(qi, d.ns, false, idx, head, cb) | |
80 } | 64 } |
81 | 65 |
82 func (d *dsImpl) AddIndexes(idxs ...*ds.IndexDefinition) { | 66 func (d *dsImpl) AddIndexes(idxs ...*ds.IndexDefinition) { |
83 if len(idxs) == 0 { | 67 if len(idxs) == 0 { |
84 return | 68 return |
85 } | 69 } |
86 | 70 |
87 for _, i := range idxs { | 71 for _, i := range idxs { |
88 if !i.Compound() { | 72 if !i.Compound() { |
89 panic(fmt.Errorf("Attempted to add non-compound index: %
s", i)) | 73 panic(fmt.Errorf("Attempted to add non-compound index: %
s", i)) |
(...skipping 27 matching lines...) Expand all Loading... |
117 | 101 |
118 ////////////////////////////////// txnDsImpl /////////////////////////////////// | 102 ////////////////////////////////// txnDsImpl /////////////////////////////////// |
119 | 103 |
120 type txnDsImpl struct { | 104 type txnDsImpl struct { |
121 data *txnDataStoreData | 105 data *txnDataStoreData |
122 ns string | 106 ns string |
123 } | 107 } |
124 | 108 |
125 var _ ds.RawInterface = (*txnDsImpl)(nil) | 109 var _ ds.RawInterface = (*txnDsImpl)(nil) |
126 | 110 |
127 func (d *txnDsImpl) DecodeKey(encoded string) (ds.Key, error) { | 111 func (d *txnDsImpl) PutMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.PutMul
tiCB) error { |
128 » return dskey.NewFromEncoded(encoded) | |
129 } | |
130 | |
131 func (d *txnDsImpl) NewKey(kind, stringID string, intID int64, parent ds.Key) ds
.Key { | |
132 » return dskey.New(globalAppID, d.ns, kind, stringID, intID, parent) | |
133 } | |
134 | |
135 func (d *txnDsImpl) PutMulti(keys []ds.Key, vals []ds.PropertyMap, cb ds.PutMult
iCB) error { | |
136 return d.data.run(func() error { | 112 return d.data.run(func() error { |
137 d.data.putMulti(keys, vals, cb) | 113 d.data.putMulti(keys, vals, cb) |
138 return nil | 114 return nil |
139 }) | 115 }) |
140 } | 116 } |
141 | 117 |
142 func (d *txnDsImpl) GetMulti(keys []ds.Key, _meta ds.MultiMetaGetter, cb ds.GetM
ultiCB) error { | 118 func (d *txnDsImpl) GetMulti(keys []*ds.Key, _meta ds.MultiMetaGetter, cb ds.Get
MultiCB) error { |
143 return d.data.run(func() error { | 119 return d.data.run(func() error { |
144 return d.data.getMulti(keys, cb) | 120 return d.data.getMulti(keys, cb) |
145 }) | 121 }) |
146 } | 122 } |
147 | 123 |
148 func (d *txnDsImpl) DeleteMulti(keys []ds.Key, cb ds.DeleteMultiCB) error { | 124 func (d *txnDsImpl) DeleteMulti(keys []*ds.Key, cb ds.DeleteMultiCB) error { |
149 return d.data.run(func() error { | 125 return d.data.run(func() error { |
150 return d.data.delMulti(keys, cb) | 126 return d.data.delMulti(keys, cb) |
151 }) | 127 }) |
152 } | 128 } |
153 | 129 |
154 func (d *txnDsImpl) DecodeCursor(s string) (ds.Cursor, error) { | 130 func (d *txnDsImpl) DecodeCursor(s string) (ds.Cursor, error) { |
155 return newCursor(s) | 131 return newCursor(s) |
156 } | 132 } |
157 | 133 |
158 func (d *txnDsImpl) Run(q ds.Query, cb ds.RawRunCB) error { | 134 func (d *txnDsImpl) Run(q *ds.FinalizedQuery, cb ds.RawRunCB) error { |
159 return executeQuery(q, d.ns, true, d.data.snap, d.data.snap, cb) | 135 return executeQuery(q, d.ns, true, d.data.snap, d.data.snap, cb) |
160 } | 136 } |
161 | 137 |
162 func (*txnDsImpl) RunInTransaction(func(c context.Context) error, *ds.Transactio
nOptions) error { | 138 func (*txnDsImpl) RunInTransaction(func(c context.Context) error, *ds.Transactio
nOptions) error { |
163 return errors.New("datastore: nested transactions are not supported") | 139 return errors.New("datastore: nested transactions are not supported") |
164 } | 140 } |
165 | 141 |
166 func (d *txnDsImpl) NewQuery(kind string) ds.Query { | |
167 return &queryImpl{ns: d.ns, kind: kind} | |
168 } | |
169 | |
170 func (*txnDsImpl) Testable() ds.Testable { | 142 func (*txnDsImpl) Testable() ds.Testable { |
171 return nil | 143 return nil |
172 } | 144 } |
OLD | NEW |