-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.go
53 lines (43 loc) · 868 Bytes
/
record.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
package main
import (
"bytes"
"encoding/binary"
)
const (
KEY_SIZE = 2
VALUE_SIZE = 2
)
type Record struct {
Key int16
Value int16
}
func NewRecord(key []byte, value []byte) *Record {
var k, v int16
reader := bytes.NewReader(key)
err := binary.Read(reader, binary.BigEndian, &k)
if err != nil {
panic(err)
}
reader = bytes.NewReader(value)
err = binary.Read(reader, binary.BigEndian, &v)
if err != nil {
panic(err)
}
record := &Record{k, v}
return record
}
func (record *Record) ToBytes() []byte {
w := new(bytes.Buffer)
err := binary.Write(w, binary.BigEndian, record.Key)
if err != nil {
panic(err)
}
err = binary.Write(w, binary.BigEndian, record.Value)
if err != nil {
panic(err)
}
return w.Bytes()
}
func (record *Record) Equals(other *Record) bool {
return record.Key == other.Key && record.Value == other.Value
}