-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmap_file.go
95 lines (81 loc) · 1.87 KB
/
mmap_file.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
package simpledb
import (
"errors"
"os"
)
const initialMmapSize = 128 * (1 << 20) // 128MB
type MmapFile struct {
*os.File
data []byte
fileSize int64
}
// Todo: add @flag, @perm
func OpenMmapFile(name string, mmapSize int64) (*MmapFile, error) {
f, err := os.OpenFile(name, os.O_CREATE | os.O_RDWR, os.FileMode(0640))
if err != nil {
return nil, err
}
data, err := mmap(f, mmapSize)
if err!=nil {
return nil, err
}
stat, err := f.Stat()
if err != nil {
return nil, err
}
return &MmapFile{f, data, stat.Size()}, nil
}
// append the data at the offset of m.dataSize
// called when writing datalog
func (m *MmapFile) Append (bs []byte) {
//m.data = append(m.data, bs...)
copy(m.data[m.fileSize:m.fileSize+int64(len(bs))], bs)
m.fileSize += int64(len(bs))
}
// write the data at the specific offset
// called when writing buckets
func (m *MmapFile) WriteAt(offset int64, bs []byte) {
//m.data = append(m.data, bs...)
copy(m.data[offset:offset+int64(len(bs))], bs)
// here, the fileSize also counts the padding
//m.fileSize = offset+int64(len(bs))
}
// called by datalog
func (m * MmapFile) Read(offset, len int64) ([]byte, error) {
if offset+len > m.fileSize {
return nil, errors.New("read is out of range")
}
return m.data[offset: offset+len], nil
}
// called by buckets
func (m * MmapFile) ReadRandom(offset, len int64) ([]byte, error) {
stat, _ := m.File.Stat()
if offset+len > stat.Size() {
err := m.Grow(offset+len)
if err != nil {
return nil, err
}
m.fileSize = offset+len
}
return m.data[offset: offset+len], nil
}
func (m *MmapFile) Grow(fileSize int64) error {
err := grow(m.File, fileSize)
if err != nil {
return err
}
// unmap it
err = munmap(m.data)
if err !=nil {
return err
}
data, err := mmap(m.File, fileSize)
if err != nil {
return err
}
m.data = data
return nil
}
func (m *MmapFile) FileSize() int64 {
return m.fileSize
}