| 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 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 dsd := cur(ic).Get(memContextDSIdx) | 22 dsd := cur(ic).Get(memContextDSIdx) |
| 23 | 23 |
| 24 ns := curGID(ic).namespace | 24 ns := curGID(ic).namespace |
| 25 if x, ok := dsd.(*dataStoreData); ok { | 25 if x, ok := dsd.(*dataStoreData); ok { |
| 26 return &dsImpl{x, ns, ic} | 26 return &dsImpl{x, ns, ic} |
| 27 } | 27 } |
| 28 return &txnDsImpl{dsd.(*txnDataStoreData), ns} | 28 return &txnDsImpl{dsd.(*txnDataStoreData), ns} |
| 29 }) | 29 }) |
| 30 } | 30 } |
| 31 | 31 |
| 32 // NewDatastore creates a new standalone memory implementation of the datastore. |
| 33 func NewDatastore(aid, ns string) ds.RawInterface { |
| 34 return &dsImpl{newDataStoreData(aid), ns, context.Background()} |
| 35 } |
| 36 |
| 32 //////////////////////////////////// dsImpl //////////////////////////////////// | 37 //////////////////////////////////// dsImpl //////////////////////////////////// |
| 33 | 38 |
| 34 // dsImpl exists solely to bind the current c to the datastore data. | 39 // dsImpl exists solely to bind the current c to the datastore data. |
| 35 type dsImpl struct { | 40 type dsImpl struct { |
| 36 data *dataStoreData | 41 data *dataStoreData |
| 37 ns string | 42 ns string |
| 38 c context.Context | 43 c context.Context |
| 39 } | 44 } |
| 40 | 45 |
| 41 var _ ds.RawInterface = (*dsImpl)(nil) | 46 var _ ds.RawInterface = (*dsImpl)(nil) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 57 d.data.delMulti(keys, cb) | 62 d.data.delMulti(keys, cb) |
| 58 return nil | 63 return nil |
| 59 } | 64 } |
| 60 | 65 |
| 61 func (d *dsImpl) DecodeCursor(s string) (ds.Cursor, error) { | 66 func (d *dsImpl) DecodeCursor(s string) (ds.Cursor, error) { |
| 62 return newCursor(s) | 67 return newCursor(s) |
| 63 } | 68 } |
| 64 | 69 |
| 65 func (d *dsImpl) Run(fq *ds.FinalizedQuery, cb ds.RawRunCB) error { | 70 func (d *dsImpl) Run(fq *ds.FinalizedQuery, cb ds.RawRunCB) error { |
| 66 idx, head := d.data.getQuerySnaps(!fq.EventuallyConsistent()) | 71 idx, head := d.data.getQuerySnaps(!fq.EventuallyConsistent()) |
| 67 » err := executeQuery(fq, d.ns, false, idx, head, cb) | 72 » err := executeQuery(fq, d.data.aid, d.ns, false, idx, head, cb) |
| 68 if d.data.maybeAutoIndex(err) { | 73 if d.data.maybeAutoIndex(err) { |
| 69 idx, head = d.data.getQuerySnaps(!fq.EventuallyConsistent()) | 74 idx, head = d.data.getQuerySnaps(!fq.EventuallyConsistent()) |
| 70 » » err = executeQuery(fq, d.ns, false, idx, head, cb) | 75 » » err = executeQuery(fq, d.data.aid, d.ns, false, idx, head, cb) |
| 71 } | 76 } |
| 72 return err | 77 return err |
| 73 } | 78 } |
| 74 | 79 |
| 75 func (d *dsImpl) Count(fq *ds.FinalizedQuery) (ret int64, err error) { | 80 func (d *dsImpl) Count(fq *ds.FinalizedQuery) (ret int64, err error) { |
| 76 idx, head := d.data.getQuerySnaps(!fq.EventuallyConsistent()) | 81 idx, head := d.data.getQuerySnaps(!fq.EventuallyConsistent()) |
| 77 » ret, err = countQuery(fq, d.ns, false, idx, head) | 82 » ret, err = countQuery(fq, d.data.aid, d.ns, false, idx, head) |
| 78 if d.data.maybeAutoIndex(err) { | 83 if d.data.maybeAutoIndex(err) { |
| 79 idx, head := d.data.getQuerySnaps(!fq.EventuallyConsistent()) | 84 idx, head := d.data.getQuerySnaps(!fq.EventuallyConsistent()) |
| 80 » » ret, err = countQuery(fq, d.ns, false, idx, head) | 85 » » ret, err = countQuery(fq, d.data.aid, d.ns, false, idx, head) |
| 81 } | 86 } |
| 82 return | 87 return |
| 83 } | 88 } |
| 84 | 89 |
| 85 func (d *dsImpl) AddIndexes(idxs ...*ds.IndexDefinition) { | 90 func (d *dsImpl) AddIndexes(idxs ...*ds.IndexDefinition) { |
| 86 if len(idxs) == 0 { | 91 if len(idxs) == 0 { |
| 87 return | 92 return |
| 88 } | 93 } |
| 89 | 94 |
| 90 for _, i := range idxs { | 95 for _, i := range idxs { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 func (d *txnDsImpl) Run(q *ds.FinalizedQuery, cb ds.RawRunCB) error { | 172 func (d *txnDsImpl) Run(q *ds.FinalizedQuery, cb ds.RawRunCB) error { |
| 168 // note that autoIndex has no effect inside transactions. This is becaus
e | 173 // note that autoIndex has no effect inside transactions. This is becaus
e |
| 169 // the transaction guarantees a consistent view of head at the time that
the | 174 // the transaction guarantees a consistent view of head at the time that
the |
| 170 // transaction opens. At best, we could add the index on head, but then
return | 175 // transaction opens. At best, we could add the index on head, but then
return |
| 171 // the error anyway, but adding the index then re-snapping at head would | 176 // the error anyway, but adding the index then re-snapping at head would |
| 172 // potentially reveal other entities not in the original transaction sna
pshot. | 177 // potentially reveal other entities not in the original transaction sna
pshot. |
| 173 // | 178 // |
| 174 // It's possible that if you have full-consistency and also auto index e
nabled | 179 // It's possible that if you have full-consistency and also auto index e
nabled |
| 175 // that this would make sense... but at that point you should probably j
ust | 180 // that this would make sense... but at that point you should probably j
ust |
| 176 // add the index up front. | 181 // add the index up front. |
| 177 » return executeQuery(q, d.ns, true, d.data.snap, d.data.snap, cb) | 182 » return executeQuery(q, d.data.parent.aid, d.ns, true, d.data.snap, d.dat
a.snap, cb) |
| 178 } | 183 } |
| 179 | 184 |
| 180 func (d *txnDsImpl) Count(fq *ds.FinalizedQuery) (ret int64, err error) { | 185 func (d *txnDsImpl) Count(fq *ds.FinalizedQuery) (ret int64, err error) { |
| 181 » return countQuery(fq, d.ns, true, d.data.snap, d.data.snap) | 186 » return countQuery(fq, d.data.parent.aid, d.ns, true, d.data.snap, d.data
.snap) |
| 182 } | 187 } |
| 183 | 188 |
| 184 func (*txnDsImpl) RunInTransaction(func(c context.Context) error, *ds.Transactio
nOptions) error { | 189 func (*txnDsImpl) RunInTransaction(func(c context.Context) error, *ds.Transactio
nOptions) error { |
| 185 return errors.New("datastore: nested transactions are not supported") | 190 return errors.New("datastore: nested transactions are not supported") |
| 186 } | 191 } |
| 187 | 192 |
| 188 func (*txnDsImpl) Testable() ds.Testable { | 193 func (*txnDsImpl) Testable() ds.Testable { |
| 189 return nil | 194 return nil |
| 190 } | 195 } |
| OLD | NEW |