Chromium Code Reviews| 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 "strings" |
| 11 "time" | 11 "time" |
| 12 | 12 |
| 13 ds "github.com/luci/gae/service/datastore" | 13 ds "github.com/luci/gae/service/datastore" |
| 14 "github.com/luci/gae/service/datastore/dskey" | |
| 15 "github.com/luci/gae/service/datastore/serialize" | 14 "github.com/luci/gae/service/datastore/serialize" |
| 16 "github.com/luci/luci-go/common/cmpbin" | 15 "github.com/luci/luci-go/common/cmpbin" |
| 17 ) | 16 ) |
| 18 | 17 |
| 19 func init() { | 18 func init() { |
| 20 serializationDeterministic = true | 19 serializationDeterministic = true |
| 21 serialize.WritePropertyMapDeterministic = true | 20 serialize.WritePropertyMapDeterministic = true |
| 22 } | 21 } |
| 23 | 22 |
| 24 var NEXT_STR = "NEXT MARKER" | 23 var NEXT_STR = "NEXT MARKER" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 52 for len(stuff) > 0 && stuff[0] != NEXT { | 51 for len(stuff) > 0 && stuff[0] != NEXT { |
| 53 ret[pname] = append(ret[pname], prop(nom())) | 52 ret[pname] = append(ret[pname], prop(nom())) |
| 54 } | 53 } |
| 55 } | 54 } |
| 56 nom() | 55 nom() |
| 57 } | 56 } |
| 58 | 57 |
| 59 return ret | 58 return ret |
| 60 } | 59 } |
| 61 | 60 |
| 62 func nq(kind_ns ...string) ds.Query { | 61 func nq(kindMaybe ...string) *ds.Query { |
| 63 » if len(kind_ns) == 2 { | 62 » kind := "Foo" |
| 64 » » return &queryImpl{kind: kind_ns[0], ns: kind_ns[1]} | 63 » if len(kindMaybe) == 1 { |
| 65 » } else if len(kind_ns) == 1 { | 64 » » kind = kindMaybe[0] |
| 66 » » return &queryImpl{kind: kind_ns[0], ns: "ns"} | |
| 67 } | 65 } |
| 68 » return &queryImpl{kind: "Foo", ns: "ns"} | 66 » return ds.NewQuery(kind) |
|
iannucci
2015/09/18 04:31:52
Querys no longer embed the namespace and appid ins
| |
| 69 } | 67 } |
| 70 | 68 |
| 71 func indx(kind string, orders ...string) *ds.IndexDefinition { | 69 func indx(kind string, orders ...string) *ds.IndexDefinition { |
| 72 ancestor := false | 70 ancestor := false |
| 73 if kind[len(kind)-1] == '!' { | 71 if kind[len(kind)-1] == '!' { |
| 74 ancestor = true | 72 ancestor = true |
| 75 kind = kind[:len(kind)-1] | 73 kind = kind[:len(kind)-1] |
| 76 } | 74 } |
| 77 ret := &ds.IndexDefinition{Kind: kind, Ancestor: ancestor} | 75 ret := &ds.IndexDefinition{Kind: kind, Ancestor: ancestor} |
| 78 for _, o := range orders { | 76 for _, o := range orders { |
| 79 dir := ds.ASCENDING | 77 dir := ds.ASCENDING |
| 80 if o[0] == '-' { | 78 if o[0] == '-' { |
| 81 dir = ds.DESCENDING | 79 dir = ds.DESCENDING |
| 82 o = o[1:] | 80 o = o[1:] |
| 83 } | 81 } |
| 84 ret.SortBy = append(ret.SortBy, ds.IndexColumn{Property: o, Dire ction: dir}) | 82 ret.SortBy = append(ret.SortBy, ds.IndexColumn{Property: o, Dire ction: dir}) |
| 85 } | 83 } |
| 86 return ret | 84 return ret |
| 87 } | 85 } |
| 88 | 86 |
| 89 var ( | 87 var ( |
| 90 prop = ds.MkProperty | 88 prop = ds.MkProperty |
| 91 propNI = ds.MkPropertyNI | 89 propNI = ds.MkPropertyNI |
| 92 ) | 90 ) |
| 93 | 91 |
| 94 func key(kind string, id interface{}, parent ...ds.Key) ds.Key { | 92 func key(elems ...interface{}) *ds.Key { |
|
iannucci
2015/09/18 04:31:52
I had like 5 copies of this function all over the
| |
| 95 » p := ds.Key(nil) | 93 » return ds.MakeKey(globalAppID, "ns", elems...) |
| 96 » if len(parent) > 0 { | |
| 97 » » p = parent[0] | |
| 98 » } | |
| 99 » switch x := id.(type) { | |
| 100 » case string: | |
| 101 » » return dskey.New(globalAppID, "ns", kind, x, 0, p) | |
| 102 » case int: | |
| 103 » » return dskey.New(globalAppID, "ns", kind, "", int64(x), p) | |
| 104 » default: | |
| 105 » » return dskey.New(globalAppID, "ns", kind, "invalid", 100, p) | |
| 106 » } | |
| 107 } | 94 } |
| 108 | 95 |
| 109 // cat is a convenience method for concatenating anything with an underlying | 96 // cat is a convenience method for concatenating anything with an underlying |
| 110 // byte representation into a single []byte. | 97 // byte representation into a single []byte. |
| 111 func cat(bytethings ...interface{}) []byte { | 98 func cat(bytethings ...interface{}) []byte { |
| 112 buf := &bytes.Buffer{} | 99 buf := &bytes.Buffer{} |
| 113 for _, thing := range bytethings { | 100 for _, thing := range bytethings { |
| 114 switch x := thing.(type) { | 101 switch x := thing.(type) { |
| 115 case int64: | 102 case int64: |
| 116 cmpbin.WriteInt(buf, x) | 103 cmpbin.WriteInt(buf, x) |
| 117 case int: | 104 case int: |
| 118 cmpbin.WriteInt(buf, int64(x)) | 105 cmpbin.WriteInt(buf, int64(x)) |
| 119 case uint64: | 106 case uint64: |
| 120 cmpbin.WriteUint(buf, x) | 107 cmpbin.WriteUint(buf, x) |
| 121 case uint: | 108 case uint: |
| 122 cmpbin.WriteUint(buf, uint64(x)) | 109 cmpbin.WriteUint(buf, uint64(x)) |
| 123 case float64: | 110 case float64: |
| 124 cmpbin.WriteFloat64(buf, x) | 111 cmpbin.WriteFloat64(buf, x) |
| 125 case byte: | 112 case byte: |
| 126 buf.WriteByte(x) | 113 buf.WriteByte(x) |
| 127 case ds.PropertyType: | 114 case ds.PropertyType: |
| 128 buf.WriteByte(byte(x)) | 115 buf.WriteByte(byte(x)) |
| 129 case string: | 116 case string: |
| 130 cmpbin.WriteString(buf, x) | 117 cmpbin.WriteString(buf, x) |
| 131 case []byte: | 118 case []byte: |
| 132 buf.Write(x) | 119 buf.Write(x) |
| 133 case time.Time: | 120 case time.Time: |
| 134 serialize.WriteTime(buf, x) | 121 serialize.WriteTime(buf, x) |
| 135 » » case ds.Key: | 122 » » case *ds.Key: |
| 136 serialize.WriteKey(buf, serialize.WithoutContext, x) | 123 serialize.WriteKey(buf, serialize.WithoutContext, x) |
| 137 case *ds.IndexDefinition: | 124 case *ds.IndexDefinition: |
| 138 serialize.WriteIndexDefinition(buf, *x) | 125 serialize.WriteIndexDefinition(buf, *x) |
| 139 case ds.Property: | 126 case ds.Property: |
| 140 serialize.WriteProperty(buf, serialize.WithoutContext, x ) | 127 serialize.WriteProperty(buf, serialize.WithoutContext, x ) |
| 141 default: | 128 default: |
| 142 panic(fmt.Errorf("I don't know how to deal with %T: %#v" , thing, thing)) | 129 panic(fmt.Errorf("I don't know how to deal with %T: %#v" , thing, thing)) |
| 143 } | 130 } |
| 144 } | 131 } |
| 145 ret := buf.Bytes() | 132 ret := buf.Bytes() |
| 146 if ret == nil { | 133 if ret == nil { |
| 147 ret = []byte{} | 134 ret = []byte{} |
| 148 } | 135 } |
| 149 return ret | 136 return ret |
| 150 } | 137 } |
| 151 | 138 |
| 152 func icat(bytethings ...interface{}) []byte { | 139 func icat(bytethings ...interface{}) []byte { |
| 153 ret := cat(bytethings...) | 140 ret := cat(bytethings...) |
| 154 for i := range ret { | 141 for i := range ret { |
| 155 ret[i] ^= 0xFF | 142 ret[i] ^= 0xFF |
| 156 } | 143 } |
| 157 return ret | 144 return ret |
| 158 } | 145 } |
| 159 | 146 |
| 160 func sat(bytethings ...interface{}) string { | 147 func sat(bytethings ...interface{}) string { |
| 161 return string(cat(bytethings...)) | 148 return string(cat(bytethings...)) |
| 162 } | 149 } |
| OLD | NEW |