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 "sort" | 10 "sort" |
11 | 11 |
12 ds "github.com/luci/gae/service/datastore" | 12 ds "github.com/luci/gae/service/datastore" |
13 "github.com/luci/gae/service/datastore/serialize" | 13 "github.com/luci/gae/service/datastore/serialize" |
14 "github.com/luci/gkvlite" | 14 "github.com/luci/gkvlite" |
| 15 "github.com/luci/luci-go/common/stringset" |
15 ) | 16 ) |
16 | 17 |
17 type qIndexSlice []*ds.IndexDefinition | 18 type qIndexSlice []*ds.IndexDefinition |
18 | 19 |
19 func (s qIndexSlice) Len() int { return len(s) } | 20 func (s qIndexSlice) Len() int { return len(s) } |
20 func (s qIndexSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | 21 func (s qIndexSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
21 func (s qIndexSlice) Less(i, j int) bool { return s[i].Less(s[j]) } | 22 func (s qIndexSlice) Less(i, j int) bool { return s[i].Less(s[j]) } |
22 | 23 |
23 func defaultIndexes(kind string, pmap ds.PropertyMap) []*ds.IndexDefinition { | 24 func defaultIndexes(kind string, pmap ds.PropertyMap) []*ds.IndexDefinition { |
24 ret := make(qIndexSlice, 0, 2*len(pmap)+1) | 25 ret := make(qIndexSlice, 0, 2*len(pmap)+1) |
(...skipping 29 matching lines...) Expand all Loading... |
54 func (s serializedPvals) Len() int { return len(s) } | 55 func (s serializedPvals) Len() int { return len(s) } |
55 func (s serializedPvals) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | 56 func (s serializedPvals) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
56 func (s serializedPvals) Less(i, j int) bool { return bytes.Compare(s[i], s[j])
< 0 } | 57 func (s serializedPvals) Less(i, j int) bool { return bytes.Compare(s[i], s[j])
< 0 } |
57 | 58 |
58 // prop name -> [<serialized DSProperty>, ...] | 59 // prop name -> [<serialized DSProperty>, ...] |
59 // includes special values '__key__' and '__ancestor__' which contains all of | 60 // includes special values '__key__' and '__ancestor__' which contains all of |
60 // the ancestor entries for this key. | 61 // the ancestor entries for this key. |
61 type serializedIndexablePmap map[string]serializedPvals | 62 type serializedIndexablePmap map[string]serializedPvals |
62 | 63 |
63 func serializeRow(vals []ds.Property) serializedPvals { | 64 func serializeRow(vals []ds.Property) serializedPvals { |
64 » dups := map[string]struct{}{} | 65 » dups := stringset.New(0) |
65 ret := make(serializedPvals, 0, len(vals)) | 66 ret := make(serializedPvals, 0, len(vals)) |
66 for _, v := range vals { | 67 for _, v := range vals { |
67 if v.IndexSetting() == ds.NoIndex { | 68 if v.IndexSetting() == ds.NoIndex { |
68 continue | 69 continue |
69 } | 70 } |
70 data := serialize.ToBytes(v.ForIndex()) | 71 data := serialize.ToBytes(v.ForIndex()) |
71 dataS := string(data) | 72 dataS := string(data) |
72 » » if _, ok := dups[dataS]; ok { | 73 » » if !dups.Add(dataS) { |
73 continue | 74 continue |
74 } | 75 } |
75 dups[dataS] = struct{}{} | |
76 ret = append(ret, data) | 76 ret = append(ret, data) |
77 } | 77 } |
78 return ret | 78 return ret |
79 } | 79 } |
80 | 80 |
81 func partiallySerialize(k ds.Key, pm ds.PropertyMap) (ret serializedIndexablePma
p) { | 81 func partiallySerialize(k ds.Key, pm ds.PropertyMap) (ret serializedIndexablePma
p) { |
82 ret = make(serializedIndexablePmap, len(pm)+2) | 82 ret = make(serializedIndexablePmap, len(pm)+2) |
83 if k == nil { | 83 if k == nil { |
84 impossible(fmt.Errorf("key to partiallySerialize is nil")) | 84 impossible(fmt.Errorf("key to partiallySerialize is nil")) |
85 } | 85 } |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 compIdx := []*ds.IndexDefinition{} | 306 compIdx := []*ds.IndexDefinition{} |
307 walkCompIdxs(store, nil, func(i *ds.IndexDefinition) bool { | 307 walkCompIdxs(store, nil, func(i *ds.IndexDefinition) bool { |
308 compIdx = append(compIdx, i) | 308 compIdx = append(compIdx, i) |
309 return true | 309 return true |
310 }) | 310 }) |
311 | 311 |
312 mergeIndexes(key.Namespace(), store, | 312 mergeIndexes(key.Namespace(), store, |
313 indexEntriesWithBuiltins(key, oldEnt, compIdx), | 313 indexEntriesWithBuiltins(key, oldEnt, compIdx), |
314 indexEntriesWithBuiltins(key, newEnt, compIdx)) | 314 indexEntriesWithBuiltins(key, newEnt, compIdx)) |
315 } | 315 } |
OLD | NEW |