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