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