-
Notifications
You must be signed in to change notification settings - Fork 8
/
encode.go
166 lines (138 loc) · 3.64 KB
/
encode.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
package serialize
import (
"fmt"
"github.com/ethereum/go-ethereum/rlp"
"github.com/kwilteam/kwil-db/core/types"
)
type SerializedData = []byte
type encodingType uint16
const (
// it is very important that the order of the encoding types is not changed
encodingTypeInvalid encodingType = iota
encodingTypeRLP
)
var currentEncodingType = encodingTypeRLP
// Encode encodes the given value into a serialized data format.
func Encode(val any) (SerializedData, error) {
var btsVal []byte
var err error
switch currentEncodingType {
case encodingTypeRLP:
btsVal, err = encodeRLP(val)
default:
return nil, fmt.Errorf("invalid encoding type: %d", currentEncodingType)
}
if err != nil {
return nil, err
}
return addSerializedTypePrefix(currentEncodingType, btsVal), nil
}
// Decode decodes the given serialized data into the given value.
func Decode[T any](bts SerializedData) (*T, error) {
encType, val, err := removeSerializedTypePrefix(bts)
if err != nil {
return nil, err
}
switch encType {
case encodingTypeRLP:
return decodeRLP[T](val)
default:
return nil, fmt.Errorf("invalid encoding type: %d", encType)
}
}
func EncodeSlice[T any](kvs []T) ([]byte, error) {
marshaller := make([]*serialBinaryMarshaller[T], len(kvs))
for i, kv := range kvs {
marshaller[i] = &serialBinaryMarshaller[T]{kv}
}
return types.SerializeSlice[*serialBinaryMarshaller[T]](marshaller)
}
func DecodeSlice[T any](bts []byte) ([]*T, error) {
marshaller, err := types.DeserializeSlice[*serialBinaryMarshaller[T]](bts, func() *serialBinaryMarshaller[T] {
return &serialBinaryMarshaller[T]{}
})
if err != nil {
return nil, err
}
result := make([]*T, len(marshaller))
for i, m := range marshaller {
result[i] = &m.val
}
return result, nil
}
// serialBinaryMarshaller is a helper struct that implements the BinaryMarshaler and BinaryUnmarshaler interfaces
type serialBinaryMarshaller[T any] struct {
val T
}
func (m *serialBinaryMarshaller[T]) MarshalBinary() ([]byte, error) {
return Encode(m.val)
}
func (m *serialBinaryMarshaller[T]) UnmarshalBinary(bts []byte) error {
val, err := Decode[T](bts)
if err != nil {
return err
}
m.val = *val
return nil
}
/*
// EncodeSlice serializes a slice into a byte slice
func EncodeSlice[T any](kvs []T) ([]byte, error) {
var result []byte
for _, kv := range kvs {
data, err := Encode(kv)
if err != nil {
return nil, err
}
length := uint64(len(data))
result = append(result, append(make([]byte, 8), data...)...)
binary.BigEndian.PutUint64(result[len(result)-len(data)-8:len(result)-len(data)], length)
}
return result, nil
}
// DecodeSlice deserializes a byte slice into a slice
// It is important this is only given the results of EncodeSlice
func DecodeSlice[T any](bts []byte) ([]*T, error) {
var result []*T
for len(bts) > 0 {
if len(bts) < 8 {
return nil, fmt.Errorf("insufficient bytes")
}
length := binary.BigEndian.Uint64(bts[:8])
if uint64(len(bts[8:])) < length {
return nil, fmt.Errorf("invalid length")
}
bts = bts[8:]
value, err := Decode[T](bts[:length])
if err != nil {
return nil, err
}
result = append(result, value)
bts = bts[length:]
}
return result, nil
}
*/
func encodeRLP(val any) ([]byte, error) {
return rlp.EncodeToBytes(val)
}
func decodeRLP[T any](bts []byte) (*T, error) {
var val T
err := rlp.DecodeBytes(bts, &val)
if err != nil {
return nil, err
}
return &val, nil
}
func DecodeInto(bts []byte, v any) error {
encType, val, err := removeSerializedTypePrefix(bts)
if err != nil {
return err
}
switch encType {
case encodingTypeRLP:
return rlp.DecodeBytes(val, v)
default:
return fmt.Errorf("invalid encoding type: %d", encType)
}
}