forked from nutsdb/nutsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_manager.go
87 lines (72 loc) · 2.07 KB
/
file_manager.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
package nutsdb
import (
"github.com/xujiajun/mmap-go"
)
// fileManager holds the fd cache and file-related operations go through the manager to obtain the file processing object
type fileManager struct {
rwMode RWMode
fdm *fdManager
segmentSize int64
}
// newFileManager will create a newFileManager object
func newFileManager(rwMode RWMode, maxFdNums int, cleanThreshold float64, segmentSize int64) (fm *fileManager) {
fm = &fileManager{
rwMode: rwMode,
fdm: newFdm(maxFdNums, cleanThreshold),
segmentSize: segmentSize,
}
return fm
}
// getDataFile will return a DataFile Object
func (fm *fileManager) getDataFile(path string, capacity int64) (datafile *DataFile, err error) {
if capacity <= 0 {
return nil, ErrCapacity
}
var rwManager RWManager
if fm.rwMode == FileIO {
rwManager, err = fm.getFileRWManager(path, capacity, fm.segmentSize)
if err != nil {
return nil, err
}
}
if fm.rwMode == MMap {
rwManager, err = fm.getMMapRWManager(path, capacity, fm.segmentSize)
if err != nil {
return nil, err
}
}
return NewDataFile(path, rwManager), nil
}
// getFileRWManager will return a FileIORWManager Object
func (fm *fileManager) getFileRWManager(path string, capacity int64, segmentSize int64) (*FileIORWManager, error) {
fd, err := fm.fdm.getFd(path)
if err != nil {
return nil, err
}
err = Truncate(path, capacity, fd)
if err != nil {
return nil, err
}
return &FileIORWManager{fd: fd, path: path, fdm: fm.fdm, segmentSize: segmentSize}, nil
}
// getMMapRWManager will return a MMapRWManager Object
func (fm *fileManager) getMMapRWManager(path string, capacity int64, segmentSize int64) (*MMapRWManager, error) {
fd, err := fm.fdm.getFd(path)
if err != nil {
return nil, err
}
err = Truncate(path, capacity, fd)
if err != nil {
return nil, err
}
m, err := mmap.Map(fd, mmap.RDWR, 0)
if err != nil {
return nil, err
}
return &MMapRWManager{m: m, path: path, fdm: fm.fdm, segmentSize: segmentSize}, nil
}
// close will close fdm resource
func (fm *fileManager) close() error {
err := fm.fdm.close()
return err
}