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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 return keyBytes(ds.WithoutContext, | 56 return keyBytes(ds.WithoutContext, |
57 ds.NewKey("", "", "__entity_group_ids__", "", 1, ds.KeyRoot(key)
)) | 57 ds.NewKey("", "", "__entity_group_ids__", "", 1, ds.KeyRoot(key)
)) |
58 } | 58 } |
59 | 59 |
60 func rootIDsKey(kind string) []byte { | 60 func rootIDsKey(kind string) []byte { |
61 return keyBytes(ds.WithoutContext, | 61 return keyBytes(ds.WithoutContext, |
62 ds.NewKey("", "", "__entity_root_ids__", kind, 0, nil)) | 62 ds.NewKey("", "", "__entity_root_ids__", kind, 0, nil)) |
63 } | 63 } |
64 | 64 |
65 func curVersion(ents *memCollection, key []byte) int64 { | 65 func curVersion(ents *memCollection, key []byte) int64 { |
66 » if v := ents.Get(key); v != nil { | 66 » if ents != nil { |
67 » » pm, err := rpm(v) | 67 » » if v := ents.Get(key); v != nil { |
68 » » if err != nil { | 68 » » » pm, err := rpm(v) |
69 » » » panic(err) // memory corruption | 69 » » » if err != nil { |
| 70 » » » » panic(err) // memory corruption |
| 71 » » » } |
| 72 » » » pl, ok := pm["__version__"] |
| 73 » » » if ok && len(pl) > 0 && pl[0].Type() == ds.PTInt { |
| 74 » » » » return pl[0].Value().(int64) |
| 75 » » » } |
| 76 » » » panic(fmt.Errorf("__version__ property missing or wrong:
%v", pm)) |
70 } | 77 } |
71 pl, ok := pm["__version__"] | |
72 if ok && len(pl) > 0 && pl[0].Type() == ds.PTInt { | |
73 return pl[0].Value().(int64) | |
74 } | |
75 panic(fmt.Errorf("__version__ property missing or wrong: %v", pm
)) | |
76 } | 78 } |
77 return 0 | 79 return 0 |
78 } | 80 } |
79 | 81 |
80 func incrementLocked(ents *memCollection, key []byte) int64 { | 82 func incrementLocked(ents *memCollection, key []byte) int64 { |
81 ret := curVersion(ents, key) + 1 | 83 ret := curVersion(ents, key) + 1 |
82 buf := &bytes.Buffer{} | 84 buf := &bytes.Buffer{} |
83 ds.PropertyMap{"__version__": {ds.MkPropertyNI(ret)}}.Write( | 85 ds.PropertyMap{"__version__": {ds.MkPropertyNI(ret)}}.Write( |
84 buf, ds.WithContext) | 86 buf, ds.WithContext) |
85 ents.Set(key, buf.Bytes()) | 87 ents.Set(key, buf.Bytes()) |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 | 412 |
411 func rpm(data []byte) (ds.PropertyMap, error) { | 413 func rpm(data []byte) (ds.PropertyMap, error) { |
412 ret := ds.PropertyMap{} | 414 ret := ds.PropertyMap{} |
413 err := ret.Read(bytes.NewBuffer(data), ds.WithContext, "", "") | 415 err := ret.Read(bytes.NewBuffer(data), ds.WithContext, "", "") |
414 return ret, err | 416 return ret, err |
415 } | 417 } |
416 | 418 |
417 type keyitem interface { | 419 type keyitem interface { |
418 Key() ds.Key | 420 Key() ds.Key |
419 } | 421 } |
OLD | NEW |