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