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 "encoding/base64" | 9 "encoding/base64" |
10 "errors" | 10 "errors" |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 // index column for it. If that's the case, then we must invert,
swap and | 161 // index column for it. If that's the case, then we must invert,
swap and |
162 // increment the inequality endpoints. | 162 // increment the inequality endpoints. |
163 // | 163 // |
164 // Invert so that the desired numbers are represented correctly
in the index. | 164 // Invert so that the desired numbers are represented correctly
in the index. |
165 // Swap so that our iterators still go from >= start to < end. | 165 // Swap so that our iterators still go from >= start to < end. |
166 // Increment so that >= and < get correctly bounded (since the i
terator is | 166 // Increment so that >= and < get correctly bounded (since the i
terator is |
167 // still using natrual bytes ordering) | 167 // still using natrual bytes ordering) |
168 if ret.suffixFormat[0].Descending { | 168 if ret.suffixFormat[0].Descending { |
169 hi, lo := []byte(nil), []byte(nil) | 169 hi, lo := []byte(nil), []byte(nil) |
170 if len(startD) > 0 { | 170 if len(startD) > 0 { |
171 » » » » lo = increment(invert(startD)) | 171 » » » » lo = increment(serialize.Invert(startD)) |
172 } | 172 } |
173 if len(endD) > 0 { | 173 if len(endD) > 0 { |
174 » » » » hi = increment(invert(endD)) | 174 » » » » hi = increment(serialize.Invert(endD)) |
175 } | 175 } |
176 endD, startD = lo, hi | 176 endD, startD = lo, hi |
177 } | 177 } |
178 } | 178 } |
179 | 179 |
180 // Now we check the start and end cursors. | 180 // Now we check the start and end cursors. |
181 // | 181 // |
182 // Cursors are composed of a list of IndexColumns at the beginning, foll
owed | 182 // Cursors are composed of a list of IndexColumns at the beginning, foll
owed |
183 // by the raw bytes to use for the suffix. The cursor is only valid if a
ll of | 183 // by the raw bytes to use for the suffix. The cursor is only valid if a
ll of |
184 // its IndexColumns match our proposed suffixFormat, as calculated above
. | 184 // its IndexColumns match our proposed suffixFormat, as calculated above
. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 ret.numCols = len(ret.suffixFormat) | 238 ret.numCols = len(ret.suffixFormat) |
239 for prop, vals := range ret.eqFilters { | 239 for prop, vals := range ret.eqFilters { |
240 if len(ret.suffixFormat) == 1 && prop == "__ancestor__" { | 240 if len(ret.suffixFormat) == 1 && prop == "__ancestor__" { |
241 continue | 241 continue |
242 } | 242 } |
243 ret.numCols += vals.Len() | 243 ret.numCols += vals.Len() |
244 } | 244 } |
245 | 245 |
246 return ret, nil | 246 return ret, nil |
247 } | 247 } |
| 248 |
| 249 func increment(bstr []byte) []byte { |
| 250 ret, overflow := serialize.Increment(bstr) |
| 251 if overflow { |
| 252 // This byte string was ALL 0xFF's. The only safe incrementation
to do here |
| 253 // would be to add a new byte to the beginning of bstr with the
value 0x01, |
| 254 // and a byte to the beginning OF ALL OTHER []byte's which bstr
may be |
| 255 // compared with. This is obviously impossible to do here, so pa
nic. If we |
| 256 // hit this, then we would need to add a spare 0 byte before eve
ry index |
| 257 // column. |
| 258 // |
| 259 // Another way to think about this is that we just accumulated a
'carry' bit, |
| 260 // and the new value has overflowed this representation. |
| 261 // |
| 262 // Fortunately, the first byte of a serialized index column entr
y is a |
| 263 // PropertyType byte, and the only valid values that we'll be in
crementing |
| 264 // are never equal to 0xFF, since they have the high bit set (so
either they're |
| 265 // 0x8*, or 0x7*, depending on if it's inverted). |
| 266 impossible(fmt.Errorf("incrementing %v would require more sigfig
s", bstr)) |
| 267 } |
| 268 return ret |
| 269 } |
OLD | NEW |