| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package datastore | 5 package datastore |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "encoding/base64" | 9 "encoding/base64" |
| 10 "encoding/json" | 10 "encoding/json" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 a, b := k.ID(), other.ID() | 52 a, b := k.ID(), other.ID() |
| 53 return a.Less(&b) | 53 return a.Less(&b) |
| 54 } | 54 } |
| 55 | 55 |
| 56 // KeyContext is the context in which a key is generated. | 56 // KeyContext is the context in which a key is generated. |
| 57 type KeyContext struct { | 57 type KeyContext struct { |
| 58 AppID string | 58 AppID string |
| 59 Namespace string | 59 Namespace string |
| 60 } | 60 } |
| 61 | 61 |
| 62 // MkKeyContext is a helper function to create a new KeyContext. |
| 63 // |
| 64 // It is preferable to field-based struct initialization because, as a function, |
| 65 // it has the ability to enforce an exact number of parameters. |
| 66 func MkKeyContext(appID, namespace string) KeyContext { |
| 67 return KeyContext{AppID: appID, Namespace: namespace} |
| 68 } |
| 69 |
| 62 // Matches returns true iff the AppID and Namespace parameters are the same for | 70 // Matches returns true iff the AppID and Namespace parameters are the same for |
| 63 // the two KeyContext instances. | 71 // the two KeyContext instances. |
| 64 func (kc KeyContext) Matches(o KeyContext) bool { | 72 func (kc KeyContext) Matches(o KeyContext) bool { |
| 65 return (kc.AppID == o.AppID && kc.Namespace == o.Namespace) | 73 return (kc.AppID == o.AppID && kc.Namespace == o.Namespace) |
| 66 } | 74 } |
| 67 | 75 |
| 68 // NewKeyToks creates a new Key. It is the Key implementation returned from | 76 // NewKeyToks creates a new Key. It is the Key implementation returned from |
| 69 // the various PropertyMap serialization routines, as well as the native key | 77 // the various PropertyMap serialization routines, as well as the native key |
| 70 // implementation for the in-memory implementation of gae. | 78 // implementation for the in-memory implementation of gae. |
| 71 // | 79 // |
| (...skipping 23 matching lines...) Expand all Loading... |
| 95 copy(newToks, toks) | 103 copy(newToks, toks) |
| 96 newToks = append(newToks, KeyTok{kind, intID, stringID}) | 104 newToks = append(newToks, KeyTok{kind, intID, stringID}) |
| 97 return &Key{kc, newToks} | 105 return &Key{kc, newToks} |
| 98 } | 106 } |
| 99 | 107 |
| 100 // MakeKey is a convenience function for manufacturing a *Key. It should only | 108 // MakeKey is a convenience function for manufacturing a *Key. It should only |
| 101 // be used when elems... is known statically (e.g. in the code) to be correct. | 109 // be used when elems... is known statically (e.g. in the code) to be correct. |
| 102 // | 110 // |
| 103 // elems is pairs of (string, string|int|int32|int64) pairs, which correspond to | 111 // elems is pairs of (string, string|int|int32|int64) pairs, which correspond to |
| 104 // Kind/id pairs. Example: | 112 // Kind/id pairs. Example: |
| 105 // KeyContext{"aid", "namespace"}.MakeKey("Parent", 1, "Child", "id") | 113 // MkKeyContext("aid", "namespace").MakeKey("Parent", 1, "Child", "id") |
| 106 // | 114 // |
| 107 // Would create the key: | 115 // Would create the key: |
| 108 // aid:namespace:/Parent,1/Child,id | 116 // aid:namespace:/Parent,1/Child,id |
| 109 // | 117 // |
| 110 // If elems is not parsable (e.g. wrong length, wrong types, etc.) this method | 118 // If elems is not parsable (e.g. wrong length, wrong types, etc.) this method |
| 111 // will panic. | 119 // will panic. |
| 112 // | 120 // |
| 113 // See MakeKey for a version of this function which automatically | 121 // See MakeKey for a version of this function which automatically |
| 114 // provides aid and ns. | 122 // provides aid and ns. |
| 115 func (kc KeyContext) MakeKey(elems ...interface{}) *Key { | 123 func (kc KeyContext) MakeKey(elems ...interface{}) *Key { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 b, err := base64.URLEncoding.DecodeString(encoded) | 179 b, err := base64.URLEncoding.DecodeString(encoded) |
| 172 if err != nil { | 180 if err != nil { |
| 173 return | 181 return |
| 174 } | 182 } |
| 175 | 183 |
| 176 r := &pb.Reference{} | 184 r := &pb.Reference{} |
| 177 if err = proto.Unmarshal(b, r); err != nil { | 185 if err = proto.Unmarshal(b, r); err != nil { |
| 178 return | 186 return |
| 179 } | 187 } |
| 180 | 188 |
| 181 » ret.kc = KeyContext{ | 189 » ret.kc = MkKeyContext(r.GetApp(), r.GetNameSpace()) |
| 182 » » AppID: r.GetApp(), | |
| 183 » » Namespace: r.GetNameSpace(), | |
| 184 » } | |
| 185 ret.toks = make([]KeyTok, len(r.Path.Element)) | 190 ret.toks = make([]KeyTok, len(r.Path.Element)) |
| 186 for i, e := range r.Path.Element { | 191 for i, e := range r.Path.Element { |
| 187 ret.toks[i] = KeyTok{ | 192 ret.toks[i] = KeyTok{ |
| 188 Kind: e.GetType(), | 193 Kind: e.GetType(), |
| 189 IntID: e.GetId(), | 194 IntID: e.GetId(), |
| 190 StringID: e.GetName(), | 195 StringID: e.GetName(), |
| 191 } | 196 } |
| 192 } | 197 } |
| 193 return | 198 return |
| 194 } | 199 } |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 for _, t := range k.toks { | 520 for _, t := range k.toks { |
| 516 ret += int64(len(t.Kind)) | 521 ret += int64(len(t.Kind)) |
| 517 if t.StringID != "" { | 522 if t.StringID != "" { |
| 518 ret += int64(len(t.StringID)) | 523 ret += int64(len(t.StringID)) |
| 519 } else { | 524 } else { |
| 520 ret += 8 | 525 ret += 8 |
| 521 } | 526 } |
| 522 } | 527 } |
| 523 return ret | 528 return ret |
| 524 } | 529 } |
| OLD | NEW |