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

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

Issue 1365743002: Refactor serialization helpers from impl/memory -> service/datastore/serialize (Closed) Base URL: https://github.com/luci/gae.git@estimate_size
Patch Set: fix comments Created 5 years, 2 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
« no previous file with comments | « no previous file | impl/memory/datastore_index.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 len(bs) == 0 {
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 len(bstr) > 0 {
37 // Copy bstr
38 ret := bjoin(bstr)
39 for i := len(ret) - 1; i >= 0; i-- {
40 if ret[i] == 0xFF {
41 ret[i] = 0
42 } else {
43 ret[i]++
44 return ret
45 }
46 }
47 }
48
49 // This byte string was ALL 0xFF's. The only safe incrementation to do h ere
50 // would be to add a new byte to the beginning of bstr with the value 0x 01,
51 // and a byte to the beginning OF ALL OTHER []byte's which bstr may be
52 // compared with. This is obviously impossible to do here, so panic. If we
53 // hit this, then we would need to add a spare 0 byte before every index
54 // column.
55 //
56 // Another way to think about this is that we just accumulated a 'carry' bit,
57 // and the new value has overflowed this representation.
58 //
59 // Fortunately, the first byte of a serialized index column entry is a
60 // PropertyType byte, and the only valid values that we'll be incrementi ng
61 // are never equal to 0xFF, since they have the high bit set (so either they're
62 // 0x8*, or 0x7*, depending on if it's inverted).
63 impossible(fmt.Errorf("incrementing %v would require more sigfigs", bstr ))
64 return nil
65 }
OLDNEW
« no previous file with comments | « no previous file | impl/memory/datastore_index.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698