-
Notifications
You must be signed in to change notification settings - Fork 12
/
pager_unix.go
117 lines (100 loc) · 2.3 KB
/
pager_unix.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
// +build !windows
// unix implementation of the `pager` interface (the file reader) with POSIX
// advisory locking
package db
import (
"errors"
"os"
"golang.org/x/exp/mmap"
"golang.org/x/sys/unix"
)
const (
seekSet = 0 // should be defined in syscall
)
type filePager struct {
f *os.File
readLock *unix.Flock_t
mm *mmap.ReaderAt
}
func newFilePager(file string) (*filePager, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
mm, err := mmap.Open(file)
if err != nil {
f.Close()
return nil, err
}
return &filePager{
f: f,
mm: mm,
}, nil
}
// pages start counting at 1
func (f *filePager) page(id int, pagesize int) ([]byte, error) {
buf := make([]byte, pagesize)
_, err := f.mm.ReadAt(buf[:], int64(id-1)*int64(pagesize))
return buf, err
}
func (f *filePager) lock(flock *unix.Flock_t) error {
return unix.FcntlFlock(f.f.Fd(), unix.F_SETLK, flock)
}
func (f *filePager) RLock() error {
// Set a 'SHARED' lock, following unixLock() logic from sqlite3.c
if f.readLock != nil {
return errors.New("trying to lock a locked lock") // panic?
}
// - get PENDING lock
pending := &unix.Flock_t{
Type: unix.F_RDLCK,
Whence: seekSet,
Start: sqlitePendingByte,
Len: 1,
}
if err := f.lock(pending); err != nil {
return err
}
defer func() {
// - drop the pending lock. No idea what to do with the error :/
pending.Type = unix.F_UNLCK
f.lock(pending)
}()
// Get the read-lock
read := &unix.Flock_t{
Type: unix.F_RDLCK,
Whence: seekSet,
Start: sqliteSharedFirst,
Len: sqliteSharedSize,
}
if err := f.lock(read); err != nil {
return err
}
f.readLock = read
return nil
}
func (f *filePager) RUnlock() error {
if f.readLock == nil {
return errors.New("trying to unlock an unlocked lock") // panic?
}
f.readLock.Type = unix.F_UNLCK
f.lock(f.readLock)
f.readLock = nil
return nil
}
// True if there is a 'reserved' lock on the database, by any process.
func (f *filePager) CheckReservedLock() (bool, error) {
// per SQLite's unixCheckReservedLock()
lock := &unix.Flock_t{
Type: unix.F_WRLCK,
Whence: seekSet,
Start: sqliteReservedByte,
Len: 1,
}
err := unix.FcntlFlock(f.f.Fd(), unix.F_GETLK, lock)
return lock.Type != unix.F_UNLCK, err
}
func (f *filePager) Close() error {
f.f.Close()
return f.mm.Close()
}