| 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 "errors" | 9 "errors" |
| 10 "fmt" | 10 "fmt" |
| 11 | 11 |
| 12 "github.com/golang/protobuf/proto" | 12 "github.com/golang/protobuf/proto" |
| 13 » "github.com/luci/luci-go/common/funnybase" | 13 » "github.com/luci/luci-go/common/cmpbin" |
| 14 "github.com/mjibson/goon" | 14 "github.com/mjibson/goon" |
| 15 | 15 |
| 16 "appengine" | 16 "appengine" |
| 17 "appengine/datastore" | 17 "appengine/datastore" |
| 18 "appengine_internal" | 18 "appengine_internal" |
| 19 basepb "appengine_internal/base" | 19 basepb "appengine_internal/base" |
| 20 ) | 20 ) |
| 21 | 21 |
| 22 const keyNumToksReasonableLimit = 50 | 22 const keyNumToksReasonableLimit = 50 |
| 23 | 23 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 func keyFromByteString(nso nsOption, d string, ns string) (*datastore.Key, error
) { | 107 func keyFromByteString(nso nsOption, d string, ns string) (*datastore.Key, error
) { |
| 108 return readKey(bytes.NewBufferString(d), nso, ns) | 108 return readKey(bytes.NewBufferString(d), nso, ns) |
| 109 } | 109 } |
| 110 | 110 |
| 111 func writeKey(buf *bytes.Buffer, nso nsOption, k *datastore.Key) { | 111 func writeKey(buf *bytes.Buffer, nso nsOption, k *datastore.Key) { |
| 112 // namespace ++ #tokens ++ [tok.kind ++ tok.stringID ++ tok.intID?]* | 112 // namespace ++ #tokens ++ [tok.kind ++ tok.stringID ++ tok.intID?]* |
| 113 namespace, toks := keyToToks(k) | 113 namespace, toks := keyToToks(k) |
| 114 if nso == withNS { | 114 if nso == withNS { |
| 115 writeString(buf, namespace) | 115 writeString(buf, namespace) |
| 116 } | 116 } |
| 117 » funnybase.WriteUint(buf, uint64(len(toks))) | 117 » cmpbin.WriteUint(buf, uint64(len(toks))) |
| 118 for _, tok := range toks { | 118 for _, tok := range toks { |
| 119 writeString(buf, tok.kind) | 119 writeString(buf, tok.kind) |
| 120 writeString(buf, tok.stringID) | 120 writeString(buf, tok.stringID) |
| 121 if tok.stringID == "" { | 121 if tok.stringID == "" { |
| 122 » » » funnybase.WriteUint(buf, tok.intID) | 122 » » » cmpbin.WriteUint(buf, tok.intID) |
| 123 } | 123 } |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 | 126 |
| 127 func readKey(buf *bytes.Buffer, nso nsOption, ns string) (*datastore.Key, error)
{ | 127 func readKey(buf *bytes.Buffer, nso nsOption, ns string) (*datastore.Key, error)
{ |
| 128 namespace := ns | 128 namespace := ns |
| 129 if nso == withNS { | 129 if nso == withNS { |
| 130 err := error(nil) | 130 err := error(nil) |
| 131 if namespace, err = readString(buf); err != nil { | 131 if namespace, err = readString(buf); err != nil { |
| 132 return nil, err | 132 return nil, err |
| 133 } | 133 } |
| 134 } | 134 } |
| 135 | 135 |
| 136 » numToks, err := funnybase.ReadUint(buf) | 136 » numToks, _, err := cmpbin.ReadUint(buf) |
| 137 if err != nil { | 137 if err != nil { |
| 138 return nil, err | 138 return nil, err |
| 139 } | 139 } |
| 140 if numToks > keyNumToksReasonableLimit { | 140 if numToks > keyNumToksReasonableLimit { |
| 141 return nil, fmt.Errorf("readKey: tried to decode huge key of len
gth %d", numToks) | 141 return nil, fmt.Errorf("readKey: tried to decode huge key of len
gth %d", numToks) |
| 142 } | 142 } |
| 143 | 143 |
| 144 toks := make([]*keyTok, numToks) | 144 toks := make([]*keyTok, numToks) |
| 145 for i := uint64(0); i < numToks; i++ { | 145 for i := uint64(0); i < numToks; i++ { |
| 146 tok := &keyTok{} | 146 tok := &keyTok{} |
| 147 if tok.kind, err = readString(buf); err != nil { | 147 if tok.kind, err = readString(buf); err != nil { |
| 148 return nil, err | 148 return nil, err |
| 149 } | 149 } |
| 150 if tok.stringID, err = readString(buf); err != nil { | 150 if tok.stringID, err = readString(buf); err != nil { |
| 151 return nil, err | 151 return nil, err |
| 152 } | 152 } |
| 153 if tok.stringID == "" { | 153 if tok.stringID == "" { |
| 154 » » » if tok.intID, err = funnybase.ReadUint(buf); err != nil
{ | 154 » » » if tok.intID, _, err = cmpbin.ReadUint(buf); err != nil
{ |
| 155 return nil, err | 155 return nil, err |
| 156 } | 156 } |
| 157 if tok.intID == 0 { | 157 if tok.intID == 0 { |
| 158 return nil, errors.New("readKey: decoded key wit
h empty stringID and empty intID") | 158 return nil, errors.New("readKey: decoded key wit
h empty stringID and empty intID") |
| 159 } | 159 } |
| 160 } | 160 } |
| 161 toks[i] = tok | 161 toks[i] = tok |
| 162 } | 162 } |
| 163 | 163 |
| 164 return toksToKey(namespace, toks), nil | 164 return toksToKey(namespace, toks), nil |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 // keyCouldBeValid is like keyValid, but it allows for the possibility that the | 227 // keyCouldBeValid is like keyValid, but it allows for the possibility that the |
| 228 // last token of the key is Incomplete(). It returns true if the Key will become | 228 // last token of the key is Incomplete(). It returns true if the Key will become |
| 229 // valid once it recieves an automatically-assigned ID. | 229 // valid once it recieves an automatically-assigned ID. |
| 230 func keyCouldBeValid(ns string, k *datastore.Key, opt keyValidOption) bool { | 230 func keyCouldBeValid(ns string, k *datastore.Key, opt keyValidOption) bool { |
| 231 // adds an id to k if it's incomplete. | 231 // adds an id to k if it's incomplete. |
| 232 if k.Incomplete() { | 232 if k.Incomplete() { |
| 233 k = newKey(ns, k.Kind(), "", 1, k.Parent()) | 233 k = newKey(ns, k.Kind(), "", 1, k.Parent()) |
| 234 } | 234 } |
| 235 return keyValid(ns, k, opt) | 235 return keyValid(ns, k, opt) |
| 236 } | 236 } |
| OLD | NEW |