| 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 "sync" | 11 "sync" |
| 11 "sync/atomic" | 12 "sync/atomic" |
| 12 | 13 |
| 13 ds "github.com/luci/gae/service/datastore" | 14 ds "github.com/luci/gae/service/datastore" |
| 14 "github.com/luci/gae/service/datastore/serialize" | 15 "github.com/luci/gae/service/datastore/serialize" |
| 15 "github.com/luci/luci-go/common/errors" | 16 "github.com/luci/luci-go/common/errors" |
| 16 "golang.org/x/net/context" | 17 "golang.org/x/net/context" |
| 17 ) | 18 ) |
| 18 | 19 |
| 19 //////////////////////////////// dataStoreData ///////////////////////////////// | 20 //////////////////////////////// dataStoreData ///////////////////////////////// |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 d.Lock() | 73 d.Lock() |
| 73 defer d.Unlock() | 74 defer d.Unlock() |
| 74 | 75 |
| 75 if always { | 76 if always { |
| 76 d.snap = nil | 77 d.snap = nil |
| 77 } else { | 78 } else { |
| 78 d.snap = d.head.Snapshot() | 79 d.snap = d.head.Snapshot() |
| 79 } | 80 } |
| 80 } | 81 } |
| 81 | 82 |
| 82 func (d *dataStoreData) addIndexes(ns string, idxs []*ds.IndexDefinition) { | 83 func (d *dataStoreData) addIndexes(idxs []*ds.IndexDefinition) { |
| 83 d.Lock() | 84 d.Lock() |
| 84 defer d.Unlock() | 85 defer d.Unlock() |
| 85 » addIndexes(d.head, d.aid, ns, idxs) | 86 » addIndexes(d.head, d.aid, idxs) |
| 86 } | 87 } |
| 87 | 88 |
| 88 func (d *dataStoreData) setAutoIndex(enable bool) { | 89 func (d *dataStoreData) setAutoIndex(enable bool) { |
| 89 d.Lock() | 90 d.Lock() |
| 90 defer d.Unlock() | 91 defer d.Unlock() |
| 91 d.autoIndex = enable | 92 d.autoIndex = enable |
| 92 } | 93 } |
| 93 | 94 |
| 94 func (d *dataStoreData) maybeAutoIndex(err error) bool { | 95 func (d *dataStoreData) maybeAutoIndex(err error) bool { |
| 95 mi, ok := err.(*ErrMissingIndex) | 96 mi, ok := err.(*ErrMissingIndex) |
| 96 if !ok { | 97 if !ok { |
| 97 return false | 98 return false |
| 98 } | 99 } |
| 99 | 100 |
| 100 d.rwlock.RLock() | 101 d.rwlock.RLock() |
| 101 ai := d.autoIndex | 102 ai := d.autoIndex |
| 102 d.rwlock.RUnlock() | 103 d.rwlock.RUnlock() |
| 103 | 104 |
| 104 if !ai { | 105 if !ai { |
| 105 return false | 106 return false |
| 106 } | 107 } |
| 107 | 108 |
| 108 » d.addIndexes(mi.ns, []*ds.IndexDefinition{mi.Missing}) | 109 » d.addIndexes([]*ds.IndexDefinition{mi.Missing}) |
| 109 return true | 110 return true |
| 110 } | 111 } |
| 111 | 112 |
| 112 func (d *dataStoreData) setDisableSpecialEntities(enabled bool) { | 113 func (d *dataStoreData) setDisableSpecialEntities(enabled bool) { |
| 113 d.Lock() | 114 d.Lock() |
| 114 defer d.Unlock() | 115 defer d.Unlock() |
| 115 d.disableSpecialEntities = true | 116 d.disableSpecialEntities = true |
| 116 } | 117 } |
| 117 | 118 |
| 118 func (d *dataStoreData) getDisableSpecialEntities() bool { | 119 func (d *dataStoreData) getDisableSpecialEntities() bool { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 func (d *dataStoreData) catchupIndexes() { | 159 func (d *dataStoreData) catchupIndexes() { |
| 159 d.rwlock.Lock() | 160 d.rwlock.Lock() |
| 160 defer d.rwlock.Unlock() | 161 defer d.rwlock.Unlock() |
| 161 if d.snap == nil { | 162 if d.snap == nil { |
| 162 // we're 'always consistent' | 163 // we're 'always consistent' |
| 163 return | 164 return |
| 164 } | 165 } |
| 165 d.snap = d.head.Snapshot() | 166 d.snap = d.head.Snapshot() |
| 166 } | 167 } |
| 167 | 168 |
| 169 func (d *dataStoreData) namespaces() []string { |
| 170 d.rwlock.Lock() |
| 171 defer d.rwlock.Unlock() |
| 172 |
| 173 return namespaces(d.head) |
| 174 } |
| 175 |
| 168 /////////////////////////// indexes(dataStoreData) //////////////////////////// | 176 /////////////////////////// indexes(dataStoreData) //////////////////////////// |
| 169 | 177 |
| 170 func groupMetaKey(key *ds.Key) []byte { | 178 func groupMetaKey(key *ds.Key) []byte { |
| 171 return keyBytes(ds.NewKey("", "", "__entity_group__", "", 1, key.Root())
) | 179 return keyBytes(ds.NewKey("", "", "__entity_group__", "", 1, key.Root())
) |
| 172 } | 180 } |
| 173 | 181 |
| 174 func groupIDsKey(key *ds.Key) []byte { | 182 func groupIDsKey(key *ds.Key) []byte { |
| 175 return keyBytes(ds.NewKey("", "", "__entity_group_ids__", "", 1, key.Roo
t())) | 183 return keyBytes(ds.NewKey("", "", "__entity_group_ids__", "", 1, key.Roo
t())) |
| 176 } | 184 } |
| 177 | 185 |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 } | 571 } |
| 564 | 572 |
| 565 func keyBytes(key *ds.Key) []byte { | 573 func keyBytes(key *ds.Key) []byte { |
| 566 return serialize.ToBytes(ds.MkProperty(key)) | 574 return serialize.ToBytes(ds.MkProperty(key)) |
| 567 } | 575 } |
| 568 | 576 |
| 569 func rpm(data []byte) (ds.PropertyMap, error) { | 577 func rpm(data []byte) (ds.PropertyMap, error) { |
| 570 return serialize.ReadPropertyMap(bytes.NewBuffer(data), | 578 return serialize.ReadPropertyMap(bytes.NewBuffer(data), |
| 571 serialize.WithContext, "", "") | 579 serialize.WithContext, "", "") |
| 572 } | 580 } |
| 581 |
| 582 func namespaces(store *memStore) []string { |
| 583 var namespaces []string |
| 584 for _, c := range store.GetCollectionNames() { |
| 585 ns, has := trimPrefix(c, "ents:") |
| 586 if !has { |
| 587 if len(namespaces) > 0 { |
| 588 break |
| 589 } |
| 590 continue |
| 591 } |
| 592 namespaces = append(namespaces, ns) |
| 593 } |
| 594 return namespaces |
| 595 } |
| 596 |
| 597 func trimPrefix(v, p string) (string, bool) { |
| 598 if strings.HasPrefix(v, p) { |
| 599 return v[len(p):], true |
| 600 } |
| 601 return v, false |
| 602 } |
| OLD | NEW |