-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbinary_file.go
More file actions
222 lines (200 loc) · 5.24 KB
/
binary_file.go
File metadata and controls
222 lines (200 loc) · 5.24 KB
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
package gotool
import (
"encoding/binary"
"io"
"os"
"strconv"
)
var binaryFileMap map[string]*BinaryFile
type BinaryDataFileIndexStruct struct {
Position uint64 // 8字节
Size uint64 // 8字节
}
type BinaryFile struct {
DataDir string
DataFilePath string
DataFile *os.File
IndexFilePath string
IndexFile *os.File
SuperDataNumberFile *os.File
CurrentPosition int64
SupperSaveIndexNumber int64
MaxDataNumberPerFile uint64
WriteChannel chan int64
}
// 初始化
func NewBinaryFile(dataDir string, maxDataNumberPerFile uint64) *BinaryFile {
if _, ok := binaryFileMap[dataDir]; ok {
return binaryFileMap[dataDir]
}
bf := &BinaryFile{
DataDir: dataDir,
DataFile: nil,
IndexFile: nil,
CurrentPosition: 0,
MaxDataNumberPerFile: maxDataNumberPerFile,
SupperSaveIndexNumber: -1,
}
bf.WriteChannel = make(chan int64, 1)
return bf
}
// 关闭
func (bf *BinaryFile) Close() {
if bf.SuperDataNumberFile != nil {
bf.SuperDataNumberFile.Close()
}
if bf.DataFile != nil {
bf.DataFile.Close()
bf.DataFilePath = ""
}
if bf.IndexFile != nil {
bf.IndexFile.Close()
bf.IndexFilePath = ""
}
bf.SupperSaveIndexNumber = -1
}
// 写入内容
func (bf *BinaryFile) Write(data []byte) error {
// 整理文件
bf.PrepareForWrite()
// 先写索引
binaryDataFileIndex := BinaryDataFileIndexStruct{
Position: uint64(bf.CurrentPosition),
Size: uint64(len(data)),
}
err := binary.Write(bf.IndexFile, binary.LittleEndian, binaryDataFileIndex.Position)
if err != nil {
return err
}
err = binary.Write(bf.IndexFile, binary.LittleEndian, binaryDataFileIndex.Size)
if err != nil {
return err
}
// 再写入数据
_, err = bf.DataFile.Write(data)
if err != nil {
return err
}
bf.CurrentPosition += int64(binaryDataFileIndex.Size)
// 更新最大存储值
bf.SupperSaveIndexNumber++
bf.SuperDataNumberFile.Seek(0, 8)
binary.Write(bf.SuperDataNumberFile, binary.LittleEndian, bf.SupperSaveIndexNumber)
return nil
}
// 通过索引读取数据
func (bf *BinaryFile) Read(startIndex uint64, length int) ([][]byte, error) {
err := bf.InitFiles(startIndex, "read")
if err != nil {
return nil, err
}
var dataAll [][]byte = make([][]byte, 0)
bfIndex := BinaryDataFileIndexStruct{
Position: 0,
Size: 0,
}
currentReadIndex := startIndex % bf.MaxDataNumberPerFile
for i := 0; i < length; i++ {
if currentReadIndex >= bf.MaxDataNumberPerFile {
res, err := bf.Read(startIndex, length-i)
if err != nil {
break
}
dataAll = append(dataAll, res...)
}
offsetStart := currentReadIndex * 16
_, err = bf.IndexFile.Seek(int64(offsetStart), 0)
if err != nil {
break
}
err := binary.Read(bf.IndexFile, binary.LittleEndian, &bfIndex.Position)
if err != nil {
break
}
err = binary.Read(bf.IndexFile, binary.LittleEndian, &bfIndex.Size)
if err != nil {
break
}
dataIn := make([]byte, bfIndex.Size)
bf.DataFile.Seek(int64(bfIndex.Position), 0)
_, err = bf.DataFile.Read(dataIn)
if err != nil {
break
}
dataAll = append(dataAll, dataIn)
currentReadIndex++
startIndex++
}
return dataAll, nil
}
func (bf *BinaryFile) PrepareForWrite() error {
var err error
// 首次写入从记录中读取最大存储值
if bf.SupperSaveIndexNumber == -1 {
// 根据当前索引计算索引文件及数据文件路径
bf.SuperDataNumberFile, err = os.OpenFile(bf.DataDir+SystemSeparator+"supperNumber.bin", os.O_CREATE|os.O_RDWR, 0777)
if err != nil {
return err
}
// 读取最大存储值
err = binary.Read(bf.SuperDataNumberFile, binary.LittleEndian, &bf.SupperSaveIndexNumber)
if err != nil {
if err == io.EOF {
bf.SupperSaveIndexNumber = 0
err = binary.Write(bf.SuperDataNumberFile, binary.LittleEndian, bf.SupperSaveIndexNumber)
if err != nil {
return err
}
} else {
return err
}
}
}
err = bf.InitFiles(uint64(bf.SupperSaveIndexNumber), "write")
if err != nil {
return err
}
if bf.SupperSaveIndexNumber%int64(bf.MaxDataNumberPerFile) == 0 {
bf.CurrentPosition = 0
}
return nil
}
func (bf *BinaryFile) InitFiles(startIndex uint64, action string) error {
var err error
// 根据索引最大值创建索引文件
fileNumber := startIndex / bf.MaxDataNumberPerFile
// 数据文件
oldDataFilePath := bf.DataFilePath
bf.DataFilePath = bf.DataDir + SystemSeparator + "data_" + strconv.Itoa(int(fileNumber)) + ".bin"
if oldDataFilePath != bf.DataFilePath {
if bf.DataFile != nil {
bf.DataFile.Close()
}
if action == "read" {
bf.DataFile, err = os.OpenFile(bf.DataFilePath, os.O_RDWR, 0777)
} else {
bf.DataFile, err = os.OpenFile(bf.DataFilePath, os.O_CREATE|os.O_RDWR, 0777)
}
if err != nil {
return err
}
}
// 索引文件
oldIndexFilePath := bf.IndexFilePath
bf.IndexFilePath = bf.DataDir + SystemSeparator + "index_" + strconv.Itoa(int(fileNumber)) + ".bin"
if oldIndexFilePath != bf.IndexFilePath {
if bf.IndexFile != nil {
bf.IndexFile.Close()
}
if action == "read" {
bf.IndexFile, err = os.OpenFile(bf.IndexFilePath, os.O_RDWR, 0777)
} else {
bf.IndexFile, err = os.OpenFile(bf.IndexFilePath, os.O_CREATE|os.O_RDWR, 0777)
}
if err != nil {
return err
}
}
// 开始的 position
return nil
}