-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwal.go
198 lines (161 loc) · 5.11 KB
/
wal.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
package nimbusdb
import (
"hash/crc32"
"sync"
"time"
"github.com/manosriram/nimbusdb/utils"
)
var (
crcTable = crc32.MakeTable(crc32.IEEE)
)
// KeyValueEntry is the raw and complete uncompressed data existing on the disk.
// KeyValueEntry is stored in Blocks in cache for faster reads.
type KeyValueEntry struct {
crc uint32
deleted byte
blockNumber int64
offset int64
size int64 // Equals StaticChunkSize + keysize + valuesize
tstamp int64
keySize int64
valueSize int64
key []byte
value []byte
fileId string
}
// Block represents a single block of disk memory. Default size is 32KB.
// Each Segment is a collection of blocks; Each block is a collection of KeyValueEntries.
type Block struct {
entries []*KeyValueEntry
blockNumber int64
blockOffset int64
}
func (kv *KeyValueEntry) StaticChunkSize() int64 {
return StaticChunkSize + kv.keySize + kv.valueSize
}
func (kv *KeyValueEntry) Key() []byte {
return kv.key
}
func (kv *KeyValueEntry) Value() []byte {
return kv.value
}
var bytePool = sync.Pool{
New: func() interface{} {
return make([]byte, 1024)
},
}
func (kv *KeyValueEntry) PayloadToByte() []byte {
keyValueEntryInBytes := make([]byte, 0, kv.StaticChunkSize())
buf := bytePool.Get().([]byte)
buf = buf[:0]
buf = append(buf, utils.Int64ToByte(kv.tstamp)...)
buf = append(buf, utils.Int64ToByte(kv.keySize)...)
buf = append(buf, utils.Int64ToByte(kv.valueSize)...)
keyValueEntryInBytes = append(keyValueEntryInBytes, buf...)
keyValueEntryInBytes = append(keyValueEntryInBytes, kv.key...)
keyValueEntryInBytes = append(keyValueEntryInBytes, kv.value...)
bytePool.Put(buf)
return keyValueEntryInBytes
}
func (kv *KeyValueEntry) ToByte() []byte {
keyValueEntryInBytes := make([]byte, 0, kv.StaticChunkSize())
buf := bytePool.Get().([]byte)
buf = buf[:0]
buf = append(buf, utils.UInt32ToByte(kv.crc)...)
buf = append(buf, kv.deleted)
buf = append(buf, utils.Int64ToByte(kv.tstamp)...)
buf = append(buf, utils.Int64ToByte(kv.keySize)...)
buf = append(buf, utils.Int64ToByte(kv.valueSize)...)
keyValueEntryInBytes = append(keyValueEntryInBytes, buf...)
keyValueEntryInBytes = append(keyValueEntryInBytes, kv.key...)
keyValueEntryInBytes = append(keyValueEntryInBytes, kv.value...)
bytePool.Put(buf)
return keyValueEntryInBytes
}
func (kv *KeyValueEntry) setTTLViaTimestamp(tstamp int64) {
kv.tstamp = tstamp
}
func (kv *KeyValueEntry) setTTLViaDuration(tstamp time.Duration) {
kv.tstamp = int64(time.Now().Add(tstamp).UnixNano())
}
func (kv *KeyValueEntry) setCRC(crc uint32) {
kv.crc = crc
}
func (kv *KeyValueEntry) calculateCRC() uint32 {
data := kv.PayloadToByte()
hash := crc32.Checksum(data, crcTable)
return hash
}
func (db *Db) writeKeyValueEntry(keyValueEntry *KeyValueEntry) error {
writer, err := db.getActiveDataFileWriter()
if err != nil {
return err
}
_, err = writer.Write(keyValueEntry.ToByte())
return err
}
// Gets the KeyValueEntry from given offset using data slice
// Used to get the entire KeyValueEntry from file data
func getKeyValueEntryFromOffsetViaData(offset int64, data []byte) (*KeyValueEntry, error) {
defer utils.Recover()
if int(offset+StaticChunkSize) > len(data) {
return nil, ERROR_OFFSET_EXCEEDED_FILE_SIZE
}
crc := data[offset : offset+CrcOffset]
intCrc := utils.ByteToUInt32(crc)
deleted := data[offset+CrcOffset]
tstamp := data[offset+DeleteFlagOffset : offset+TstampOffset]
tstamp64Bit := utils.ByteToInt64(tstamp)
hasTimestampExpired := utils.HasTimestampExpired(tstamp64Bit)
// get key size
ksz := data[offset+TstampOffset : offset+KeySizeOffset]
intKsz := utils.ByteToInt64(ksz)
// get value size
vsz := data[offset+KeySizeOffset : offset+ValueSizeOffset]
intVsz := utils.ByteToInt64(vsz)
if int(offset+ValueSizeOffset+intKsz) > len(data) {
return nil, ERROR_OFFSET_EXCEEDED_FILE_SIZE
}
// get key
k := data[offset+ValueSizeOffset : offset+ValueSizeOffset+intKsz]
if int(offset+ValueSizeOffset+intKsz+intVsz) > len(data) {
return nil, ERROR_OFFSET_EXCEEDED_FILE_SIZE
}
// get value
v := data[offset+ValueSizeOffset+intKsz : offset+ValueSizeOffset+intKsz+intVsz]
keyValueEntryFromOffset := &KeyValueEntry{
deleted: deleted,
offset: offset,
keySize: int64(len(k)),
valueSize: int64(len(utils.Encode(v))),
size: int64(StaticChunkSize + intKsz + intVsz),
key: k,
value: utils.Encode(v),
}
keyValueEntryFromOffset.setTTLViaTimestamp(tstamp64Bit)
if intCrc != keyValueEntryFromOffset.calculateCRC() {
return nil, ERROR_CRC_DOES_NOT_MATCH
}
if hasTimestampExpired {
return keyValueEntryFromOffset, ERROR_KEY_NOT_FOUND
}
return keyValueEntryFromOffset, nil
}
// Gets KeyValueEntry using offset and adds it to cacheBlock
// Used to add block's entries to cacheBlock.entries
func appendEntriesToBlock(data []byte, block *BlockOffsetPair, cacheBlock *Block) error {
offset := block.startOffset
for offset < block.endOffset {
pair, err := getKeyValueEntryFromOffsetViaData(offset, data)
if err != nil {
return err
}
if pair != nil {
offset += pair.size
cacheBlock.entries = append(cacheBlock.entries, pair)
continue
}
break
}
return nil
}