-
Notifications
You must be signed in to change notification settings - Fork 6
/
value.go
269 lines (241 loc) · 7.41 KB
/
value.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package indexer
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-varint"
)
var (
_ ValueCodec = (*JsonValueCodec)(nil)
_ ValueCodec = (*BinaryValueCodec)(nil)
_ ValueCodec = (*BinaryWithJsonFallbackCodec)(nil)
// ErrCodecOverflow signals that unexpected size was encountered while
// unmarshalling bytes to Value.
ErrCodecOverflow = errors.New("overflow")
)
type (
// Value is the value of an index entry that is stored for each multihash in
// the indexer.
Value struct {
// ProviderID is the peer ID of the provider of the multihash.
ProviderID peer.ID `json:"p"`
// ContextID identifies the metadata that is part of this value.
ContextID []byte `json:"c"`
// MetadataBytes is serialized metadata. The is kept serialized, because
// the indexer only uses the serialized form of this data.
MetadataBytes []byte `json:"m,omitempty"`
}
// ValueCodec represents Value serializer and deserializer to/from bytes.
ValueCodec interface {
// MarshalValue serializes a single value.
MarshalValue(Value) ([]byte, error)
// UnmarshalValue deserializes a single value.
UnmarshalValue(b []byte) (Value, error)
// MarshalValueKeys serializes a Value list for storage.
MarshalValueKeys([][]byte) ([]byte, error)
// UnmarshalValueKeys deserializes value keys list.
UnmarshalValueKeys([]byte) ([][]byte, error)
}
// JsonValueCodec serializes and deserializes Value as JSON.
// See: json.Marshal, json.Unmarshal
JsonValueCodec struct{}
// BinaryValueCodec serializes and deserializes Value as binary sections
// prepended with byte length as varint.
BinaryValueCodec struct {
// ZeroCopy controls whether decoded values of []byte type point into
// the input []byte parameter passed to UnmarshalValue or
// UnmarshalValueKeys.
//
// This optimization prevents unnecessary copying. It is optional as
// the caller MUST ensure that the input parameter []byte is not
// modified after the Unmarshal happens, as any changes are mirrored in
// the decoded result.
ZeroCopy bool
}
// BinaryWithJsonFallbackCodec always serialises values as binary but deserializes
// both from binary and JSON, which gracefully and opportunistically migrates codec
// from JSON to the more efficient binary format.
BinaryWithJsonFallbackCodec struct {
BinaryValueCodec
JsonValueCodec
}
)
// Match return true if both values have the same ProviderID and ContextID.
func (v Value) Match(other Value) bool {
return v.ProviderID == other.ProviderID && bytes.Equal(v.ContextID, other.ContextID)
}
// Equal returns true if two Value instances are identical.
func (v Value) Equal(other Value) bool {
return v.Match(other) && bytes.Equal(v.MetadataBytes, other.MetadataBytes)
}
// MatchEqual returns true for the first bool if both values have the same
// ProviderID and ContextID, and returns true for the second value if the
// metadata is also equal.
func (v Value) MatchEqual(other Value) (isMatch bool, isEqual bool) {
if v.Match(other) {
isMatch = true
isEqual = bytes.Equal(v.MetadataBytes, other.MetadataBytes)
}
return
}
func (JsonValueCodec) MarshalValue(v Value) ([]byte, error) {
return json.Marshal(&v)
}
func (JsonValueCodec) UnmarshalValue(b []byte) (v Value, err error) {
err = json.Unmarshal(b, &v)
return
}
func (JsonValueCodec) MarshalValueKeys(vk [][]byte) ([]byte, error) {
return json.Marshal(&vk)
}
func (JsonValueCodec) UnmarshalValueKeys(b []byte) (vk [][]byte, err error) {
err = json.Unmarshal(b, &vk)
return
}
func (BinaryValueCodec) MarshalValue(v Value) ([]byte, error) {
pid := []byte(v.ProviderID)
pl := len(pid)
upl := uint64(pl)
cl := len(v.ContextID)
ucl := uint64(cl)
ml := len(v.MetadataBytes)
uml := uint64(ml)
size := varint.UvarintSize(upl) + pl +
varint.UvarintSize(ucl) + cl +
varint.UvarintSize(uml) + ml
var buf bytes.Buffer
buf.Grow(size)
buf.Write(varint.ToUvarint(upl))
buf.Write(pid)
buf.Write(varint.ToUvarint(ucl))
buf.Write(v.ContextID)
buf.Write(varint.ToUvarint(uml))
buf.Write(v.MetadataBytes)
return buf.Bytes(), nil
}
// UnmarshalValue deserializes a single value.
//
// If a failure occurs during serialization an error is returned along with
// the partially deserialized value keys. Only nil error means complete and
// successful deserialization.
func (c BinaryValueCodec) UnmarshalValue(b []byte) (Value, error) {
var v Value
buf := bytes.NewBuffer(b)
// Decode provider ID.
usize, err := varint.ReadUvarint(buf)
if err != nil {
return v, err
}
size := int(usize)
if size < 0 || size > buf.Len() {
return Value{}, ErrCodecOverflow
}
v.ProviderID = peer.ID(buf.Next(size))
// Decode context ID.
usize, err = varint.ReadUvarint(buf)
if err != nil {
return v, err
}
size = int(usize)
if size < 0 || size > buf.Len() {
return v, ErrCodecOverflow
}
if c.ZeroCopy {
v.ContextID = buf.Next(size)
} else {
v.ContextID = make([]byte, size)
buf.Read(v.ContextID)
}
// Decode metadata.
usize, err = varint.ReadUvarint(buf)
if err != nil {
return v, err
}
size = int(usize)
if size < 0 || size > buf.Len() {
return v, ErrCodecOverflow
}
if c.ZeroCopy {
v.MetadataBytes = buf.Next(size)
} else {
v.MetadataBytes = make([]byte, size)
buf.Read(v.MetadataBytes)
}
if buf.Len() != 0 {
return v, fmt.Errorf("too many bytes; %d remain unread", buf.Len())
}
return v, nil
}
func (BinaryValueCodec) MarshalValueKeys(vk [][]byte) ([]byte, error) {
var buf bytes.Buffer
for _, v := range vk {
vl := len(v)
uvl := uint64(vl)
buf.Grow(varint.UvarintSize(uvl) + vl)
buf.Write(varint.ToUvarint(uvl))
buf.Write(v)
}
return buf.Bytes(), nil
}
// UnmarshalValueKeys deserializes value keys.
//
// If a failure occurs during serialization an error is returned along with
// the partially deserialized value keys. Only nil error means complete and
// successful deserialization.
func (c BinaryValueCodec) UnmarshalValueKeys(b []byte) ([][]byte, error) {
var vk [][]byte
buf := bytes.NewBuffer(b)
// Decode each value key.
for buf.Len() != 0 {
usize, err := varint.ReadUvarint(buf)
if err != nil {
return vk, err
}
size := int(usize)
if size < 0 || size > buf.Len() {
return vk, ErrCodecOverflow
}
if c.ZeroCopy {
vk = append(vk, buf.Next(size))
} else {
vkData := make([]byte, size)
buf.Read(vkData)
vk = append(vk, vkData)
}
}
return vk, nil
}
func (bjc BinaryWithJsonFallbackCodec) MarshalValue(v Value) ([]byte, error) {
return bjc.BinaryValueCodec.MarshalValue(v)
}
func (bjc BinaryWithJsonFallbackCodec) UnmarshalValue(b []byte) (Value, error) {
v, err := bjc.BinaryValueCodec.UnmarshalValue(b)
if err == nil {
return v, nil
}
// If b does not look like JSON, i.e. a JSON object with no whitespace at head or tail,
// return the binary unmarshal error.
bl := len(b)
if bl < 2 || b[0] != '{' || b[bl-1] != '}' {
return Value{}, err
}
return bjc.JsonValueCodec.UnmarshalValue(b)
}
func (bjc BinaryWithJsonFallbackCodec) MarshalValueKeys(vk [][]byte) ([]byte, error) {
return bjc.BinaryValueCodec.MarshalValueKeys(vk)
}
func (bjc BinaryWithJsonFallbackCodec) UnmarshalValueKeys(b []byte) ([][]byte, error) {
v, err := bjc.BinaryValueCodec.UnmarshalValueKeys(b)
if err == nil {
return v, nil
}
// If b does not look like JSON, i.e. a JSON array with no whitespace at head or tail,
// return the binary unmarshal error.
bl := len(b)
if bl < 2 || b[0] != '[' || b[bl-1] != ']' {
return nil, err
}
return bjc.JsonValueCodec.UnmarshalValueKeys(b)
}