-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsm_impl.go
204 lines (165 loc) · 5.79 KB
/
fsm_impl.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
199
200
201
202
203
204
package fsm
import (
"hash/crc32"
"math"
"os"
"time"
)
// fileHashmap KV-FileStorage
type fileHashmap struct {
//最大索引槽数量(可配置)
maxSlotCount uint32
//最大索引数量(可配置)
maxIndexCount uint32
//最多文件数量(可配置)
maxFileCount uint
//索引列表
indexList map[uint]*os.File
//数据列表
dataList map[uint]*os.File
}
// hashcode 算Key的Hash值
func (f *fileHashmap) hashcode(key string) uint32 {
return crc32.ChecksumIEEE([]byte(key))
}
// beginTime 获取文件最开始的写入时间
// 占Header的[04-11]
func (f *fileHashmap) beginTime(absFilePos uint) uint64 {
return f.readUInt64(0+4, f.indexList[absFilePos])
}
// writeBeginTime 写入文件最开始的写入时间
// 占Header的[04-11]
func (f *fileHashmap) writeBeginTime(absFilePos uint) uint64 {
var now uint64 = 0
if now = f.beginTime(absFilePos); now == 0 {
now = uint64(time.Now().Unix())
f.writeUInt64(0+4, now, f.indexList[absFilePos])
}
return now
}
// writeEndTime 写入文件最后的写入时间
// 占Header的[12-19]
func (f *fileHashmap) writeEndTime(endTime uint64, absFilePos uint) {
f.writeUInt64(0+4+8, endTime, f.indexList[absFilePos])
}
// nextWriteableIndexOffset 获取下一次可写入索引的游标位置(最多 1<<32 - 1 个)
// 占Header的[20-23]
func (f *fileHashmap) nextWriteableIndexOffset(absFilePos uint) uint32 {
available := f.readUInt32(0+4+8+8, f.indexList[absFilePos])
if available >= math.MaxUint32 {
available = 0
}
return available
}
// writeNextWriteableIndexOffset 下一次可写入索引的游标位置
// 占Header的[20-23]
func (f *fileHashmap) writeNextWriteableIndexOffset(absFilePos uint) {
pos := f.nextWriteableIndexOffset(absFilePos) + 1
if pos >= f.maxIndexCount {
pos = 0
}
f.writeUInt32(0+4+8+8, pos, f.indexList[absFilePos])
}
// nextWriteableDataOffset 获取下一次可写入索引的游标位置(最多 1<<32 - 1 个)
// 占Header的[24-27]
func (f *fileHashmap) nextWriteableDataOffset(absFilePos uint) uint32 {
available := f.readUInt32(0+4+8+8+4, f.indexList[absFilePos])
if available >= math.MaxUint32 {
available = 0
}
return available
}
// writeNextWriteableDataOffset 下一次可写入索引的游标位置
// 占Header的[24-27]
func (f *fileHashmap) writeNextWriteableDataOffset(absDataPos uint32, absFilePos uint) {
f.writeUInt32(0+4+8+8+4, absDataPos, f.indexList[absFilePos])
}
// writeIndex 写入16字节的索引信息
func (f *fileHashmap) writeIndex(absIndexPos int64, keyHash, value, timeDiff, slotValue uint32, absFilePos uint) {
now := time.Now().Unix()
f.writeUInt32(absIndexPos, keyHash, f.indexList[absFilePos])
f.writeUInt32(absIndexPos+4, value, f.indexList[absFilePos])
f.writeUInt32(absIndexPos+4+4, uint32(now)-timeDiff, f.indexList[absFilePos])
f.writeUInt32(absIndexPos+4+4+4, slotValue, f.indexList[absFilePos])
f.writeEndTime(uint64(now), absFilePos)
f.writeNextWriteableIndexOffset(absFilePos)
}
// readIndex 读索引
func (f *fileHashmap) readIndex(absIndexPos int64, absFilePos uint) (keyHash uint32, value uint32, timeDiff uint32, prevIndex uint32) {
return f.readUInt32(absIndexPos, f.indexList[absFilePos]),
f.readUInt32(absIndexPos+4,
f.indexList[absFilePos]),
f.readUInt32(absIndexPos+4+4, f.indexList[absFilePos]),
f.readUInt32(absIndexPos+4+4+4, f.indexList[absFilePos])
}
// writeData 写入数据
func (f *fileHashmap) writeData(absDataPos int64, value []byte, absFilePos uint) {
nextWritePos := uint32(0)
//如果索引的位置不是0,则要找合适的位置
if f.nextWriteableIndexOffset(absFilePos) > 0 {
nextWritePos = f.readNextAvailableWritePos(uint32(absDataPos), len(value), absFilePos)
}
f.writeUInt32(absDataPos, uint32(len(value)), f.dataList[absFilePos])
f.writeByte(absDataPos+4, value, f.dataList[absFilePos])
f.writeNextWriteableDataOffset(nextWritePos, absFilePos)
}
// readData 读数据
func (f *fileHashmap) readData(absDataPos int64, absFilePos uint) []byte {
return f.readByte(absDataPos+4, uint64(f.readUInt32(absDataPos, f.dataList[absFilePos])), f.dataList[absFilePos])
}
// readNextAvailableWritePos 获取下次可写入数据的位置
/*
||||---------
||||-11111111-||||-222222-||||-3333333333333-||||-4---------------------------------
||||-11110000-||||-222222-||||-3333333333333-||||-4---------------------------------
||||-111111111111111111111100000000000000000-||||-4---------------------------------
*/
func (f *fileHashmap) readNextAvailableWritePos(absDataPos uint32, bytesLength int, absFilePos uint) uint32 {
writeOffset := absDataPos
for {
pos := f.readUInt32(int64(writeOffset), f.dataList[absFilePos])
writeOffset += 4
if pos < math.MaxUint32 {
writeOffset += pos
} else {
writeOffset += uint32(bytesLength)
}
if writeOffset-absDataPos >= uint32(bytesLength) + 4 {
break
}
}
return writeOffset
}
// Get Key=String
// 返回Key数组(可以重复)
func (f *fileHashmap) getIndex(key string) []uint32 {
values := make([]uint32, 0)
if key == "" {
return values
}
keyHash := f.hashcode(key)
absFilePos := uint(keyHash) % f.maxFileCount
slotPos := keyHash % f.maxSlotCount
absSlotPos := int64(HeaderSize) + int64(slotPos)*int64(SlotSize)
slotValue := f.readUInt32(absSlotPos, f.indexList[absFilePos])
if slotValue >= math.MaxUint32 {
return nil
}
lastIndex := slotValue
nextIndex := slotValue
duplicateValue := make(map[uint32]struct{}, 0)
for {
absIndexPos := int64(HeaderSize) + int64(f.maxSlotCount)*int64(SlotSize) + int64(nextIndex)*int64(IndexSize)
hash, value, _, prevIndex := f.readIndex(absIndexPos, absFilePos)
if keyHash == hash {
if _, exists := duplicateValue[value]; !exists {
values = append(values, value)
}
}
if (prevIndex == lastIndex) || (nextIndex == 0 && prevIndex == 0) {
return values
}
nextIndex = prevIndex
duplicateValue[value] = struct{}{}
}
}