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 bigtable | 5 package bigtable |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "crypto/sha256" | 9 "crypto/sha256" |
10 "encoding/base64" | 10 "encoding/base64" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 // NOTE: There is a "legacy" period of time when row keys will NOT include a | 100 // NOTE: There is a "legacy" period of time when row keys will NOT include a |
101 // count. Since these sort before row keys with a count, row key order will be | 101 // count. Since these sort before row keys with a count, row key order will be |
102 // maintained. These row keys will have a count value of "0". | 102 // maintained. These row keys will have a count value of "0". |
103 type rowKey struct { | 103 type rowKey struct { |
104 pathHash []byte | 104 pathHash []byte |
105 index int64 | 105 index int64 |
106 count int64 | 106 count int64 |
107 } | 107 } |
108 | 108 |
109 // newRowKey generates the row key matching a given entry path and index. | 109 // newRowKey generates the row key matching a given entry path and index. |
110 func newRowKey(path string, index, count int64) *rowKey { | 110 func newRowKey(project, path string, index, count int64) *rowKey { |
111 » pathHash := sha256.Sum256([]byte(path)) | 111 » h := sha256.New() |
| 112 |
| 113 » // TODO(dnj): Remove this conditional when non-project data is aged off. |
| 114 » if project != "" { |
| 115 » » _, _ = h.Write([]byte(project)) |
| 116 » » _, _ = h.Write([]byte("/")) |
| 117 » } |
| 118 |
| 119 » _, _ = h.Write([]byte(path)) |
112 return &rowKey{ | 120 return &rowKey{ |
113 » » pathHash: pathHash[:], | 121 » » pathHash: h.Sum(nil), |
114 index: index, | 122 index: index, |
115 count: count, | 123 count: count, |
116 } | 124 } |
117 } | 125 } |
118 | 126 |
119 // decodeRowKey decodes an encoded row key into its structural components. | 127 // decodeRowKey decodes an encoded row key into its structural components. |
120 func decodeRowKey(v string) (*rowKey, error) { | 128 func decodeRowKey(v string) (*rowKey, error) { |
121 keyParts := strings.SplitN(v, "~", 3) | 129 keyParts := strings.SplitN(v, "~", 3) |
122 if len(keyParts) < 2 { | 130 if len(keyParts) < 2 { |
123 // TODO: Make this force 3 once "legacy mode" is disabled. | 131 // TODO: Make this force 3 once "legacy mode" is disabled. |
124 return nil, errMalformedRowKey | 132 return nil, errMalformedRowKey |
125 } | 133 } |
126 | 134 |
127 hashEnc, idxEnc := keyParts[0], keyParts[1] | 135 hashEnc, idxEnc := keyParts[0], keyParts[1] |
128 if base64.URLEncoding.DecodedLen(len(hashEnc)) < sha256.Size { | 136 if base64.URLEncoding.DecodedLen(len(hashEnc)) < sha256.Size { |
129 return nil, errMalformedRowKey | 137 return nil, errMalformedRowKey |
130 } | 138 } |
131 | 139 |
132 » // Decode encoded path hash. | 140 » // Decode encoded project/path hash. |
133 var err error | 141 var err error |
134 rk := rowKey{} | 142 rk := rowKey{} |
135 rk.pathHash, err = base64.URLEncoding.DecodeString(hashEnc) | 143 rk.pathHash, err = base64.URLEncoding.DecodeString(hashEnc) |
136 if err != nil { | 144 if err != nil { |
137 return nil, errMalformedRowKey | 145 return nil, errMalformedRowKey |
138 } | 146 } |
139 | 147 |
140 // Decode index. | 148 // Decode index. |
141 rk.index, err = readHexInt64(idxEnc) | 149 rk.index, err = readHexInt64(idxEnc) |
142 if err != nil { | 150 if err != nil { |
(...skipping 24 matching lines...) Expand all Loading... |
167 rkb.appendInt64(rk.index) | 175 rkb.appendInt64(rk.index) |
168 if rk.count > 0 { | 176 if rk.count > 0 { |
169 rkb.appendBytes([]byte("~")) | 177 rkb.appendBytes([]byte("~")) |
170 rkb.appendInt64(rk.count) | 178 rkb.appendInt64(rk.count) |
171 } | 179 } |
172 v = rkb.value() | 180 v = rkb.value() |
173 }) | 181 }) |
174 return | 182 return |
175 } | 183 } |
176 | 184 |
177 // prefix returns the encoded path prefix for the row key. | 185 // prefix returns the encoded path prefix for the row key, which is the hash of |
| 186 // that row's project/path. |
178 func (rk *rowKey) pathPrefix() (v string) { | 187 func (rk *rowKey) pathPrefix() (v string) { |
179 withRowKeyBuffers(func(rkb *rowKeyBuffers) { | 188 withRowKeyBuffers(func(rkb *rowKeyBuffers) { |
180 rkb.appendPathPrefix(rk.pathHash) | 189 rkb.appendPathPrefix(rk.pathHash) |
181 rkb.appendBytes([]byte("~")) | 190 rkb.appendBytes([]byte("~")) |
182 v = rkb.value() | 191 v = rkb.value() |
183 }) | 192 }) |
184 return | 193 return |
185 } | 194 } |
186 | 195 |
187 // pathPrefixUpperBound returns the path prefix that is higher than any path | 196 // pathPrefixUpperBound returns the path prefix that is higher than any path |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 return 0, errMalformedRowKey | 230 return 0, errMalformedRowKey |
222 } | 231 } |
223 | 232 |
224 // There should be no more data. | 233 // There should be no more data. |
225 if dr.Len() > 0 { | 234 if dr.Len() > 0 { |
226 return 0, errMalformedRowKey | 235 return 0, errMalformedRowKey |
227 } | 236 } |
228 | 237 |
229 return value, nil | 238 return value, nil |
230 } | 239 } |
OLD | NEW |