| 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 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 if key.Incomplete() { | 242 if key.Incomplete() { |
| 243 id, err := d.allocateIDsLocked(ents, key, 1) | 243 id, err := d.allocateIDsLocked(ents, key, 1) |
| 244 if err != nil { | 244 if err != nil { |
| 245 return key, err | 245 return key, err |
| 246 } | 246 } |
| 247 key = ds.NewKey(key.AppID(), key.Namespace(), key.Kind(), "", id
, key.Parent()) | 247 key = ds.NewKey(key.AppID(), key.Namespace(), key.Kind(), "", id
, key.Parent()) |
| 248 } | 248 } |
| 249 return key, nil | 249 return key, nil |
| 250 } | 250 } |
| 251 | 251 |
| 252 func (d *dataStoreData) putMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.Pu
tMultiCB) { | 252 func (d *dataStoreData) putMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.Pu
tMultiCB) error { |
| 253 ns := keys[0].Namespace() | 253 ns := keys[0].Namespace() |
| 254 | 254 |
| 255 for i, k := range keys { | 255 for i, k := range keys { |
| 256 pmap, _ := vals[i].Save(false) | 256 pmap, _ := vals[i].Save(false) |
| 257 dataBytes := serialize.ToBytes(pmap) | 257 dataBytes := serialize.ToBytes(pmap) |
| 258 | 258 |
| 259 k, err := func() (ret *ds.Key, err error) { | 259 k, err := func() (ret *ds.Key, err error) { |
| 260 d.Lock() | 260 d.Lock() |
| 261 defer d.Unlock() | 261 defer d.Unlock() |
| 262 | 262 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 275 if old != nil { | 275 if old != nil { |
| 276 if oldPM, err = rpm(old); err != nil { | 276 if oldPM, err = rpm(old); err != nil { |
| 277 return | 277 return |
| 278 } | 278 } |
| 279 } | 279 } |
| 280 ents.Set(keyBytes(ret), dataBytes) | 280 ents.Set(keyBytes(ret), dataBytes) |
| 281 updateIndexes(d.head, ret, oldPM, pmap) | 281 updateIndexes(d.head, ret, oldPM, pmap) |
| 282 return | 282 return |
| 283 }() | 283 }() |
| 284 if cb != nil { | 284 if cb != nil { |
| 285 » » » cb(k, err) | 285 » » » if err := cb(k, err); err != nil { |
| 286 » » » » if err == ds.Stop { |
| 287 » » » » » return nil |
| 288 » » » » } |
| 289 » » » » return err |
| 290 » » » } |
| 286 } | 291 } |
| 287 } | 292 } |
| 293 return nil |
| 288 } | 294 } |
| 289 | 295 |
| 290 func getMultiInner(keys []*ds.Key, cb ds.GetMultiCB, getColl func() (*memCollect
ion, error)) error { | 296 func getMultiInner(keys []*ds.Key, cb ds.GetMultiCB, getColl func() (*memCollect
ion, error)) error { |
| 291 ents, err := getColl() | 297 ents, err := getColl() |
| 292 if err != nil { | 298 if err != nil { |
| 293 return err | 299 return err |
| 294 } | 300 } |
| 295 if ents == nil { | 301 if ents == nil { |
| 296 for range keys { | 302 for range keys { |
| 297 cb(nil, ds.ErrNoSuchEntity) | 303 cb(nil, ds.ErrNoSuchEntity) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 311 } | 317 } |
| 312 | 318 |
| 313 func (d *dataStoreData) getMulti(keys []*ds.Key, cb ds.GetMultiCB) error { | 319 func (d *dataStoreData) getMulti(keys []*ds.Key, cb ds.GetMultiCB) error { |
| 314 return getMultiInner(keys, cb, func() (*memCollection, error) { | 320 return getMultiInner(keys, cb, func() (*memCollection, error) { |
| 315 s := d.takeSnapshot() | 321 s := d.takeSnapshot() |
| 316 | 322 |
| 317 return s.GetCollection("ents:" + keys[0].Namespace()), nil | 323 return s.GetCollection("ents:" + keys[0].Namespace()), nil |
| 318 }) | 324 }) |
| 319 } | 325 } |
| 320 | 326 |
| 321 func (d *dataStoreData) delMulti(keys []*ds.Key, cb ds.DeleteMultiCB) { | 327 func (d *dataStoreData) delMulti(keys []*ds.Key, cb ds.DeleteMultiCB) error { |
| 322 ns := keys[0].Namespace() | 328 ns := keys[0].Namespace() |
| 323 | 329 |
| 324 hasEntsInNS := func() bool { | 330 hasEntsInNS := func() bool { |
| 325 d.Lock() | 331 d.Lock() |
| 326 defer d.Unlock() | 332 defer d.Unlock() |
| 327 return d.mutableEntsLocked(ns) != nil | 333 return d.mutableEntsLocked(ns) != nil |
| 328 }() | 334 }() |
| 329 | 335 |
| 330 if hasEntsInNS { | 336 if hasEntsInNS { |
| 331 for _, k := range keys { | 337 for _, k := range keys { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 344 oldPM, err := rpm(old) | 350 oldPM, err := rpm(old) |
| 345 if err != nil { | 351 if err != nil { |
| 346 return err | 352 return err |
| 347 } | 353 } |
| 348 ents.Delete(kb) | 354 ents.Delete(kb) |
| 349 updateIndexes(d.head, k, oldPM, nil) | 355 updateIndexes(d.head, k, oldPM, nil) |
| 350 } | 356 } |
| 351 return nil | 357 return nil |
| 352 }() | 358 }() |
| 353 if cb != nil { | 359 if cb != nil { |
| 354 » » » » cb(err) | 360 » » » » if err := cb(err); err != nil { |
| 361 » » » » » if err == ds.Stop { |
| 362 » » » » » » return nil |
| 363 » » » » » } |
| 364 » » » » » return err |
| 365 » » » » } |
| 355 } | 366 } |
| 356 } | 367 } |
| 357 } else if cb != nil { | 368 } else if cb != nil { |
| 358 for range keys { | 369 for range keys { |
| 359 » » » cb(nil) | 370 » » » if err := cb(nil); err != nil { |
| 371 » » » » if err == ds.Stop { |
| 372 » » » » » return nil |
| 373 » » » » } |
| 374 » » » » return err |
| 375 » » » } |
| 360 } | 376 } |
| 361 } | 377 } |
| 378 return nil |
| 362 } | 379 } |
| 363 | 380 |
| 364 func (d *dataStoreData) canApplyTxn(obj memContextObj) bool { | 381 func (d *dataStoreData) canApplyTxn(obj memContextObj) bool { |
| 365 // TODO(riannucci): implement with Flush/FlushRevert for persistance. | 382 // TODO(riannucci): implement with Flush/FlushRevert for persistance. |
| 366 | 383 |
| 367 txn := obj.(*txnDataStoreData) | 384 txn := obj.(*txnDataStoreData) |
| 368 for rk, muts := range txn.muts { | 385 for rk, muts := range txn.muts { |
| 369 if len(muts) == 0 { // read-only | 386 if len(muts) == 0 { // read-only |
| 370 continue | 387 continue |
| 371 } | 388 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 388 } | 405 } |
| 389 | 406 |
| 390 func (d *dataStoreData) applyTxn(c context.Context, obj memContextObj) { | 407 func (d *dataStoreData) applyTxn(c context.Context, obj memContextObj) { |
| 391 txn := obj.(*txnDataStoreData) | 408 txn := obj.(*txnDataStoreData) |
| 392 for _, muts := range txn.muts { | 409 for _, muts := range txn.muts { |
| 393 if len(muts) == 0 { // read-only | 410 if len(muts) == 0 { // read-only |
| 394 continue | 411 continue |
| 395 } | 412 } |
| 396 // TODO(riannucci): refactor to do just 1 putMulti, and 1 delMul
ti | 413 // TODO(riannucci): refactor to do just 1 putMulti, and 1 delMul
ti |
| 397 for _, m := range muts { | 414 for _, m := range muts { |
| 398 err := error(nil) | |
| 399 k := m.key | 415 k := m.key |
| 400 if m.data == nil { | 416 if m.data == nil { |
| 401 » » » » d.delMulti([]*ds.Key{k}, | 417 » » » » impossible(d.delMulti([]*ds.Key{k}, |
| 402 » » » » » func(e error) { err = e }) | 418 » » » » » func(e error) error { return e })) |
| 403 } else { | 419 } else { |
| 404 » » » » d.putMulti([]*ds.Key{m.key}, []ds.PropertyMap{m.
data}, | 420 » » » » impossible(d.putMulti([]*ds.Key{m.key}, []ds.Pro
pertyMap{m.data}, |
| 405 » » » » » func(_ *ds.Key, e error) { err = e }) | 421 » » » » » func(_ *ds.Key, e error) error { return
e })) |
| 406 } | 422 } |
| 407 impossible(err) | |
| 408 } | 423 } |
| 409 } | 424 } |
| 410 } | 425 } |
| 411 | 426 |
| 412 func (d *dataStoreData) mkTxn(o *ds.TransactionOptions) memContextObj { | 427 func (d *dataStoreData) mkTxn(o *ds.TransactionOptions) memContextObj { |
| 413 return &txnDataStoreData{ | 428 return &txnDataStoreData{ |
| 414 // alias to the main datastore's so that testing code can have p
rimitive | 429 // alias to the main datastore's so that testing code can have p
rimitive |
| 415 // access to break features inside of transactions. | 430 // access to break features inside of transactions. |
| 416 parent: d, | 431 parent: d, |
| 417 isXG: o != nil && o.XG, | 432 isXG: o != nil && o.XG, |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 } | 572 } |
| 558 | 573 |
| 559 func keyBytes(key *ds.Key) []byte { | 574 func keyBytes(key *ds.Key) []byte { |
| 560 return serialize.ToBytes(ds.MkProperty(key)) | 575 return serialize.ToBytes(ds.MkProperty(key)) |
| 561 } | 576 } |
| 562 | 577 |
| 563 func rpm(data []byte) (ds.PropertyMap, error) { | 578 func rpm(data []byte) (ds.PropertyMap, error) { |
| 564 return serialize.ReadPropertyMap(bytes.NewBuffer(data), | 579 return serialize.ReadPropertyMap(bytes.NewBuffer(data), |
| 565 serialize.WithContext, "", "") | 580 serialize.WithContext, "", "") |
| 566 } | 581 } |
| OLD | NEW |