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 "time" | 11 "time" |
11 | 12 |
12 ds "github.com/luci/gae/service/datastore" | 13 ds "github.com/luci/gae/service/datastore" |
13 "github.com/luci/gae/service/datastore/dskey" | 14 "github.com/luci/gae/service/datastore/dskey" |
14 "github.com/luci/gae/service/datastore/serialize" | 15 "github.com/luci/gae/service/datastore/serialize" |
15 "github.com/luci/luci-go/common/cmpbin" | 16 "github.com/luci/luci-go/common/cmpbin" |
16 ) | 17 ) |
17 | 18 |
18 type kv struct{ k, v []byte } | 19 func init() { |
| 20 » serializationDeterministic = true |
| 21 » serialize.WritePropertyMapDeterministic = true |
| 22 } |
| 23 |
| 24 var NEXT_STR = "NEXT MARKER" |
| 25 var NEXT = &NEXT_STR |
| 26 |
| 27 // Use like: |
| 28 // pmap( |
| 29 // "prop", "val", 0, 100, NEXT, |
| 30 // "other", "val", 0, 100, NEXT, |
| 31 // ) |
| 32 // |
| 33 func pmap(stuff ...interface{}) ds.PropertyMap { |
| 34 » ret := ds.PropertyMap{} |
| 35 |
| 36 » nom := func() interface{} { |
| 37 » » if len(stuff) > 0 { |
| 38 » » » ret := stuff[0] |
| 39 » » » stuff = stuff[1:] |
| 40 » » » return ret |
| 41 » » } |
| 42 » » return nil |
| 43 » } |
| 44 |
| 45 » for len(stuff) > 0 { |
| 46 » » pname := nom().(string) |
| 47 » » if pname[0] == '$' || (strings.HasPrefix(pname, "__") && strings
.HasSuffix(pname, "__")) { |
| 48 » » » for len(stuff) > 0 && stuff[0] != NEXT { |
| 49 » » » » ret[pname] = append(ret[pname], propNI(nom())) |
| 50 » » » } |
| 51 » » } else { |
| 52 » » » for len(stuff) > 0 && stuff[0] != NEXT { |
| 53 » » » » ret[pname] = append(ret[pname], prop(nom())) |
| 54 » » » } |
| 55 » » } |
| 56 » » nom() |
| 57 » } |
| 58 |
| 59 » return ret |
| 60 } |
| 61 |
| 62 func nq(kind_ns ...string) ds.Query { |
| 63 » if len(kind_ns) == 2 { |
| 64 » » return &queryImpl{kind: kind_ns[0], ns: kind_ns[1]} |
| 65 » } else if len(kind_ns) == 1 { |
| 66 » » return &queryImpl{kind: kind_ns[0], ns: "ns"} |
| 67 » } |
| 68 » return &queryImpl{kind: "Foo", ns: "ns"} |
| 69 } |
19 | 70 |
20 func indx(kind string, orders ...string) *ds.IndexDefinition { | 71 func indx(kind string, orders ...string) *ds.IndexDefinition { |
21 ancestor := false | 72 ancestor := false |
22 if kind[len(kind)-1] == '!' { | 73 if kind[len(kind)-1] == '!' { |
23 ancestor = true | 74 ancestor = true |
24 kind = kind[:len(kind)-1] | 75 kind = kind[:len(kind)-1] |
25 } | 76 } |
26 ret := &ds.IndexDefinition{Kind: kind, Ancestor: ancestor} | 77 ret := &ds.IndexDefinition{Kind: kind, Ancestor: ancestor} |
27 for _, o := range orders { | 78 for _, o := range orders { |
28 dir := ds.ASCENDING | 79 dir := ds.ASCENDING |
(...skipping 15 matching lines...) Expand all Loading... |
44 p := ds.Key(nil) | 95 p := ds.Key(nil) |
45 if len(parent) > 0 { | 96 if len(parent) > 0 { |
46 p = parent[0] | 97 p = parent[0] |
47 } | 98 } |
48 switch x := id.(type) { | 99 switch x := id.(type) { |
49 case string: | 100 case string: |
50 return dskey.New(globalAppID, "ns", kind, x, 0, p) | 101 return dskey.New(globalAppID, "ns", kind, x, 0, p) |
51 case int: | 102 case int: |
52 return dskey.New(globalAppID, "ns", kind, "", int64(x), p) | 103 return dskey.New(globalAppID, "ns", kind, "", int64(x), p) |
53 default: | 104 default: |
54 » » panic(fmt.Errorf("what the %T: %v", id, id)) | 105 » » return dskey.New(globalAppID, "ns", kind, "invalid", 100, p) |
55 } | 106 } |
56 } | 107 } |
57 | 108 |
58 // cat is a convenience method for concatenating anything with an underlying | 109 // cat is a convenience method for concatenating anything with an underlying |
59 // byte representation into a single []byte. | 110 // byte representation into a single []byte. |
60 func cat(bytethings ...interface{}) []byte { | 111 func cat(bytethings ...interface{}) []byte { |
61 buf := &bytes.Buffer{} | 112 buf := &bytes.Buffer{} |
62 for _, thing := range bytethings { | 113 for _, thing := range bytethings { |
63 switch x := thing.(type) { | 114 switch x := thing.(type) { |
64 case int64: | 115 case int64: |
(...skipping 13 matching lines...) Expand all Loading... |
78 case string: | 129 case string: |
79 cmpbin.WriteString(buf, x) | 130 cmpbin.WriteString(buf, x) |
80 case []byte: | 131 case []byte: |
81 buf.Write(x) | 132 buf.Write(x) |
82 case time.Time: | 133 case time.Time: |
83 serialize.WriteTime(buf, x) | 134 serialize.WriteTime(buf, x) |
84 case ds.Key: | 135 case ds.Key: |
85 serialize.WriteKey(buf, serialize.WithoutContext, x) | 136 serialize.WriteKey(buf, serialize.WithoutContext, x) |
86 case *ds.IndexDefinition: | 137 case *ds.IndexDefinition: |
87 serialize.WriteIndexDefinition(buf, *x) | 138 serialize.WriteIndexDefinition(buf, *x) |
| 139 case ds.Property: |
| 140 serialize.WriteProperty(buf, serialize.WithoutContext, x
) |
88 default: | 141 default: |
89 panic(fmt.Errorf("I don't know how to deal with %T: %#v"
, thing, thing)) | 142 panic(fmt.Errorf("I don't know how to deal with %T: %#v"
, thing, thing)) |
90 } | 143 } |
91 } | 144 } |
92 ret := buf.Bytes() | 145 ret := buf.Bytes() |
93 if ret == nil { | 146 if ret == nil { |
94 ret = []byte{} | 147 ret = []byte{} |
95 } | 148 } |
96 return ret | 149 return ret |
97 } | 150 } |
98 | 151 |
99 func icat(bytethings ...interface{}) []byte { | 152 func icat(bytethings ...interface{}) []byte { |
100 ret := cat(bytethings...) | 153 ret := cat(bytethings...) |
101 for i := range ret { | 154 for i := range ret { |
102 ret[i] ^= 0xFF | 155 ret[i] ^= 0xFF |
103 } | 156 } |
104 return ret | 157 return ret |
105 } | 158 } |
106 | 159 |
107 func sat(bytethings ...interface{}) string { | 160 func sat(bytethings ...interface{}) string { |
108 return string(cat(bytethings...)) | 161 return string(cat(bytethings...)) |
109 } | 162 } |
OLD | NEW |