-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec.go
181 lines (145 loc) · 4.17 KB
/
codec.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
package memorylanedb
import (
"bufio"
"encoding/binary"
"io"
)
var byteOrder = binary.LittleEndian
type Codec struct {
w *bufio.Writer
r io.Reader
}
func NewCodec(f io.ReadWriter) *Codec {
return &Codec{
w: bufio.NewWriter(f),
r: bufio.NewReader(f),
}
}
func (c *Codec) EncodeEntry(entry *Entry) (int64, error) {
if entry == nil {
return 0, ErrorNilEncoding
}
prefixSize := entry.HeaderSize()
prefixBuffer := make([]byte, prefixSize)
byteOrder.PutUint32(prefixBuffer[:CRC_SIZE], entry.Checksum)
byteOrder.PutUint32(prefixBuffer[CRC_SIZE:CRC_SIZE+TSSTAMP_SIZE], entry.Tstamp)
byteOrder.PutUint16(prefixBuffer[CRC_SIZE+TSSTAMP_SIZE:CRC_SIZE+TSSTAMP_SIZE+KEY_SIZE], entry.KeySize)
byteOrder.PutUint32(prefixBuffer[CRC_SIZE+TSSTAMP_SIZE+KEY_SIZE:], entry.ValueSize)
_, err := c.w.Write(prefixBuffer)
if err != nil {
return 0, ErrWritingPrefix
}
_, err = c.w.Write(entry.Key)
if err != nil {
return 0, ErrWritingKey
}
_, err = c.w.Write(entry.Value)
if err != nil {
return 0, ErrWritingValue
}
if flushErr := c.w.Flush(); flushErr != nil {
return 0, flushErr
}
return entry.Size(), nil
}
func (c *Codec) DecodeEntry(entry *Entry) (int64, error) {
if entry == nil {
return 0, ErrorNilDecoding
}
prefixSize := entry.HeaderSize()
prefixBuffer := make([]byte, prefixSize)
_, err := io.ReadFull(c.r, prefixBuffer)
if err != nil {
return 0, err
}
var ptr uint32 = 0
entry.Checksum = byteOrder.Uint32(prefixBuffer[ptr : ptr+CRC_SIZE])
ptr += CRC_SIZE
entry.Tstamp = byteOrder.Uint32(prefixBuffer[ptr : ptr+TSSTAMP_SIZE])
ptr += TSSTAMP_SIZE
entry.KeySize = byteOrder.Uint16(prefixBuffer[ptr : ptr+KEY_SIZE])
ptr += KEY_SIZE
entry.ValueSize = byteOrder.Uint32(prefixBuffer[ptr : ptr+VALUE_SIZE])
ptr += VALUE_SIZE
keyBuf := make([]byte, entry.KeySize)
_, err = io.ReadFull(c.r, keyBuf)
if err != nil {
return 0, err
}
entry.Key = keyBuf
valueBuf := make([]byte, entry.ValueSize)
_, err = io.ReadFull(c.r, valueBuf)
if err != nil {
return 0, err
}
entry.Value = valueBuf
return entry.Size(), nil
}
func (c *Codec) DecodeSingleEntry(buf []byte, entry *Entry) (int64, error) {
if entry == nil {
return 0, ErrorNilDecoding
}
var ptr uint32 = 0
entry.Checksum = byteOrder.Uint32(buf[ptr : ptr+CRC_SIZE])
ptr += CRC_SIZE
entry.Tstamp = byteOrder.Uint32(buf[ptr : ptr+TSSTAMP_SIZE])
ptr += TSSTAMP_SIZE
entry.KeySize = byteOrder.Uint16(buf[ptr : ptr+KEY_SIZE])
ptr += KEY_SIZE
entry.ValueSize = byteOrder.Uint32(buf[ptr : ptr+VALUE_SIZE])
ptr += VALUE_SIZE
bufWithoutPrefix := buf[ptr:]
entry.Key = bufWithoutPrefix[:entry.KeySize]
entry.Value = bufWithoutPrefix[entry.KeySize:]
return entry.Size(), nil
}
func (c *Codec) EncodeHint(hint *Hint) (int64, error) {
if hint == nil {
return 0, ErrorNilEncoding
}
prefixSize := hint.HeaderSize()
prefixBuffer := make([]byte, prefixSize)
byteOrder.PutUint32(prefixBuffer[:TSSTAMP_SIZE], hint.Tstamp)
byteOrder.PutUint16(prefixBuffer[TSSTAMP_SIZE:TSSTAMP_SIZE+KEY_SIZE], hint.KeySize)
byteOrder.PutUint32(prefixBuffer[TSSTAMP_SIZE+KEY_SIZE:TSSTAMP_SIZE+KEY_SIZE+VALUE_SIZE], hint.ValueSize)
byteOrder.PutUint32(prefixBuffer[prefixSize-VALUE_OFFSET_SIZE:], hint.ValueOffset)
_, err := c.w.Write(prefixBuffer)
if err != nil {
return 0, ErrWritingPrefix
}
_, err = c.w.Write(hint.Key)
if err != nil {
return 0, ErrWritingValue
}
if flushErr := c.w.Flush(); flushErr != nil {
return 0, flushErr
}
return hint.Size(), nil
}
func (c *Codec) DecodeHint(hint *Hint) error {
if hint == nil {
return ErrorNilDecoding
}
prefixSize := hint.HeaderSize()
prefixBuffer := make([]byte, prefixSize)
_, err := io.ReadFull(c.r, prefixBuffer)
if err != nil {
return err
}
var ptr uint32 = 0
hint.Tstamp = byteOrder.Uint32(prefixBuffer[ptr : ptr+TSSTAMP_SIZE])
ptr += TSSTAMP_SIZE
hint.KeySize = byteOrder.Uint16(prefixBuffer[ptr : ptr+KEY_SIZE])
ptr += KEY_SIZE
hint.ValueSize = byteOrder.Uint32(prefixBuffer[ptr : ptr+VALUE_SIZE])
ptr += VALUE_SIZE
hint.ValueOffset = byteOrder.Uint32(prefixBuffer[ptr : ptr+VALUE_OFFSET_SIZE])
ptr += VALUE_OFFSET_SIZE
keyBuf := make([]byte, hint.KeySize)
_, err = io.ReadFull(c.r, keyBuf)
if err != nil {
return err
}
hint.Key = keyBuf
return nil
}