| 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 "bytes" | 8 "bytes" |
| 9 "fmt" | 9 "fmt" |
| 10 "sync" | 10 "sync" |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 if ok && len(pl) > 0 && pl[0].Type() == ds.PTInt { | 102 if ok && len(pl) > 0 && pl[0].Type() == ds.PTInt { |
| 103 return pl[0].Value().(int64) | 103 return pl[0].Value().(int64) |
| 104 } | 104 } |
| 105 | 105 |
| 106 memoryCorruption(fmt.Errorf("__version__ property missin
g or wrong: %v", pm)) | 106 memoryCorruption(fmt.Errorf("__version__ property missin
g or wrong: %v", pm)) |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 return 0 | 109 return 0 |
| 110 } | 110 } |
| 111 | 111 |
| 112 func incrementLocked(ents *memCollection, key []byte) int64 { | 112 func incrementLocked(ents *memCollection, key []byte, amt int) int64 { |
| 113 » if amt <= 0 { |
| 114 » » panic(fmt.Errorf("incrementLocked called with bad `amt`: %d", am
t)) |
| 115 » } |
| 113 ret := curVersion(ents, key) + 1 | 116 ret := curVersion(ents, key) + 1 |
| 114 ents.Set(key, serialize.ToBytes(ds.PropertyMap{ | 117 ents.Set(key, serialize.ToBytes(ds.PropertyMap{ |
| 115 » » "__version__": {ds.MkPropertyNI(ret)}, | 118 » » "__version__": {ds.MkPropertyNI(ret + int64(amt-1))}, |
| 116 })) | 119 })) |
| 117 return ret | 120 return ret |
| 118 } | 121 } |
| 119 | 122 |
| 120 func (d *dataStoreData) entsKeyLocked(key *ds.Key) (*memCollection, *ds.Key) { | 123 func (d *dataStoreData) mutableEnts(ns string) *memCollection { |
| 121 » coll := "ents:" + key.Namespace() | 124 » d.Lock() |
| 125 » defer d.Unlock() |
| 126 |
| 127 » coll := "ents:" + ns |
| 122 ents := d.head.GetCollection(coll) | 128 ents := d.head.GetCollection(coll) |
| 123 if ents == nil { | 129 if ents == nil { |
| 124 ents = d.head.SetCollection(coll, nil) | 130 ents = d.head.SetCollection(coll, nil) |
| 125 } | 131 } |
| 132 return ents |
| 133 } |
| 126 | 134 |
| 135 func (d *dataStoreData) allocateIDs(incomplete *ds.Key, n int) int64 { |
| 136 ents := d.mutableEnts(incomplete.Namespace()) |
| 137 |
| 138 d.Lock() |
| 139 defer d.Unlock() |
| 140 return d.allocateIDsLocked(ents, incomplete, n) |
| 141 } |
| 142 |
| 143 func (d *dataStoreData) allocateIDsLocked(ents *memCollection, incomplete *ds.Ke
y, n int) int64 { |
| 144 idKey := []byte(nil) |
| 145 if incomplete.Parent() == nil { |
| 146 idKey = rootIDsKey(incomplete.Last().Kind) |
| 147 } else { |
| 148 idKey = groupIDsKey(incomplete) |
| 149 } |
| 150 return incrementLocked(ents, idKey, n) |
| 151 } |
| 152 |
| 153 func (d *dataStoreData) fixKeyLocked(ents *memCollection, key *ds.Key) *ds.Key { |
| 127 if key.Incomplete() { | 154 if key.Incomplete() { |
| 128 » » idKey := []byte(nil) | 155 » » id := d.allocateIDsLocked(ents, key, 1) |
| 129 » » if key.Parent() == nil { | |
| 130 » » » idKey = rootIDsKey(key.Last().Kind) | |
| 131 » » } else { | |
| 132 » » » idKey = groupIDsKey(key) | |
| 133 » » } | |
| 134 » » id := incrementLocked(ents, idKey) | |
| 135 key = ds.NewKey(key.AppID(), key.Namespace(), key.Last().Kind, "
", id, key.Parent()) | 156 key = ds.NewKey(key.AppID(), key.Namespace(), key.Last().Kind, "
", id, key.Parent()) |
| 136 } | 157 } |
| 137 | 158 » return key |
| 138 » return ents, key | |
| 139 } | 159 } |
| 140 | 160 |
| 141 func (d *dataStoreData) putMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.Pu
tMultiCB) { | 161 func (d *dataStoreData) putMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.Pu
tMultiCB) { |
| 162 ents := d.mutableEnts(keys[0].Namespace()) |
| 163 |
| 142 for i, k := range keys { | 164 for i, k := range keys { |
| 143 pmap, _ := vals[i].Save(false) | 165 pmap, _ := vals[i].Save(false) |
| 144 dataBytes := serialize.ToBytes(pmap) | 166 dataBytes := serialize.ToBytes(pmap) |
| 145 | 167 |
| 146 k, err := func() (ret *ds.Key, err error) { | 168 k, err := func() (ret *ds.Key, err error) { |
| 147 » » » d.rwlock.Lock() | 169 » » » d.Lock() |
| 148 » » » defer d.rwlock.Unlock() | 170 » » » defer d.Unlock() |
| 149 | 171 |
| 150 » » » ents, ret := d.entsKeyLocked(k) | 172 » » » ret = d.fixKeyLocked(ents, k) |
| 151 » » » incrementLocked(ents, groupMetaKey(ret)) | 173 » » » incrementLocked(ents, groupMetaKey(ret), 1) |
| 152 | 174 |
| 153 old := ents.Get(keyBytes(ret)) | 175 old := ents.Get(keyBytes(ret)) |
| 154 oldPM := ds.PropertyMap(nil) | 176 oldPM := ds.PropertyMap(nil) |
| 155 if old != nil { | 177 if old != nil { |
| 156 if oldPM, err = rpmWoCtx(old, ret.Namespace());
err != nil { | 178 if oldPM, err = rpmWoCtx(old, ret.Namespace());
err != nil { |
| 157 return | 179 return |
| 158 } | 180 } |
| 159 } | 181 } |
| 160 updateIndexes(d.head, ret, oldPM, pmap) | 182 updateIndexes(d.head, ret, oldPM, pmap) |
| 161 ents.Set(keyBytes(ret), dataBytes) | 183 ents.Set(keyBytes(ret), dataBytes) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 } | 227 } |
| 206 ns := keys[0].Namespace() | 228 ns := keys[0].Namespace() |
| 207 | 229 |
| 208 d.rwlock.Lock() | 230 d.rwlock.Lock() |
| 209 defer d.rwlock.Unlock() | 231 defer d.rwlock.Unlock() |
| 210 | 232 |
| 211 ents := d.head.GetCollection("ents:" + ns) | 233 ents := d.head.GetCollection("ents:" + ns) |
| 212 | 234 |
| 213 for i, k := range keys { | 235 for i, k := range keys { |
| 214 if ents != nil { | 236 if ents != nil { |
| 215 » » » incrementLocked(ents, groupMetaKey(k)) | 237 » » » incrementLocked(ents, groupMetaKey(k), 1) |
| 216 kb := toDel[i] | 238 kb := toDel[i] |
| 217 if old := ents.Get(kb); old != nil { | 239 if old := ents.Get(kb); old != nil { |
| 218 oldPM, err := rpmWoCtx(old, ns) | 240 oldPM, err := rpmWoCtx(old, ns) |
| 219 if err != nil { | 241 if err != nil { |
| 220 if cb != nil { | 242 if cb != nil { |
| 221 cb(err) | 243 cb(err) |
| 222 } | 244 } |
| 223 continue | 245 continue |
| 224 } | 246 } |
| 225 updateIndexes(d.head, k, oldPM, nil) | 247 updateIndexes(d.head, k, oldPM, nil) |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 td.muts[rk] = []txnMutation{} | 399 td.muts[rk] = []txnMutation{} |
| 378 } | 400 } |
| 379 if !getOnly { | 401 if !getOnly { |
| 380 td.muts[rk] = append(td.muts[rk], txnMutation{key, data}) | 402 td.muts[rk] = append(td.muts[rk], txnMutation{key, data}) |
| 381 } | 403 } |
| 382 | 404 |
| 383 return nil | 405 return nil |
| 384 } | 406 } |
| 385 | 407 |
| 386 func (td *txnDataStoreData) putMulti(keys []*ds.Key, vals []ds.PropertyMap, cb d
s.PutMultiCB) { | 408 func (td *txnDataStoreData) putMulti(keys []*ds.Key, vals []ds.PropertyMap, cb d
s.PutMultiCB) { |
| 409 ents := td.parent.mutableEnts(keys[0].Namespace()) |
| 410 |
| 387 for i, k := range keys { | 411 for i, k := range keys { |
| 388 func() { | 412 func() { |
| 389 td.parent.Lock() | 413 td.parent.Lock() |
| 390 defer td.parent.Unlock() | 414 defer td.parent.Unlock() |
| 391 » » » _, k = td.parent.entsKeyLocked(k) | 415 » » » k = td.parent.fixKeyLocked(ents, k) |
| 392 }() | 416 }() |
| 393 err := td.writeMutation(false, k, vals[i]) | 417 err := td.writeMutation(false, k, vals[i]) |
| 394 if cb != nil { | 418 if cb != nil { |
| 395 cb(k, err) | 419 cb(k, err) |
| 396 } | 420 } |
| 397 } | 421 } |
| 398 } | 422 } |
| 399 | 423 |
| 400 func (td *txnDataStoreData) getMulti(keys []*ds.Key, cb ds.GetMultiCB) error { | 424 func (td *txnDataStoreData) getMulti(keys []*ds.Key, cb ds.GetMultiCB) error { |
| 401 return getMultiInner(keys, cb, func() (*memCollection, error) { | 425 return getMultiInner(keys, cb, func() (*memCollection, error) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 426 | 450 |
| 427 func rpmWoCtx(data []byte, ns string) (ds.PropertyMap, error) { | 451 func rpmWoCtx(data []byte, ns string) (ds.PropertyMap, error) { |
| 428 return serialize.ReadPropertyMap(bytes.NewBuffer(data), | 452 return serialize.ReadPropertyMap(bytes.NewBuffer(data), |
| 429 serialize.WithoutContext, globalAppID, ns) | 453 serialize.WithoutContext, globalAppID, ns) |
| 430 } | 454 } |
| 431 | 455 |
| 432 func rpm(data []byte) (ds.PropertyMap, error) { | 456 func rpm(data []byte) (ds.PropertyMap, error) { |
| 433 return serialize.ReadPropertyMap(bytes.NewBuffer(data), | 457 return serialize.ReadPropertyMap(bytes.NewBuffer(data), |
| 434 serialize.WithContext, "", "") | 458 serialize.WithContext, "", "") |
| 435 } | 459 } |
| OLD | NEW |