| 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" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 var _ rds.Interface = (*dsImpl)(nil) | 40 var _ rds.Interface = (*dsImpl)(nil) |
| 41 | 41 |
| 42 func (d *dsImpl) DecodeKey(encoded string) (rds.Key, error) { | 42 func (d *dsImpl) DecodeKey(encoded string) (rds.Key, error) { |
| 43 return rds.NewKeyFromEncoded(encoded) | 43 return rds.NewKeyFromEncoded(encoded) |
| 44 } | 44 } |
| 45 | 45 |
| 46 func (d *dsImpl) NewKey(kind, stringID string, intID int64, parent rds.Key) rds.
Key { | 46 func (d *dsImpl) NewKey(kind, stringID string, intID int64, parent rds.Key) rds.
Key { |
| 47 return rds.NewKey(globalAppID, d.ns, kind, stringID, intID, parent) | 47 return rds.NewKey(globalAppID, d.ns, kind, stringID, intID, parent) |
| 48 } | 48 } |
| 49 | 49 |
| 50 func (d *dsImpl) Put(key rds.Key, pls rds.PropertyLoadSaver) (rds.Key, error) { | 50 func (d *dsImpl) PutMulti(keys []rds.Key, vals []rds.PropertyLoadSaver, cb rds.P
utMultiCB) error { |
| 51 » return d.data.put(d.ns, key, pls) | 51 » d.data.putMulti(keys, vals, cb) |
| 52 » return nil |
| 52 } | 53 } |
| 53 | 54 |
| 54 func (d *dsImpl) PutMulti(keys []rds.Key, plss []rds.PropertyLoadSaver) ([]rds.K
ey, error) { | 55 func (d *dsImpl) GetMulti(keys []rds.Key, cb rds.GetMultiCB) error { |
| 55 » return d.data.putMulti(d.ns, keys, plss) | 56 » d.data.getMulti(keys, cb) |
| 57 » return nil |
| 56 } | 58 } |
| 57 | 59 |
| 58 func (d *dsImpl) Get(key rds.Key, pls rds.PropertyLoadSaver) error { | 60 func (d *dsImpl) DeleteMulti(keys []rds.Key, cb rds.DeleteMultiCB) error { |
| 59 » return d.data.get(d.ns, key, pls) | 61 » d.data.delMulti(keys, cb) |
| 60 } | 62 » return nil |
| 61 | |
| 62 func (d *dsImpl) GetMulti(keys []rds.Key, plss []rds.PropertyLoadSaver) error { | |
| 63 » return d.data.getMulti(d.ns, keys, plss) | |
| 64 } | |
| 65 | |
| 66 func (d *dsImpl) Delete(key rds.Key) error { | |
| 67 » return d.data.del(d.ns, key) | |
| 68 } | |
| 69 | |
| 70 func (d *dsImpl) DeleteMulti(keys []rds.Key) error { | |
| 71 » return d.data.delMulti(d.ns, keys) | |
| 72 } | 63 } |
| 73 | 64 |
| 74 func (d *dsImpl) NewQuery(kind string) rds.Query { | 65 func (d *dsImpl) NewQuery(kind string) rds.Query { |
| 75 return &queryImpl{ns: d.ns, kind: kind} | 66 return &queryImpl{ns: d.ns, kind: kind} |
| 76 } | 67 } |
| 77 | 68 |
| 78 func (d *dsImpl) Run(q rds.Query) rds.Iterator { | 69 func (d *dsImpl) Run(q rds.Query, cb rds.RunCB) error { |
| 79 » rq := q.(*queryImpl) | 70 » return nil |
| 80 » rq = rq.normalize().checkCorrectness(d.ns, false) | 71 » /* |
| 81 » return &queryIterImpl{rq} | 72 » » rq := q.(*queryImpl) |
| 82 } | 73 » » rq = rq.normalize().checkCorrectness(d.ns, false) |
| 83 | 74 » » return &queryIterImpl{rq} |
| 84 func (d *dsImpl) GetAll(q rds.Query, dst *[]rds.PropertyMap) ([]rds.Key, error)
{ | 75 » */ |
| 85 » // TODO(riannucci): assert that dst is a slice of structs | |
| 86 » panic("NOT IMPLEMENTED") | |
| 87 } | |
| 88 | |
| 89 func (d *dsImpl) Count(q rds.Query) (int, error) { | |
| 90 » return count(d.Run(q.KeysOnly())) | |
| 91 } | 76 } |
| 92 | 77 |
| 93 ////////////////////////////////// txnDsImpl /////////////////////////////////// | 78 ////////////////////////////////// txnDsImpl /////////////////////////////////// |
| 94 | 79 |
| 95 type txnDsImpl struct { | 80 type txnDsImpl struct { |
| 96 data *txnDataStoreData | 81 data *txnDataStoreData |
| 97 ns string | 82 ns string |
| 98 } | 83 } |
| 99 | 84 |
| 100 var _ rds.Interface = (*txnDsImpl)(nil) | 85 var _ rds.Interface = (*txnDsImpl)(nil) |
| 101 | 86 |
| 102 func (d *txnDsImpl) DecodeKey(encoded string) (rds.Key, error) { | 87 func (d *txnDsImpl) DecodeKey(encoded string) (rds.Key, error) { |
| 103 return rds.NewKeyFromEncoded(encoded) | 88 return rds.NewKeyFromEncoded(encoded) |
| 104 } | 89 } |
| 105 | 90 |
| 106 func (d *txnDsImpl) NewKey(kind, stringID string, intID int64, parent rds.Key) r
ds.Key { | 91 func (d *txnDsImpl) NewKey(kind, stringID string, intID int64, parent rds.Key) r
ds.Key { |
| 107 return rds.NewKey(globalAppID, d.ns, kind, stringID, intID, parent) | 92 return rds.NewKey(globalAppID, d.ns, kind, stringID, intID, parent) |
| 108 } | 93 } |
| 109 | 94 |
| 110 func (d *txnDsImpl) Put(key rds.Key, pls rds.PropertyLoadSaver) (retKey rds.Key,
err error) { | 95 func (d *txnDsImpl) PutMulti(keys []rds.Key, vals []rds.PropertyLoadSaver, cb rd
s.PutMultiCB) error { |
| 111 » err = d.data.run(func() (err error) { | |
| 112 » » retKey, err = d.data.put(d.ns, key, pls) | |
| 113 » » return | |
| 114 » }) | |
| 115 » return | |
| 116 } | |
| 117 | |
| 118 func (d *txnDsImpl) PutMulti(keys []rds.Key, plss []rds.PropertyLoadSaver) (retK
eys []rds.Key, err error) { | |
| 119 » err = d.data.run(func() (err error) { | |
| 120 » » retKeys, err = d.data.putMulti(d.ns, keys, plss) | |
| 121 » » return | |
| 122 » }) | |
| 123 » return | |
| 124 } | |
| 125 | |
| 126 func (d *txnDsImpl) Get(key rds.Key, pls rds.PropertyLoadSaver) error { | |
| 127 return d.data.run(func() error { | 96 return d.data.run(func() error { |
| 128 » » return d.data.get(d.ns, key, pls) | 97 » » d.data.putMulti(keys, vals, cb) |
| 98 » » return nil |
| 129 }) | 99 }) |
| 130 } | 100 } |
| 131 | 101 |
| 132 func (d *txnDsImpl) GetMulti(keys []rds.Key, plss []rds.PropertyLoadSaver) error
{ | 102 func (d *txnDsImpl) GetMulti(keys []rds.Key, cb rds.GetMultiCB) error { |
| 133 return d.data.run(func() error { | 103 return d.data.run(func() error { |
| 134 » » return d.data.getMulti(d.ns, keys, plss) | 104 » » return d.data.getMulti(keys, cb) |
| 135 }) | 105 }) |
| 136 } | 106 } |
| 137 | 107 |
| 138 func (d *txnDsImpl) Delete(key rds.Key) error { | 108 func (d *txnDsImpl) DeleteMulti(keys []rds.Key, cb rds.DeleteMultiCB) error { |
| 139 return d.data.run(func() error { | 109 return d.data.run(func() error { |
| 140 » » return d.data.del(d.ns, key) | 110 » » return d.data.delMulti(keys, cb) |
| 141 }) | 111 }) |
| 142 } | 112 } |
| 143 | 113 |
| 144 func (d *txnDsImpl) DeleteMulti(keys []rds.Key) error { | 114 func (d *txnDsImpl) Run(q rds.Query, cb rds.RunCB) error { |
| 145 » return d.data.run(func() error { | |
| 146 » » return d.data.delMulti(d.ns, keys) | |
| 147 » }) | |
| 148 } | |
| 149 | |
| 150 func (d *txnDsImpl) Run(q rds.Query) rds.Iterator { | |
| 151 rq := q.(*queryImpl) | 115 rq := q.(*queryImpl) |
| 152 if rq.ancestor == nil { | 116 if rq.ancestor == nil { |
| 153 » » rq.err = errors.New("memory: queries in transactions only suppor
t ancestor queries") | 117 » » return errors.New("memory: queries in transactions only support
ancestor queries") |
| 154 » » return &queryIterImpl{rq} | |
| 155 } | 118 } |
| 156 panic("NOT IMPLEMENTED") | 119 panic("NOT IMPLEMENTED") |
| 157 } | 120 } |
| 158 | 121 |
| 159 func (*txnDsImpl) RunInTransaction(func(c context.Context) error, *rds.Transacti
onOptions) error { | 122 func (*txnDsImpl) RunInTransaction(func(c context.Context) error, *rds.Transacti
onOptions) error { |
| 160 return errors.New("datastore: nested transactions are not supported") | 123 return errors.New("datastore: nested transactions are not supported") |
| 161 } | 124 } |
| 162 | 125 |
| 163 func (d *txnDsImpl) NewQuery(kind string) rds.Query { | 126 func (d *txnDsImpl) NewQuery(kind string) rds.Query { |
| 164 return &queryImpl{ns: d.ns, kind: kind} | 127 return &queryImpl{ns: d.ns, kind: kind} |
| 165 } | 128 } |
| 166 | |
| 167 func (d *txnDsImpl) GetAll(q rds.Query, dst *[]rds.PropertyMap) ([]rds.Key, erro
r) { | |
| 168 // TODO(riannucci): assert that dst is a slice of structs | |
| 169 panic("NOT IMPLEMENTED") | |
| 170 } | |
| 171 | |
| 172 func (d *txnDsImpl) Count(q rds.Query) (int, error) { | |
| 173 return count(d.Run(q.KeysOnly())) | |
| 174 } | |
| 175 | |
| 176 func count(itr rds.Iterator) (ret int, err error) { | |
| 177 for _, err = itr.Next(nil); err != nil; _, err = itr.Next(nil) { | |
| 178 ret++ | |
| 179 } | |
| 180 if err == rds.ErrQueryDone { | |
| 181 err = nil | |
| 182 } | |
| 183 return | |
| 184 } | |
| OLD | NEW |