Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: impl/memory/binary_tools.go

Issue 1302813003: impl/memory: Implement Queries (Closed) Base URL: https://github.com/luci/gae.git@add_multi_iterator
Patch Set: minor fixes Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package memory
6
7 import (
8 "fmt"
9 )
10
11 func bjoin(itms ...[]byte) []byte {
12 total := 0
13 for _, i := range itms {
14 total += len(i)
15 }
16 ret := make([]byte, 0, total)
17 for _, i := range itms {
18 ret = append(ret, i...)
19 }
20 return ret
21 }
22
23 // invert simply inverts all the bytes in bs.
24 func invert(bs []byte) []byte {
25 if bs == nil {
dnj 2015/08/28 16:37:54 if len(bs) == 0
iannucci 2015/08/28 19:48:55 Done.
26 return nil
27 }
28 ret := make([]byte, len(bs))
29 for i, b := range bs {
30 ret[i] = 0xFF ^ b
31 }
32 return ret
33 }
34
35 func increment(bstr []byte) []byte {
36 if bstr == nil {
dnj 2015/08/28 16:37:54 len(bstr) == 0 But one cannot increment zero-leng
iannucci 2015/08/28 19:48:55 Done.
37 return nil
38 }
39
40 // Copy bstr
41 ret := bjoin(bstr)
42 for i := len(ret) - 1; i >= 0; i-- {
43 if ret[i] == 0xFF {
44 ret[i] = 0
45 } else {
46 ret[i]++
47 return ret
48 }
49 }
50
51 // This byte string was ALL FF's. The only safe incrementation to do her e
dnj 2015/08/28 16:37:54 0xFF (you use 0x prefix elsewhere)
iannucci 2015/08/28 19:48:54 done
52 // would be to add a new byte to the beginning of bstr with the value 0x 01,
53 // and a byte to the beginning OF ALL OTHER []byte's which bstr may be
54 // compared with. This is obviously impossible to do here, so panic. If we
55 // hit this, then we would need to add a spare 0 byte before every index
56 // column.
57 //
58 // Another way to think about this is that we just accumulated a 'carry' bit,
59 // and the new value has overflowed this representation.
60 //
61 // Fortunately, the first byte of a serialized index column entry is a
62 // PropertyType byte, and the only valid values that we'll be incrementi ng
63 // are never equal to 0xFF, since they have the higbit set (so either th ey're
dnj 2015/08/28 16:37:54 high bit**
iannucci 2015/08/28 19:48:55 Done
64 // 0x8*, or 0x7*, depending on if it's inverted).
65 impossible(fmt.Errorf("incrementing %v would require more sigfigs", bstr ))
66 return nil
67 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698