OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package serialize | 5 package serialize |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 ) | 9 ) |
10 | 10 |
11 // Buffer is the interface which corresponds to the subset of *bytes.Buffer | 11 // WriteBuffer is the interface which corresponds to the subset of *bytes.Buffer |
12 // that this package requires. | 12 // that this package requires. |
13 type Buffer interface { | 13 type WriteBuffer interface { |
| 14 » ReadBuffer |
| 15 |
14 String() string | 16 String() string |
15 Bytes() []byte | 17 Bytes() []byte |
16 Len() int | |
17 | 18 |
18 Grow(int) | 19 Grow(int) |
19 | 20 |
20 Read([]byte) (int, error) | |
21 ReadByte() (byte, error) | |
22 | |
23 Write([]byte) (int, error) | 21 Write([]byte) (int, error) |
24 WriteByte(c byte) error | 22 WriteByte(c byte) error |
25 WriteString(s string) (int, error) | 23 WriteString(s string) (int, error) |
26 } | 24 } |
27 | 25 |
28 var _ Buffer = (*bytes.Buffer)(nil) | 26 // ReadBuffer is the interface which corresponds to the subset of *bytes.Reader |
| 27 // that this package requires. |
| 28 type ReadBuffer interface { |
| 29 » Len() int |
| 30 |
| 31 » Read([]byte) (int, error) |
| 32 » ReadByte() (byte, error) |
| 33 } |
| 34 |
| 35 var ( |
| 36 » _ WriteBuffer = (*bytes.Buffer)(nil) |
| 37 » _ ReadBuffer = (*bytes.Reader)(nil) |
| 38 ) |
29 | 39 |
30 // InvertibleBuffer is just like Buffer, except that it also has a stateful | 40 // InvertibleBuffer is just like Buffer, except that it also has a stateful |
31 // Invert() method, which will cause all reads and writes to/from it to be | 41 // Invert() method, which will cause all reads and writes to/from it to be |
32 // inverted (e.g. every byte XOR 0xFF). | 42 // inverted (e.g. every byte XOR 0xFF). |
33 // | 43 // |
34 // Implementing queries requires manipulating the index entries (e.g. | 44 // Implementing queries requires manipulating the index entries (e.g. |
35 // synthesizing them, parsing them, etc.). In particular, when you have | 45 // synthesizing them, parsing them, etc.). In particular, when you have |
36 // a reverse-sorted field (e.g. high to low instead of low to high), it's | 46 // a reverse-sorted field (e.g. high to low instead of low to high), it's |
37 // achieved by having all the bits inverted. | 47 // achieved by having all the bits inverted. |
38 // | 48 // |
39 // All the serialization formats include delimiter information, which the | 49 // All the serialization formats include delimiter information, which the |
40 // parsers only know to parse non-inverted. If we don't have this buffer, we'd | 50 // parsers only know to parse non-inverted. If we don't have this buffer, we'd |
41 // basically have to invert every byte in the []byte array when we're trying to | 51 // basically have to invert every byte in the []byte array when we're trying to |
42 // decode a reverse-ordered field (including the bytes of all fields after the | 52 // decode a reverse-ordered field (including the bytes of all fields after the |
43 // one we intend to parse) so that the parser can consume as many bytes as it | 53 // one we intend to parse) so that the parser can consume as many bytes as it |
44 // needs (and it only knows the number of bytes it needs as it decodes them). | 54 // needs (and it only knows the number of bytes it needs as it decodes them). |
45 // This InvertibleBuffer lets that happen on the fly without having to flip the | 55 // This InvertibleBuffer lets that happen on the fly without having to flip the |
46 // whole []byte. | 56 // whole []byte. |
47 // | 57 // |
48 // If you know you need it, you'll know it's the right thing. If you're not sure | 58 // If you know you need it, you'll know it's the right thing. If you're not sure |
49 // then you definitely don't need it! | 59 // then you definitely don't need it! |
50 type InvertibleBuffer interface { | 60 type InvertibleBuffer interface { |
51 » Buffer | 61 » WriteBuffer |
52 SetInvert(inverted bool) | 62 SetInvert(inverted bool) |
53 } | 63 } |
54 | 64 |
55 type invertibleBuffer struct { | 65 type invertibleBuffer struct { |
56 » Buffer | 66 » WriteBuffer |
57 invert bool | 67 invert bool |
58 } | 68 } |
59 | 69 |
60 // Invertible returns an InvertibleBuffer based on the Buffer. | 70 // Invertible returns an InvertibleBuffer based on the Buffer. |
61 func Invertible(b Buffer) InvertibleBuffer { | 71 func Invertible(b WriteBuffer) InvertibleBuffer { |
62 return &invertibleBuffer{b, false} | 72 return &invertibleBuffer{b, false} |
63 } | 73 } |
64 | 74 |
65 func (ib *invertibleBuffer) Read(bs []byte) (int, error) { | 75 func (ib *invertibleBuffer) Read(bs []byte) (int, error) { |
66 » n, err := ib.Buffer.Read(bs) | 76 » n, err := ib.WriteBuffer.Read(bs) |
67 if ib.invert { | 77 if ib.invert { |
68 for i, b := range bs { | 78 for i, b := range bs { |
69 bs[i] = b ^ 0xFF | 79 bs[i] = b ^ 0xFF |
70 } | 80 } |
71 } | 81 } |
72 return n, err | 82 return n, err |
73 } | 83 } |
74 | 84 |
75 func (ib *invertibleBuffer) WriteString(s string) (int, error) { | 85 func (ib *invertibleBuffer) WriteString(s string) (int, error) { |
76 if ib.invert { | 86 if ib.invert { |
77 ib.Grow(len(s)) | 87 ib.Grow(len(s)) |
78 for i := 0; i < len(s); i++ { | 88 for i := 0; i < len(s); i++ { |
79 » » » if err := ib.Buffer.WriteByte(s[i] ^ 0xFF); err != nil { | 89 » » » if err := ib.WriteBuffer.WriteByte(s[i] ^ 0xFF); err !=
nil { |
80 return i, err | 90 return i, err |
81 } | 91 } |
82 } | 92 } |
83 return len(s), nil | 93 return len(s), nil |
84 } | 94 } |
85 » return ib.Buffer.WriteString(s) | 95 » return ib.WriteBuffer.WriteString(s) |
86 } | 96 } |
87 | 97 |
88 func (ib *invertibleBuffer) Write(bs []byte) (int, error) { | 98 func (ib *invertibleBuffer) Write(bs []byte) (int, error) { |
89 if ib.invert { | 99 if ib.invert { |
90 ib.Grow(len(bs)) | 100 ib.Grow(len(bs)) |
91 for i, b := range bs { | 101 for i, b := range bs { |
92 » » » if err := ib.Buffer.WriteByte(b ^ 0xFF); err != nil { | 102 » » » if err := ib.WriteBuffer.WriteByte(b ^ 0xFF); err != nil
{ |
93 return i, err | 103 return i, err |
94 } | 104 } |
95 } | 105 } |
96 return len(bs), nil | 106 return len(bs), nil |
97 } | 107 } |
98 » return ib.Buffer.Write(bs) | 108 » return ib.WriteBuffer.Write(bs) |
99 } | 109 } |
100 | 110 |
101 func (ib *invertibleBuffer) WriteByte(b byte) error { | 111 func (ib *invertibleBuffer) WriteByte(b byte) error { |
102 if ib.invert { | 112 if ib.invert { |
103 b = b ^ 0xFF | 113 b = b ^ 0xFF |
104 } | 114 } |
105 » return ib.Buffer.WriteByte(b) | 115 » return ib.WriteBuffer.WriteByte(b) |
106 } | 116 } |
107 | 117 |
108 func (ib *invertibleBuffer) ReadByte() (byte, error) { | 118 func (ib *invertibleBuffer) ReadByte() (byte, error) { |
109 » ret, err := ib.Buffer.ReadByte() | 119 » ret, err := ib.WriteBuffer.ReadByte() |
110 if ib.invert { | 120 if ib.invert { |
111 ret = ret ^ 0xFF | 121 ret = ret ^ 0xFF |
112 } | 122 } |
113 return ret, err | 123 return ret, err |
114 } | 124 } |
115 | 125 |
116 func (ib *invertibleBuffer) SetInvert(inverted bool) { | 126 func (ib *invertibleBuffer) SetInvert(inverted bool) { |
117 ib.invert = inverted | 127 ib.invert = inverted |
118 } | 128 } |
OLD | NEW |