-
Notifications
You must be signed in to change notification settings - Fork 31
/
arena.go
52 lines (42 loc) · 1.28 KB
/
arena.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
package bigqueue
import (
"fmt"
"os"
"syscall"
"github.com/grandecola/mmap"
)
// newArena returns pointer to a mapped file. It takes a file location and mmaps it.
// If file location does not exist, it creates a file of given size.
func newArena(file string, size int) (*mmap.File, error) {
fd, err := openOrCreateFile(file, int64(size))
if err != nil {
return nil, err
}
m, err := mmap.NewSharedFileMmap(fd, 0, size, syscall.PROT_READ|syscall.PROT_WRITE)
if err != nil {
return nil, fmt.Errorf("error in mmaping a file :: %w", err)
}
// We can close the file descriptor here.
if err := fd.Close(); err != nil {
return nil, fmt.Errorf("error in closing the fd :: %w", err)
}
return m, nil
}
// openOrCreateFile opens the file if it exists,
// otherwise creates a new file of a given size.
func openOrCreateFile(file string, size int64) (*os.File, error) {
fd, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR, cFilePerm)
if err != nil {
return nil, fmt.Errorf("error in creating/opening file :: %w", err)
}
info, err := fd.Stat()
if err != nil {
return nil, fmt.Errorf("error in finding info for file :: %w", err)
}
if info.Size() < size {
if err := fd.Truncate(size); err != nil {
return nil, fmt.Errorf("error in truncating file :: %w", err)
}
}
return fd, nil
}