/
storage_memory_idx.go
358 lines (293 loc) · 7.05 KB
/
storage_memory_idx.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package storage
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"hash/crc32"
"io"
"os"
"sync"
"github.com/antlabs/deepcopy"
"google.golang.org/protobuf/proto"
)
// 索引组成
// 4个字节的payload长度
// 8个字节的key
// 8个字节的offset
// 4个字节的size
// 4个字节的crc32
// 8个字节的过期时间
var (
_ Storager = (*IndexInMemory)(nil)
defaultTable = crc32.MakeTable(0xD5828281)
maxDatLimit = 32 * GB
payload = 4
ErrFull = errors.New("The space is full")
)
type Index struct {
Key int32 `protobuf:"varint,1,opt,name=key,proto3" json:"key,omitempty"` //返回给客户端的值
Size int32 `protobuf:"varint,2,opt,name=size,proto3" json:"size,omitempty"` //大小
Offset int64 `protobuf:"varint,3,opt,name=offset,proto3" json:"offset,omitempty"` //偏移量
Timeout int64 `protobuf:"varint,4,opt,name=timeout,proto3" json:"timeout,omitempty"` //超时时间
Crc32 uint32 `protobuf:"varint,5,opt,name=crc32,proto3" json:"crc32,omitempty"` //crc32校验和
}
type Data struct {
Index
Data []byte
}
type metadata struct {
// 下个索引值, 需要持久化到文件中
Seq int64
// 当前记录总字节数, 限制单个索引能管理的最大数据,需要持久化到文件中
TotalSize int64
//被删除文件的个数,需要持久化到文件中
DeleteCount int
// 文件总数, 被删除的也计算在内, 需要持久化到文件中
FileCount int
// false代表可写, 需要持久化到文件中
Readonly bool
//最后一个offset, 需要持久化到文件中
DatOffset int64
}
// 一个内存索引管理32GB文件
type IndexInMemory struct {
idx *os.File //索引文件
dat *os.File //数据文件
md *os.File //元数据文件
enMd *json.Encoder
// 读写锁
rwmu sync.RWMutex
//最后一个索引数据的offset
idxOffset int64
metadata
// sync.Map没有Len比较蛋疼,所以这里还是map+读写锁
allIndex map[int64]Index
}
// 生成索引文件名
func idxName(fileName string) string {
return fmt.Sprintf("%s.idx", fileName)
}
// 生成数据文件名
func datName(fileName string) string {
return fmt.Sprintf("%s.dat", fileName)
}
// 生成元数据名
func metaName(fileName string) string {
return fmt.Sprintf("%s.meta", fileName)
}
func newIndexInMemory(fileName string) (idx *IndexInMemory, err error) {
var memIndex IndexInMemory
memIndex.allIndex = make(map[int64]Index, 10)
// 打开并加载索引文件
if err = memIndex.loadIdx(fileName); err != nil {
return nil, fmt.Errorf("loadIdx:%w", err)
}
// 打开数据文件
if err = memIndex.loadDat(fileName); err != nil {
return nil, fmt.Errorf("loadDat:%w", err)
}
// 打开元数据文件
if err = memIndex.loadMeta(fileName); err != nil {
return nil, fmt.Errorf("loadMeta:%w", err)
}
return &memIndex, nil
}
// 加载元数据
func (i *IndexInMemory) loadMeta(name string) (err error) {
name = metaName(name)
i.md, err = os.OpenFile(name, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
r := bufio.NewReader(i.md)
var prev []byte
for {
l, e := r.ReadBytes('\n')
if len(l) == 0 && e != nil {
break
}
prev = l
}
if len(prev) > 0 {
err = json.Unmarshal(prev, &i.metadata)
if err != nil {
return err
}
}
if i.Readonly {
i.md.Close()
tmpFile := name + ".tmp"
i.md, err = os.OpenFile(tmpFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
err = os.Rename(tmpFile, name)
}
i.enMd = json.NewEncoder(i.md)
return
}
func (i *IndexInMemory) loadIdx(name string) (err error) {
// 打开索引文件
i.idx, err = os.OpenFile(idxName(name), os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
head := uint32(0)
defer func() {
if err == io.EOF {
err = nil
}
}()
for {
err = binary.Read(i.idx, binary.LittleEndian, &head)
if err != nil {
return
}
buf := make([]byte, head)
_, err = i.idx.ReadAt(buf, i.idxOffset+4)
if err != nil {
return err
}
var index IdxVersion0
if err = proto.Unmarshal(buf, &index); err != nil {
//fmt.Printf("head = %d, %x\n", head, buf)
return err
}
var index2 Index
if err = deepcopy.Copy(&index2, &index).Do(); err != nil {
return err
}
i.idxOffset += int64(head) + 4
i.allIndex[int64(index2.Key)] = index2
}
}
func (i *IndexInMemory) loadDat(name string) (err error) {
i.dat, err = os.OpenFile(datName((name)), os.O_CREATE|os.O_RDWR, 0644)
return
}
func (i *IndexInMemory) checkFull() (err error) {
i.rwmu.Lock()
defer i.rwmu.Unlock()
if i.Readonly {
err = ErrFull
return
}
if i.TotalSize >= int64(maxDatLimit) {
i.Readonly = true
return ErrFull
}
return nil
}
func (i *IndexInMemory) updateMetadata() {
i.enMd.Encode(&i.metadata)
}
func (i *IndexInMemory) GetSeq() (key int64) {
i.rwmu.Lock()
key = i.Seq
i.Seq++
i.rwmu.Unlock()
return
}
// 保存
func (i *IndexInMemory) Put(key int64, data []byte) (err error) {
if err := i.checkFull(); err != nil {
return err
}
// TODO sync.Pool
var buf bytes.Buffer
crc := crc32.Update(0, defaultTable, data)
idx := IdxVersion0{}
idx.Key = key
idx.Size = int32(len(data))
idx.Offset = i.DatOffset
idx.Crc32 = crc
all, err := proto.Marshal(&idx)
if err != nil {
return err
}
// 写入头
if err := binary.Write(&buf, binary.LittleEndian, int32(len(all))); err != nil {
return err
}
// 写入内容
binary.Write(&buf, binary.LittleEndian, all)
i.rwmu.Lock()
// 1. 写入索引文件
n, err := i.idx.Write(buf.Bytes())
if err != nil {
i.rwmu.Unlock()
return err
}
// 3. 更新数据文件
if _, err := i.dat.WriteAt(data, i.DatOffset); err != nil {
i.idx.Truncate(i.idxOffset) //修改文件指针的大小
i.idx.Seek(int64(-n), 1) //修改文件偏移指针
i.rwmu.Unlock()
return err
}
// 2. 更新offset
i.DatOffset += int64(len(data))
i.idxOffset += int64(len(all)) + 4
var idxMem Index
err = deepcopy.Copy(&idxMem, &idx).Do()
if err != nil {
i.rwmu.Unlock()
return err
}
i.allIndex[i.Seq] = idxMem
i.Seq++
i.FileCount++
i.updateMetadata()
i.rwmu.Unlock()
return nil
}
// 获取
func (i *IndexInMemory) Get(key int64) (element Data, ok bool, err error) {
i.rwmu.RLock()
element.Index, ok = i.allIndex[key]
if !ok {
i.rwmu.RUnlock()
return
}
// TODO sync.Pool
element.Data = make([]byte, element.Size)
i.dat.ReadAt(element.Data, element.Offset)
crc := crc32.Checksum(element.Data, defaultTable)
if crc != element.Crc32 {
err = fmt.Errorf("The data file is bad:key(%d)\n", key)
i.rwmu.RUnlock()
return
}
i.rwmu.RUnlock()
return
}
// 删除
func (i *IndexInMemory) Delete(key int64) error {
i.rwmu.Lock()
delete(i.allIndex, key)
i.DeleteCount++
i.updateMetadata()
i.rwmu.Unlock()
return nil
}
// close
func (i *IndexInMemory) Close() (err error) {
i.rwmu.Lock()
defer i.rwmu.Unlock()
if err = i.idx.Sync(); err != nil {
return err
}
if err = i.dat.Sync(); err != nil {
return err
}
if err = i.md.Sync(); err != nil {
return err
}
if err = i.idx.Close(); err != nil {
return err
}
if err = i.dat.Close(); err != nil {
return err
}
return i.md.Close()
}