-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinFileRead.go
325 lines (292 loc) · 7.42 KB
/
binFileRead.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
package fchan
import (
"bufio"
"encoding/binary"
"io"
"os"
"strings"
"github.com/yireyun/go-flock"
"github.com/yireyun/go-fwrite"
)
//文件记录器
type BinFileReadWrite struct {
Name string
fileName string
fileCfg *FileConfig
flock flock.Flocker
fd *os.File
reader *bufio.Reader
readOff int64
lineHead []byte
lineTail []byte
lineMark string
lineEof *FileLine
}
//创建文件记录器
//fileName 是出文件名
//err 是输出错误信息
func NewBinFileReadWrite(name string) *BinFileReadWrite {
rw := new(BinFileReadWrite)
rw.Name = name
rw.fileCfg = new(FileConfig)
rw.lineHead = []byte(TxtLineHead)
rw.lineTail = []byte(TxtLineTail)
rw.lineMark = LineMark
rw.lineEof = NewFileLine()
return rw
}
//初始化
//fileSync 是输入是否同步写文件
//filePrefix 是输入文件前缀
//writeSuffix 是输入正在写文件后缀
//renameSuffix 是输入重命名文件后缀
//cleanSuffix 是输入清理文件名后缀
//cleaning 是输入是否清理历史
//maxDays 是输入最大天数,最小为3天
func (rw *BinFileReadWrite) Init(fileSync bool,
filePrefix, writeSuffix, renameSuffix, cleanSuffix string,
cleaning bool, maxDays int) (fileName string, err error) {
prefix := func(s string) string {
s = strings.TrimSpace(s)
if l := len(s); l > 0 && s[l-1] == '.' {
return s[:l-1]
} else {
return s
}
}
suffix := func(s string) string {
s = strings.TrimSpace(s)
if l := len(s); l > 0 && s[0] != '.' {
return "." + s
} else {
return s
}
}
filePrefix = prefix(filePrefix)
if filePrefix == "" {
return "", errorf("filePrefix is null")
}
writeSuffix = suffix(writeSuffix)
if writeSuffix == "" {
return "", errorf("writeSuffix is null")
}
renameSuffix = suffix(renameSuffix)
if renameSuffix == "" {
return "", errorf("renameSuffix is null")
}
cleanSuffix = suffix(cleanSuffix)
if cleanSuffix == "" {
return "", errorf("cleanSuffix is null")
}
if cleaning && maxDays < fwrite.MaxKeepDays { //最小为3天
return "", errorf("maxDays not less than 3 day")
}
if rw.fileCfg.FilePrefix == filePrefix &&
rw.fileCfg.WriteSuffix == writeSuffix &&
rw.fileCfg.RenameSuffix == renameSuffix &&
rw.fileCfg.CleanSuffix == cleanSuffix &&
rw.fileCfg.Cleaning == cleaning &&
rw.fileCfg.MaxDays == maxDays {
return rw.fileCfg.FileName, nil
}
rw.fileCfg.FilePrefix = filePrefix
rw.fileCfg.WriteSuffix = writeSuffix
rw.fileCfg.RenameSuffix = renameSuffix
rw.fileCfg.CleanSuffix = cleanSuffix
if rw.fileCfg.RotateRenameSuffix {
rw.fileCfg.RotateRename = writeSuffix != renameSuffix
} else {
rw.fileCfg.RotateRename = true
}
if rw.fileCfg.CleanRenameSuffix {
rw.fileCfg.CleanRename = writeSuffix != renameSuffix
} else {
rw.fileCfg.CleanRename = false
}
rw.fileCfg.MaxDays = maxDays
rw.fileCfg.FileName = rw.fileCfg.FilePrefix + rw.fileCfg.WriteSuffix
return rw.fileCfg.FileName, nil
}
//初始化行标志和标记
//lineHead 是输入行头标志,如:"---",长度不能小于1
//lineTail 是输入行尾标志,如:"===",长度不能小于1
func (rw *BinFileReadWrite) InitLineMark(lineHead, lineTail string) error {
lineHead = strings.TrimSpace(lineHead)
lineTail = strings.TrimSpace(lineTail)
if len(lineHead) < 0 {
return ErrLineHeadNil
}
if len(lineTail) < 0 {
return ErrLineTailNil
}
rw.lineHead = []byte(lineHead)
rw.lineTail = []byte(lineTail)
return nil
}
func (rw *BinFileReadWrite) readFull(r io.Reader, buf []byte) error {
index := 0
for index < len(buf) {
n, err := r.Read(buf[index:])
if err != nil {
return err
}
index += n //累计读取量
rw.readOff += int64(n) //累计读偏移
}
return nil
}
//打开文件
//fileName 是输出文件名
//fileSync 是输入是否同步写文件
//err 是输出错误信息
func (rw *BinFileReadWrite) Open(fileName string, fileSync bool) error {
flag := os.O_RDWR
if fileSync {
flag |= os.O_SYNC
}
fd, err := os.OpenFile(fileName, flag, 0660) //建议同步写
if err != nil { //文件不存在
return err
}
rw.lineEof.Reset()
if rw.fileName != "" && rw.fd != nil {
rw.fd.Close()
}
rw.fileName = fileName
rw.flock = flock.NewFlock(fileName + fwrite.LockSuffix)
rw.fd = fd
rw.readOff = 0
rw.reader = bufio.NewReader(fd)
return nil
}
//关闭文件
func (rw *BinFileReadWrite) Close() error {
if rw.fd == nil || rw.reader == nil {
return ErrFileNotOpen
}
if e := rw.fd.Close(); e != nil {
return e
}
rw.fd = nil
rw.reader = nil
rw.lineEof.Reset()
return nil
}
//读取文件行
//line 是输入文件行
func (rw *BinFileReadWrite) Read(line *FileLine) error {
if rw.fd == nil || rw.reader == nil {
return ErrFileNotOpen
}
if rw.lineEof.IsEof {
line.Clone(rw.lineEof)
return nil
}
line.Reset()
line.FileName = rw.fileName
//读取二进制数据头 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var lineHeadByte = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
e := rw.readFull(rw.reader, lineHeadByte) //读取行头
if e != nil {
return e
}
lineSize := int(binary.BigEndian.Uint32(lineHeadByte[:4]))
lineVer := lineHeadByte[4]
if lineVer != 1 {
return errorf("line version is't 1\n")
}
headSize := lineHeadByte[5]
if headSize != 8 {
return errorf("line head size is't 8\n")
}
markSize := lineHeadByte[6]
//space := lineHeadByte[7]
line.LineNO = int64(binary.BigEndian.Uint32(lineHeadByte[8:12]))
bordSize := int(binary.BigEndian.Uint32(lineHeadByte[12:16]))
if n := lineSize - FixSize - HeadSize - bordSize - int(markSize); n != 0 {
return errorf("line size mismatching")
}
//读取二进制数据
line.buff.Reset()
line.buff.Grow(bordSize)
buff := line.buff.Bytes()[:bordSize]
rw.readFull(rw.reader, buff) //读取二进制数据
if e != nil {
return e
}
line.Line.Write(buff)
//读取二进制数据标记
line.buff.Reset()
line.buff.Grow(int(markSize))
buff = line.buff.Bytes()[:markSize]
rw.readFull(rw.reader, buff) //读取二进制数据
if e != nil {
return e
}
line.Mark = trimMark(string(buff))
line.use = len(line.Mark)
line.free = lineSize - FixSize - HeadSize - bordSize - line.use
line.off = rw.readOff - int64(line.free)
line.readFD = rw.fd
line.IsEof = line.LineNO == 0 && line.Mark == fwrite.FileEof
if line.IsEof {
rw.lineEof.Clone(line)
}
return nil
}
//检查文件是否锁定
func (rw *BinFileReadWrite) Locked() bool {
if rw.fd == nil {
panic("文件没有打开")
}
if e := rw.flock.NBLock(); e == nil {
rw.flock.Unlock()
return false
} else {
return true
}
}
//读取文件行
//line 是输入文件行
//make 是输入文件行标记
func (rw *BinFileReadWrite) Mark(line *FileLine, mark string) error {
if rw.fd == nil || rw.reader == nil {
return ErrFileNotOpen
}
if line.readFD != rw.fd {
return ErrLineNotMatch
}
if line.off <= 0 {
return errorf("line off less equi than 0")
}
if line.use < 0 {
return errorf("line use less than 0")
}
mark = strings.TrimSpace(mark)
if len(mark) > line.free {
return errorf("line mark len more than %v", line.free)
}
var e error
if line.use > 0 {
line.Mark = line.Mark + "," + mark
_, e = rw.fd.WriteAt([]byte(","+mark), line.off)
} else {
line.Mark = mark
_, e = rw.fd.WriteAt([]byte(mark), line.off)
}
return e
}
//释放所有资源
func (rw *BinFileReadWrite) Destroy() {
if rw.fd != nil {
rw.fd.Close()
rw.fd = nil
rw.reader = nil
}
}
//写入缓存数据
func (rw *BinFileReadWrite) Flush() {
if rw.fd != nil {
rw.fd.Sync()
}
}