/
actionkv.go
176 lines (158 loc) · 4.12 KB
/
actionkv.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
package main
import (
"bytes"
"encoding/binary"
"errors"
"hash/crc32"
"io"
"log"
"os"
)
// KeyValuePair represents a key-value pair
type KeyValuePair struct {
Key ByteString
Value ByteString
}
// ActionKV represents the data store with a BackingFile and an Index
type ActionKV struct {
BackingFile *os.File
Index map[string]uint64
}
// ByteString is just a slice of bytes
type ByteString []byte
// NewActionKV creates a new instance of ActionKV
func NewActionKV(fname string) (*ActionKV, error) {
// can't defer file close here
f, err := os.OpenFile(fname, os.O_CREATE|os.O_RDWR, 6644)
if err != nil {
return nil, err
}
return &ActionKV{BackingFile: f, Index: make(map[string]uint64)}, nil
}
func processRecord(f io.Reader) (*KeyValuePair, error) {
var (
savedChecksum uint32
keyLen uint32
valLen uint32
)
if err := binary.Read(f, binary.LittleEndian, &savedChecksum); err != nil {
return nil, err
}
if err := binary.Read(f, binary.LittleEndian, &keyLen); err != nil {
return nil, err
}
if err := binary.Read(f, binary.LittleEndian, &valLen); err != nil {
return nil, err
}
data := make([]byte, keyLen+valLen)
if _, err := io.ReadFull(f, data); err != nil {
return nil, err
}
checksum := crc32.ChecksumIEEE(data)
if checksum != savedChecksum {
log.Panicf("Invalid data: %x != %x", savedChecksum, checksum)
}
return &KeyValuePair{Key: data[0:keyLen], Value: data[keyLen:]}, nil
}
// Load loads the backing file and gets the currentPosition in the file
// to start adding data
func (a *ActionKV) Load() error {
buf := bytes.Buffer{}
if _, err := io.Copy(&buf, a.BackingFile); err != nil {
log.Fatal(err)
}
f := bytes.NewReader(buf.Bytes())
for {
currentPosition, err := f.Seek(0, io.SeekCurrent)
if err != nil {
return err
}
kv, err := processRecord(f)
if err != nil {
if err == io.EOF {
break
} else {
return err
}
}
a.Index[string(kv.Key)] = uint64(currentPosition)
}
return nil
}
// GetAt gets a key-value pair at a certain position in the backing file
func (a *ActionKV) GetAt(pos uint64) (*KeyValuePair, error) {
a.BackingFile.Seek(int64(pos), io.SeekStart)
kv, err := processRecord(a.BackingFile)
if err != nil {
return nil, err
}
return kv, nil
}
// Get returns a value from the data store for a given key
func (a *ActionKV) Get(key ByteString) (ByteString, error) {
pos, ok := a.Index[string(key)]
if !ok {
return nil, errors.New("couldn't find key")
}
kv, err := a.GetAt(pos)
if err != nil {
return nil, err
}
return kv.Value, nil
}
// Insert adds a new key-value pair to the data store
func (a *ActionKV) Insert(key ByteString, value ByteString) error {
pos, err := a.insertButIgnoreIndex(key, value)
if err != nil {
return err
}
a.Index[string(key)] = pos
return nil
}
// Delete marks a key-value pair as deleted
func (a *ActionKV) Delete(key ByteString) error {
if err := a.Insert(key, ByteString("value deleted")); err != nil {
return err
}
return nil
}
// Update updates a key-value pair
func (a *ActionKV) Update(key ByteString, value ByteString) error {
if err := a.Insert(key, value); err != nil {
return err
}
return nil
}
// insertButIgnoreIndex inserts a new key-value pair without using the index
func (a *ActionKV) insertButIgnoreIndex(key ByteString, value ByteString) (uint64, error) {
keyLen := len(key)
valLen := len(value)
tmp := make(ByteString, 0, keyLen+valLen)
for _, b := range key {
tmp = append(tmp, byte(b))
}
for _, b := range value {
tmp = append(tmp, byte(b))
}
checksum := crc32.ChecksumIEEE(tmp)
currentPosition, err := a.BackingFile.Seek(0, io.SeekCurrent)
if err != nil {
return 0, err
}
if _, err = a.BackingFile.Seek(0, io.SeekEnd); err != nil {
return 0, err
}
if err = binary.Write(a.BackingFile, binary.LittleEndian, checksum); err != nil {
return 0, err
}
if err = binary.Write(a.BackingFile, binary.LittleEndian, uint32(keyLen)); err != nil {
return 0, err
}
if err = binary.Write(a.BackingFile, binary.LittleEndian, uint32(valLen)); err != nil {
return 0, err
}
if _, err = a.BackingFile.Write(tmp); err != nil {
return 0, err
}
return uint64(currentPosition), nil
}