-
Notifications
You must be signed in to change notification settings - Fork 4
/
mmapfile.go
272 lines (242 loc) · 6.56 KB
/
mmapfile.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
package utils
import (
"encoding/binary"
"github.com/edsrzf/mmap-go"
log "github.com/getwe/goose/log"
"os"
"path/filepath"
"reflect"
)
type MmapFile struct {
// 文件路径
filePath string
// 文件名
fileName string
// 文件指针
fileHandle *os.File
// mmap指针
fileMmap mmap.MMap
// 文件大小(单位byte),uint32表示文件大小不能超过4G
fileSize uint32
}
// mmap方式打开文件,文件最大大小为fsize(单位byte)
// 如果文件已经存在,则直接打开
// 如果文件不存在,则新建
func (this *MmapFile) OpenFile(path string, name string, fsize uint32) error {
// 打开文件
f, err := os.OpenFile(filepath.Join(path, name), os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return log.Error("os.OpenFile : %s", err.Error())
}
fstat, _ := f.Stat()
if fstat.Size() < int64(fsize) {
// 产生空洞
_, err = f.Seek(int64(fsize-1), 0)
if err != nil {
return log.Error("file seek : %s", err.Error())
}
_, err = f.WriteString(" ")
if err != nil {
return log.Error("WriteString : %s", err.Error())
}
} else if fstat.Size() > int64(fsize) {
f.Truncate(int64(fsize))
}
// 映射
this.fileMmap, err = mmap.Map(f, mmap.RDWR, 0)
if err != nil {
return log.Error("mmap.Map fail : %s", err.Error())
}
// 映射后,相当于分配了一个[]byte,其大小应该是等于fsize
if uint64(len(this.fileMmap)) != uint64(fsize) {
return log.Error("fileMmap length error [%d] != [%d]",
len(this.fileMmap), fsize)
}
this.filePath = path
this.fileName = name
this.fileSize = fsize
this.fileHandle = f
return nil
}
func (this *MmapFile) Close() error {
err := this.fileMmap.Unmap()
if err != nil {
return err
}
this.fileHandle.Close()
return nil
}
func (this *MmapFile) Flush() error {
err := this.fileMmap.Flush()
if err != nil {
return err
}
return nil
}
// 从offset位置开始写入数字n,占用空间大小根据n反射类型决定
func (this *MmapFile) WriteNum(offset uint32, n interface{}) error {
destType, destSz := IntKindSize(n)
if destSz == 0 {
return log.Error("Mmapfile.WriteNum not a num")
}
if int64(offset)+int64(destSz) > int64(len(this.fileMmap)) {
return log.Error("Mmapfile.WriteNum over length limit")
}
// 从map中分配一个[]byte,已经保证长度足够
// 后面写操作不会引发内存拷贝
buf := this.fileMmap[offset : offset+uint32(destSz)]
order := binary.BigEndian
num := reflect.ValueOf(n)
if !num.IsValid() {
return log.Error("Mmapfile.WriteNum not a num")
}
switch destType {
case reflect.Int8:
buf[0] = byte(num.Int())
case reflect.Uint8:
buf[0] = byte(num.Uint())
case reflect.Int16:
order.PutUint16(buf, uint16(num.Int()))
case reflect.Uint16:
order.PutUint16(buf, uint16(num.Uint()))
case reflect.Int32:
order.PutUint32(buf, uint32(num.Int()))
case reflect.Uint32:
order.PutUint32(buf, uint32(num.Uint()))
case reflect.Int64:
order.PutUint64(buf, uint64(num.Int()))
case reflect.Uint64:
order.PutUint64(buf, uint64(num.Uint()))
default:
return log.Error("MmapFile.WriteNum Wrong Type")
}
/*
switch destSz {
case 1:
buf[0] = byte(num.Uint())
case 2:
order.PutUint16(buf,uint16(num.Uint()))
case 4:
order.PutUint32(buf,uint32(num.Uint()))
case 8:
order.PutUint64(buf,uint64(num.Uint()))
default:
return NewGooseError("MmapFile.WriteNum","Wrong Type","")
}
*/
return nil
/*
switch v := n.(type) {
case int8:
buf[0] = byte(v)
case uint8:
buf[0] = byte(v)
case int16:
order.PutUint16(buf, uint16(v))
case uint16:
order.PutUint16(buf, v)
case int32:
order.PutUint32(buf, uint32(v))
case uint32:
order.PutUint32(buf, v)
case int64:
order.PutUint64(buf, uint64(v))
case uint64:
order.PutUint64(buf, v)
default:
return NewGooseError("MmapFile.WriteNum","Wrong Type","")
}
return nil
*/
}
// 对ReadNum的封装,方便外部直接使用
func (this *MmapFile) ReadUint8(offset uint32) (uint8, error) {
n, err := this.ReadNum(offset, 1)
return uint8(n), err
}
func (this *MmapFile) ReadUint32(offset uint32) (uint32, error) {
n, err := this.ReadNum(offset, 4)
return uint32(n), err
}
func (this *MmapFile) ReadUint64(offset uint32) (uint64, error) {
n, err := this.ReadNum(offset, 8)
return uint64(n), err
}
// 读取offset开始的destSz个字节作为数字返回
func (this *MmapFile) ReadNum(offset uint32, destSz uint32) (uint64, error) {
if destSz == 0 {
return 0, log.Error("Mmapfile.ReadNum not a basic num")
}
if int64(offset)+int64(destSz) > int64(len(this.fileMmap)) {
return 0, log.Error("Mmapfile.ReadNum over length limit")
}
buf := this.fileMmap[offset : offset+uint32(destSz)]
order := binary.BigEndian
switch destSz {
case 1:
return uint64(buf[0]), nil
case 2:
return uint64(order.Uint16(buf)), nil
case 4:
return uint64(order.Uint32(buf)), nil
case 8:
return uint64(order.Uint64(buf)), nil
default:
return 0, log.Error("MmapFile.ReadNum Wrong Type")
}
return 0, nil
/*
switch v := n.(type) {
case *int8:
*v = int8(buf[0])
case *uint8:
*v = buf[0]
case *int16:
*v = int16(order.Uint16(buf))
case *uint16:
*v = uint16(order.Uint16(buf))
case *int32:
*v = int32(order.Uint32(buf))
case *uint32:
*v = uint32(order.Uint32(buf))
case *int64:
*v = int64(order.Uint64(buf))
case *uint64:
*v = uint64(order.Uint64(buf))
default:
return NewGooseError("MmapFile.ReadNum","Wrong Type","")
}
return nil
*/
}
// write max length bytes
func (this *MmapFile) WriteBytes(offset uint32, buf []byte, length uint32) error {
if length > uint32(len(buf)) {
length = uint32(len(buf))
}
if uint64(offset+length) > uint64(len(this.fileMmap)) {
return log.Error("Mmapfile.WriteBytes over length limit")
}
var i uint32
for i = 0; i < length; i++ {
this.fileMmap[offset+i] = buf[i]
}
return nil
}
// read bytes (reference)
func (this *MmapFile) ReadBytes(offset uint32, length uint32) ([]byte, error) {
if uint64(offset+length) > uint64(len(this.fileMmap)) {
return nil, log.Error("Mmapfile.ReadBytes over length limit")
}
return this.fileMmap[offset : offset+length], nil
}
// read bytes (copy slice)
func (this *MmapFile) ReadBytesCopy(offset int32, length int32) ([]byte, error) {
if int64(offset+length) > int64(len(this.fileMmap)) {
return nil, log.Error("Mmapfile.ReadBytes over length limit")
}
newbuf := make([]byte, length)
copy(newbuf, this.fileMmap[offset:offset+length])
return newbuf, nil
}
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */