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" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 withNS nsOption = true | 97 withNS nsOption = true |
98 noNS = false | 98 noNS = false |
99 ) | 99 ) |
100 | 100 |
101 func keyBytes(nso nsOption, k *datastore.Key) []byte { | 101 func keyBytes(nso nsOption, k *datastore.Key) []byte { |
102 buf := &bytes.Buffer{} | 102 buf := &bytes.Buffer{} |
103 writeKey(buf, nso, k) | 103 writeKey(buf, nso, k) |
104 return buf.Bytes() | 104 return buf.Bytes() |
105 } | 105 } |
106 | 106 |
107 func keyFromByteString(nso nsOption, d string) (*datastore.Key, error) { | 107 func keyFromByteString(nso nsOption, d string, ns string) (*datastore.Key, error
) { |
108 » return readKey(bytes.NewBufferString(d), nso) | 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 funnybase.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 funnybase.WriteUint(buf, tok.intID) |
123 } | 123 } |
124 } | 124 } |
125 } | 125 } |
126 | 126 |
127 func readKey(buf *bytes.Buffer, nso nsOption) (*datastore.Key, error) { | 127 func readKey(buf *bytes.Buffer, nso nsOption, ns string) (*datastore.Key, error)
{ |
128 » namespace := "" | 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 := funnybase.ReadUint(buf) |
137 if err != nil { | 137 if err != nil { |
138 return nil, err | 138 return nil, err |
(...skipping 88 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 |