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 » rds "github.com/luci/gae/service/rawdatastore" | 12 » ds "github.com/luci/gae/service/datastore" |
13 "github.com/luci/luci-go/common/cmpbin" | 13 "github.com/luci/luci-go/common/cmpbin" |
14 ) | 14 ) |
15 | 15 |
16 type kv struct{ k, v []byte } | 16 type kv struct{ k, v []byte } |
17 | 17 |
18 func indx(kind string, orders ...string) *qIndex { | 18 func indx(kind string, orders ...string) *qIndex { |
19 ancestor := false | 19 ancestor := false |
20 if kind[len(kind)-1] == '!' { | 20 if kind[len(kind)-1] == '!' { |
21 ancestor = true | 21 ancestor = true |
22 kind = kind[:len(kind)-1] | 22 kind = kind[:len(kind)-1] |
23 } | 23 } |
24 ret := &qIndex{kind, ancestor, nil} | 24 ret := &qIndex{kind, ancestor, nil} |
25 for _, o := range orders { | 25 for _, o := range orders { |
26 dir := qASC | 26 dir := qASC |
27 if o[0] == '-' { | 27 if o[0] == '-' { |
28 dir = qDEC | 28 dir = qDEC |
29 o = o[1:] | 29 o = o[1:] |
30 } | 30 } |
31 ret.sortby = append(ret.sortby, qSortBy{o, dir}) | 31 ret.sortby = append(ret.sortby, qSortBy{o, dir}) |
32 } | 32 } |
33 return ret | 33 return ret |
34 } | 34 } |
35 | 35 |
36 var ( | 36 var ( |
37 » prop = rds.MkProperty | 37 » prop = ds.MkProperty |
38 » propNI = rds.MkPropertyNI | 38 » propNI = ds.MkPropertyNI |
39 ) | 39 ) |
40 | 40 |
41 func key(kind string, id interface{}, parent ...rds.Key) rds.Key { | 41 func key(kind string, id interface{}, parent ...ds.Key) ds.Key { |
42 » p := rds.Key(nil) | 42 » p := ds.Key(nil) |
43 if len(parent) > 0 { | 43 if len(parent) > 0 { |
44 p = parent[0] | 44 p = parent[0] |
45 } | 45 } |
46 switch x := id.(type) { | 46 switch x := id.(type) { |
47 case string: | 47 case string: |
48 » » return rds.NewKey(globalAppID, "ns", kind, x, 0, p) | 48 » » return ds.NewKey(globalAppID, "ns", kind, x, 0, p) |
49 case int: | 49 case int: |
50 » » return rds.NewKey(globalAppID, "ns", kind, "", int64(x), p) | 50 » » return ds.NewKey(globalAppID, "ns", kind, "", int64(x), p) |
51 default: | 51 default: |
52 panic(fmt.Errorf("what the %T: %v", id, id)) | 52 panic(fmt.Errorf("what the %T: %v", id, id)) |
53 } | 53 } |
54 } | 54 } |
55 | 55 |
56 // cat is a convenience method for concatenating anything with an underlying | 56 // cat is a convenience method for concatenating anything with an underlying |
57 // byte representation into a single []byte. | 57 // byte representation into a single []byte. |
58 func cat(bytethings ...interface{}) []byte { | 58 func cat(bytethings ...interface{}) []byte { |
59 buf := &bytes.Buffer{} | 59 buf := &bytes.Buffer{} |
60 for _, thing := range bytethings { | 60 for _, thing := range bytethings { |
61 switch x := thing.(type) { | 61 switch x := thing.(type) { |
62 case int64: | 62 case int64: |
63 cmpbin.WriteInt(buf, x) | 63 cmpbin.WriteInt(buf, x) |
64 case int: | 64 case int: |
65 cmpbin.WriteInt(buf, int64(x)) | 65 cmpbin.WriteInt(buf, int64(x)) |
66 case uint64: | 66 case uint64: |
67 cmpbin.WriteUint(buf, x) | 67 cmpbin.WriteUint(buf, x) |
68 case uint: | 68 case uint: |
69 cmpbin.WriteUint(buf, uint64(x)) | 69 cmpbin.WriteUint(buf, uint64(x)) |
70 case float64: | 70 case float64: |
71 cmpbin.WriteFloat64(buf, x) | 71 cmpbin.WriteFloat64(buf, x) |
72 case byte: | 72 case byte: |
73 buf.WriteByte(x) | 73 buf.WriteByte(x) |
74 » » case rds.PropertyType: | 74 » » case ds.PropertyType: |
75 buf.WriteByte(byte(x)) | 75 buf.WriteByte(byte(x)) |
76 case string: | 76 case string: |
77 cmpbin.WriteString(buf, x) | 77 cmpbin.WriteString(buf, x) |
78 case []byte: | 78 case []byte: |
79 buf.Write(x) | 79 buf.Write(x) |
80 case time.Time: | 80 case time.Time: |
81 » » » rds.WriteTime(buf, x) | 81 » » » ds.WriteTime(buf, x) |
82 » » case rds.Key: | 82 » » case ds.Key: |
83 » » » rds.WriteKey(buf, rds.WithoutContext, x) | 83 » » » ds.WriteKey(buf, ds.WithoutContext, x) |
84 case *qIndex: | 84 case *qIndex: |
85 x.WriteBinary(buf) | 85 x.WriteBinary(buf) |
86 default: | 86 default: |
87 panic(fmt.Errorf("I don't know how to deal with %T: %#v"
, thing, thing)) | 87 panic(fmt.Errorf("I don't know how to deal with %T: %#v"
, thing, thing)) |
88 } | 88 } |
89 } | 89 } |
90 ret := buf.Bytes() | 90 ret := buf.Bytes() |
91 if ret == nil { | 91 if ret == nil { |
92 ret = []byte{} | 92 ret = []byte{} |
93 } | 93 } |
94 return ret | 94 return ret |
95 } | 95 } |
96 | 96 |
97 func icat(bytethings ...interface{}) []byte { | 97 func icat(bytethings ...interface{}) []byte { |
98 ret := cat(bytethings...) | 98 ret := cat(bytethings...) |
99 for i := range ret { | 99 for i := range ret { |
100 ret[i] ^= 0xFF | 100 ret[i] ^= 0xFF |
101 } | 101 } |
102 return ret | 102 return ret |
103 } | 103 } |
104 | 104 |
105 func sat(bytethings ...interface{}) string { | 105 func sat(bytethings ...interface{}) string { |
106 return string(cat(bytethings...)) | 106 return string(cat(bytethings...)) |
107 } | 107 } |
OLD | NEW |