| 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 | 9 |
| 10 "golang.org/x/net/context" | 10 "golang.org/x/net/context" |
| 11 | 11 |
| 12 » "github.com/luci/gae" | 12 » rds "github.com/luci/gae/service/rawdatastore" |
| 13 » "github.com/luci/gae/helper" | |
| 14 ) | 13 ) |
| 15 | 14 |
| 16 //////////////////////////////////// public //////////////////////////////////// | 15 //////////////////////////////////// public //////////////////////////////////// |
| 17 | 16 |
| 18 // useRDS adds a gae.Datastore implementation to context, accessible | 17 // useRDS adds a gae.Datastore implementation to context, accessible |
| 19 // by gae.GetDS(c) | 18 // by gae.GetDS(c) |
| 20 func useRDS(c context.Context) context.Context { | 19 func useRDS(c context.Context) context.Context { |
| 21 » return gae.SetRDSFactory(c, func(ic context.Context) gae.RawDatastore { | 20 » return rds.SetFactory(c, func(ic context.Context) rds.Interface { |
| 22 dsd := cur(ic).Get(memContextDSIdx) | 21 dsd := cur(ic).Get(memContextDSIdx) |
| 23 | 22 |
| 24 ns := curGID(ic).namespace | 23 ns := curGID(ic).namespace |
| 25 if x, ok := dsd.(*dataStoreData); ok { | 24 if x, ok := dsd.(*dataStoreData); ok { |
| 26 return &dsImpl{x, ns, ic} | 25 return &dsImpl{x, ns, ic} |
| 27 } | 26 } |
| 28 return &txnDsImpl{dsd.(*txnDataStoreData), ns} | 27 return &txnDsImpl{dsd.(*txnDataStoreData), ns} |
| 29 }) | 28 }) |
| 30 } | 29 } |
| 31 | 30 |
| 32 //////////////////////////////////// dsImpl //////////////////////////////////// | 31 //////////////////////////////////// dsImpl //////////////////////////////////// |
| 33 | 32 |
| 34 // dsImpl exists solely to bind the current c to the datastore data. | 33 // dsImpl exists solely to bind the current c to the datastore data. |
| 35 type dsImpl struct { | 34 type dsImpl struct { |
| 36 data *dataStoreData | 35 data *dataStoreData |
| 37 ns string | 36 ns string |
| 38 c context.Context | 37 c context.Context |
| 39 } | 38 } |
| 40 | 39 |
| 41 var _ gae.RawDatastore = (*dsImpl)(nil) | 40 var _ rds.Interface = (*dsImpl)(nil) |
| 42 | 41 |
| 43 func (d *dsImpl) DecodeKey(encoded string) (gae.DSKey, error) { | 42 func (d *dsImpl) DecodeKey(encoded string) (rds.Key, error) { |
| 44 » return helper.NewDSKeyFromEncoded(encoded) | 43 » return rds.NewKeyFromEncoded(encoded) |
| 45 } | 44 } |
| 46 | 45 |
| 47 func (d *dsImpl) NewKey(kind, stringID string, intID int64, parent gae.DSKey) ga
e.DSKey { | 46 func (d *dsImpl) NewKey(kind, stringID string, intID int64, parent rds.Key) rds.
Key { |
| 48 » return helper.NewDSKey(globalAppID, d.ns, kind, stringID, intID, parent) | 47 » return rds.NewKey(globalAppID, d.ns, kind, stringID, intID, parent) |
| 49 } | 48 } |
| 50 | 49 |
| 51 func (d *dsImpl) Put(key gae.DSKey, pls gae.DSPropertyLoadSaver) (gae.DSKey, err
or) { | 50 func (d *dsImpl) Put(key rds.Key, pls rds.PropertyLoadSaver) (rds.Key, error) { |
| 52 return d.data.put(d.ns, key, pls) | 51 return d.data.put(d.ns, key, pls) |
| 53 } | 52 } |
| 54 | 53 |
| 55 func (d *dsImpl) PutMulti(keys []gae.DSKey, plss []gae.DSPropertyLoadSaver) ([]g
ae.DSKey, error) { | 54 func (d *dsImpl) PutMulti(keys []rds.Key, plss []rds.PropertyLoadSaver) ([]rds.K
ey, error) { |
| 56 return d.data.putMulti(d.ns, keys, plss) | 55 return d.data.putMulti(d.ns, keys, plss) |
| 57 } | 56 } |
| 58 | 57 |
| 59 func (d *dsImpl) Get(key gae.DSKey, pls gae.DSPropertyLoadSaver) error { | 58 func (d *dsImpl) Get(key rds.Key, pls rds.PropertyLoadSaver) error { |
| 60 return d.data.get(d.ns, key, pls) | 59 return d.data.get(d.ns, key, pls) |
| 61 } | 60 } |
| 62 | 61 |
| 63 func (d *dsImpl) GetMulti(keys []gae.DSKey, plss []gae.DSPropertyLoadSaver) erro
r { | 62 func (d *dsImpl) GetMulti(keys []rds.Key, plss []rds.PropertyLoadSaver) error { |
| 64 return d.data.getMulti(d.ns, keys, plss) | 63 return d.data.getMulti(d.ns, keys, plss) |
| 65 } | 64 } |
| 66 | 65 |
| 67 func (d *dsImpl) Delete(key gae.DSKey) error { | 66 func (d *dsImpl) Delete(key rds.Key) error { |
| 68 return d.data.del(d.ns, key) | 67 return d.data.del(d.ns, key) |
| 69 } | 68 } |
| 70 | 69 |
| 71 func (d *dsImpl) DeleteMulti(keys []gae.DSKey) error { | 70 func (d *dsImpl) DeleteMulti(keys []rds.Key) error { |
| 72 return d.data.delMulti(d.ns, keys) | 71 return d.data.delMulti(d.ns, keys) |
| 73 } | 72 } |
| 74 | 73 |
| 75 func (d *dsImpl) NewQuery(kind string) gae.DSQuery { | 74 func (d *dsImpl) NewQuery(kind string) rds.Query { |
| 76 return &queryImpl{ns: d.ns, kind: kind} | 75 return &queryImpl{ns: d.ns, kind: kind} |
| 77 } | 76 } |
| 78 | 77 |
| 79 func (d *dsImpl) Run(q gae.DSQuery) gae.RDSIterator { | 78 func (d *dsImpl) Run(q rds.Query) rds.Iterator { |
| 80 rq := q.(*queryImpl) | 79 rq := q.(*queryImpl) |
| 81 rq = rq.normalize().checkCorrectness(d.ns, false) | 80 rq = rq.normalize().checkCorrectness(d.ns, false) |
| 82 return &queryIterImpl{rq} | 81 return &queryIterImpl{rq} |
| 83 } | 82 } |
| 84 | 83 |
| 85 func (d *dsImpl) GetAll(q gae.DSQuery, dst *[]gae.DSPropertyMap) ([]gae.DSKey, e
rror) { | 84 func (d *dsImpl) GetAll(q rds.Query, dst *[]rds.PropertyMap) ([]rds.Key, error)
{ |
| 86 // TODO(riannucci): assert that dst is a slice of structs | 85 // TODO(riannucci): assert that dst is a slice of structs |
| 87 panic("NOT IMPLEMENTED") | 86 panic("NOT IMPLEMENTED") |
| 88 } | 87 } |
| 89 | 88 |
| 90 func (d *dsImpl) Count(q gae.DSQuery) (int, error) { | 89 func (d *dsImpl) Count(q rds.Query) (int, error) { |
| 91 return count(d.Run(q.KeysOnly())) | 90 return count(d.Run(q.KeysOnly())) |
| 92 } | 91 } |
| 93 | 92 |
| 94 ////////////////////////////////// txnDsImpl /////////////////////////////////// | 93 ////////////////////////////////// txnDsImpl /////////////////////////////////// |
| 95 | 94 |
| 96 type txnDsImpl struct { | 95 type txnDsImpl struct { |
| 97 data *txnDataStoreData | 96 data *txnDataStoreData |
| 98 ns string | 97 ns string |
| 99 } | 98 } |
| 100 | 99 |
| 101 var _ gae.RawDatastore = (*txnDsImpl)(nil) | 100 var _ rds.Interface = (*txnDsImpl)(nil) |
| 102 | 101 |
| 103 func (d *txnDsImpl) DecodeKey(encoded string) (gae.DSKey, error) { | 102 func (d *txnDsImpl) DecodeKey(encoded string) (rds.Key, error) { |
| 104 » return helper.NewDSKeyFromEncoded(encoded) | 103 » return rds.NewKeyFromEncoded(encoded) |
| 105 } | 104 } |
| 106 | 105 |
| 107 func (d *txnDsImpl) NewKey(kind, stringID string, intID int64, parent gae.DSKey)
gae.DSKey { | 106 func (d *txnDsImpl) NewKey(kind, stringID string, intID int64, parent rds.Key) r
ds.Key { |
| 108 » return helper.NewDSKey(globalAppID, d.ns, kind, stringID, intID, parent) | 107 » return rds.NewKey(globalAppID, d.ns, kind, stringID, intID, parent) |
| 109 } | 108 } |
| 110 | 109 |
| 111 func (d *txnDsImpl) Put(key gae.DSKey, pls gae.DSPropertyLoadSaver) (retKey gae.
DSKey, err error) { | 110 func (d *txnDsImpl) Put(key rds.Key, pls rds.PropertyLoadSaver) (retKey rds.Key,
err error) { |
| 112 err = d.data.run(func() (err error) { | 111 err = d.data.run(func() (err error) { |
| 113 retKey, err = d.data.put(d.ns, key, pls) | 112 retKey, err = d.data.put(d.ns, key, pls) |
| 114 return | 113 return |
| 115 }) | 114 }) |
| 116 return | 115 return |
| 117 } | 116 } |
| 118 | 117 |
| 119 func (d *txnDsImpl) PutMulti(keys []gae.DSKey, plss []gae.DSPropertyLoadSaver) (
retKeys []gae.DSKey, err error) { | 118 func (d *txnDsImpl) PutMulti(keys []rds.Key, plss []rds.PropertyLoadSaver) (retK
eys []rds.Key, err error) { |
| 120 err = d.data.run(func() (err error) { | 119 err = d.data.run(func() (err error) { |
| 121 retKeys, err = d.data.putMulti(d.ns, keys, plss) | 120 retKeys, err = d.data.putMulti(d.ns, keys, plss) |
| 122 return | 121 return |
| 123 }) | 122 }) |
| 124 return | 123 return |
| 125 } | 124 } |
| 126 | 125 |
| 127 func (d *txnDsImpl) Get(key gae.DSKey, pls gae.DSPropertyLoadSaver) error { | 126 func (d *txnDsImpl) Get(key rds.Key, pls rds.PropertyLoadSaver) error { |
| 128 return d.data.run(func() error { | 127 return d.data.run(func() error { |
| 129 return d.data.get(d.ns, key, pls) | 128 return d.data.get(d.ns, key, pls) |
| 130 }) | 129 }) |
| 131 } | 130 } |
| 132 | 131 |
| 133 func (d *txnDsImpl) GetMulti(keys []gae.DSKey, plss []gae.DSPropertyLoadSaver) e
rror { | 132 func (d *txnDsImpl) GetMulti(keys []rds.Key, plss []rds.PropertyLoadSaver) error
{ |
| 134 return d.data.run(func() error { | 133 return d.data.run(func() error { |
| 135 return d.data.getMulti(d.ns, keys, plss) | 134 return d.data.getMulti(d.ns, keys, plss) |
| 136 }) | 135 }) |
| 137 } | 136 } |
| 138 | 137 |
| 139 func (d *txnDsImpl) Delete(key gae.DSKey) error { | 138 func (d *txnDsImpl) Delete(key rds.Key) error { |
| 140 return d.data.run(func() error { | 139 return d.data.run(func() error { |
| 141 return d.data.del(d.ns, key) | 140 return d.data.del(d.ns, key) |
| 142 }) | 141 }) |
| 143 } | 142 } |
| 144 | 143 |
| 145 func (d *txnDsImpl) DeleteMulti(keys []gae.DSKey) error { | 144 func (d *txnDsImpl) DeleteMulti(keys []rds.Key) error { |
| 146 return d.data.run(func() error { | 145 return d.data.run(func() error { |
| 147 return d.data.delMulti(d.ns, keys) | 146 return d.data.delMulti(d.ns, keys) |
| 148 }) | 147 }) |
| 149 } | 148 } |
| 150 | 149 |
| 151 func (d *txnDsImpl) Run(q gae.DSQuery) gae.RDSIterator { | 150 func (d *txnDsImpl) Run(q rds.Query) rds.Iterator { |
| 152 rq := q.(*queryImpl) | 151 rq := q.(*queryImpl) |
| 153 if rq.ancestor == nil { | 152 if rq.ancestor == nil { |
| 154 rq.err = errors.New("memory: queries in transactions only suppor
t ancestor queries") | 153 rq.err = errors.New("memory: queries in transactions only suppor
t ancestor queries") |
| 155 return &queryIterImpl{rq} | 154 return &queryIterImpl{rq} |
| 156 } | 155 } |
| 157 panic("NOT IMPLEMENTED") | 156 panic("NOT IMPLEMENTED") |
| 158 } | 157 } |
| 159 | 158 |
| 160 func (*txnDsImpl) RunInTransaction(func(c context.Context) error, *gae.DSTransac
tionOptions) error { | 159 func (*txnDsImpl) RunInTransaction(func(c context.Context) error, *rds.Transacti
onOptions) error { |
| 161 return errors.New("datastore: nested transactions are not supported") | 160 return errors.New("datastore: nested transactions are not supported") |
| 162 } | 161 } |
| 163 | 162 |
| 164 func (d *txnDsImpl) NewQuery(kind string) gae.DSQuery { | 163 func (d *txnDsImpl) NewQuery(kind string) rds.Query { |
| 165 return &queryImpl{ns: d.ns, kind: kind} | 164 return &queryImpl{ns: d.ns, kind: kind} |
| 166 } | 165 } |
| 167 | 166 |
| 168 func (d *txnDsImpl) GetAll(q gae.DSQuery, dst *[]gae.DSPropertyMap) ([]gae.DSKey
, error) { | 167 func (d *txnDsImpl) GetAll(q rds.Query, dst *[]rds.PropertyMap) ([]rds.Key, erro
r) { |
| 169 // TODO(riannucci): assert that dst is a slice of structs | 168 // TODO(riannucci): assert that dst is a slice of structs |
| 170 panic("NOT IMPLEMENTED") | 169 panic("NOT IMPLEMENTED") |
| 171 } | 170 } |
| 172 | 171 |
| 173 func (d *txnDsImpl) Count(q gae.DSQuery) (int, error) { | 172 func (d *txnDsImpl) Count(q rds.Query) (int, error) { |
| 174 return count(d.Run(q.KeysOnly())) | 173 return count(d.Run(q.KeysOnly())) |
| 175 } | 174 } |
| 176 | 175 |
| 177 func count(itr gae.RDSIterator) (ret int, err error) { | 176 func count(itr rds.Iterator) (ret int, err error) { |
| 178 for _, err = itr.Next(nil); err != nil; _, err = itr.Next(nil) { | 177 for _, err = itr.Next(nil); err != nil; _, err = itr.Next(nil) { |
| 179 ret++ | 178 ret++ |
| 180 } | 179 } |
| 181 » if err == gae.ErrDSQueryDone { | 180 » if err == rds.ErrQueryDone { |
| 182 err = nil | 181 err = nil |
| 183 } | 182 } |
| 184 return | 183 return |
| 185 } | 184 } |
| OLD | NEW |