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 "strings" | 10 "strings" |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 if amt <= 0 { | 208 if amt <= 0 { |
209 panic(fmt.Errorf("incrementLocked called with bad `amt`: %d", am
t)) | 209 panic(fmt.Errorf("incrementLocked called with bad `amt`: %d", am
t)) |
210 } | 210 } |
211 ret := curVersion(ents, key) + 1 | 211 ret := curVersion(ents, key) + 1 |
212 ents.Set(key, serialize.ToBytes(ds.PropertyMap{ | 212 ents.Set(key, serialize.ToBytes(ds.PropertyMap{ |
213 "__version__": {ds.MkPropertyNI(ret + int64(amt-1))}, | 213 "__version__": {ds.MkPropertyNI(ret + int64(amt-1))}, |
214 })) | 214 })) |
215 return ret | 215 return ret |
216 } | 216 } |
217 | 217 |
218 func (d *dataStoreData) allocateIDs(incomplete *ds.Key, n int) (int64, error) { | 218 func (d *dataStoreData) allocateIDs(keys []*ds.Key) error { |
219 » d.Lock() | 219 » start, err := func() (int64, error) { |
220 » defer d.Unlock() | 220 » » d.Lock() |
| 221 » » defer d.Unlock() |
221 | 222 |
222 » ents := d.head.GetOrCreateCollection("ents:" + incomplete.Namespace()) | 223 » » // Allocate a set of IDs in our key namespace. We know that "key
s" has at |
223 » return d.allocateIDsLocked(ents, incomplete, n) | 224 » » // least one entry and that all keys share the same namespace an
d entty |
| 225 » » // type because checkfilter has already asserted this. |
| 226 » » baseKey := keys[0] |
| 227 » » ents := d.head.GetOrCreateCollection("ents:" + baseKey.Namespace
()) |
| 228 » » start, err := d.allocateIDsLocked(ents, baseKey, len(keys)) |
| 229 » » if err != nil { |
| 230 » » » return -1, err |
| 231 » » } |
| 232 » » return start, nil |
| 233 » }() |
| 234 » if err != nil { |
| 235 » » return err |
| 236 » } |
| 237 |
| 238 » // Update the keys in our "keys" slice with their new IDs. |
| 239 » for i, k := range keys { |
| 240 » » keys[i] = ds.NewKey(k.AppID(), k.Namespace(), k.Kind(), "", star
t+int64(i), k.Parent()) |
| 241 » } |
| 242 » return nil |
224 } | 243 } |
225 | 244 |
226 func (d *dataStoreData) allocateIDsLocked(ents memCollection, incomplete *ds.Key
, n int) (int64, error) { | 245 func (d *dataStoreData) allocateIDsLocked(ents memCollection, incomplete *ds.Key
, n int) (int64, error) { |
227 if d.disableSpecialEntities { | 246 if d.disableSpecialEntities { |
228 return 0, errors.New("disableSpecialEntities is true so allocate
IDs is disabled") | 247 return 0, errors.New("disableSpecialEntities is true so allocate
IDs is disabled") |
229 } | 248 } |
230 | 249 |
231 idKey := []byte(nil) | 250 idKey := []byte(nil) |
232 if incomplete.Parent() == nil { | 251 if incomplete.Parent() == nil { |
233 idKey = rootIDsKey(incomplete.Kind()) | 252 idKey = rootIDsKey(incomplete.Kind()) |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 } | 603 } |
585 return namespaces | 604 return namespaces |
586 } | 605 } |
587 | 606 |
588 func trimPrefix(v, p string) (string, bool) { | 607 func trimPrefix(v, p string) (string, bool) { |
589 if strings.HasPrefix(v, p) { | 608 if strings.HasPrefix(v, p) { |
590 return v[len(p):], true | 609 return v[len(p):], true |
591 } | 610 } |
592 return v, false | 611 return v, false |
593 } | 612 } |
OLD | NEW |