Skip to content

Commit 8532e88

Browse files
committed
refactor sqlite locking constants
constants are shared between os lock implementations, so move them out of os-specific files also camelcases them
1 parent 3b19a46 commit 8532e88

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

db/pager.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package db
22

3+
const (
4+
sqlitePendingByte = 0x40000000
5+
sqliteReservedByte = sqlitePendingByte + 1
6+
sqliteSharedFirst = sqlitePendingByte + 2
7+
sqliteSharedSize = 510
8+
)
9+
310
type pager interface {
411
// load a page from storage.
512
page(n int, pagesize int) ([]byte, error)

db/pager_unix.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ import (
1414
)
1515

1616
const (
17-
seek_set = 0 // should be defined in syscall
18-
sqlite_pending_byte = 0x40000000
19-
sqlite_reserved_byte = sqlite_pending_byte + 1
20-
sqlite_shared_first = sqlite_pending_byte + 2
21-
sqlite_shared_size = 510
17+
seekSet = 0 // should be defined in syscall
2218
)
2319

2420
type filePager struct {
@@ -64,8 +60,8 @@ func (f *filePager) RLock() error {
6460
// - get PENDING lock
6561
pending := &unix.Flock_t{
6662
Type: unix.F_RDLCK,
67-
Whence: seek_set,
68-
Start: sqlite_pending_byte,
63+
Whence: seekSet,
64+
Start: sqlitePendingByte,
6965
Len: 1,
7066
}
7167
if err := f.lock(pending); err != nil {
@@ -81,9 +77,9 @@ func (f *filePager) RLock() error {
8177
// Get the read-lock
8278
read := &unix.Flock_t{
8379
Type: unix.F_RDLCK,
84-
Whence: seek_set,
85-
Start: sqlite_shared_first,
86-
Len: sqlite_shared_size,
80+
Whence: seekSet,
81+
Start: sqliteSharedFirst,
82+
Len: sqliteSharedSize,
8783
}
8884
if err := f.lock(read); err != nil {
8985
return err
@@ -107,8 +103,8 @@ func (f *filePager) CheckReservedLock() (bool, error) {
107103
// per SQLite's unixCheckReservedLock()
108104
lock := &unix.Flock_t{
109105
Type: unix.F_WRLCK,
110-
Whence: seek_set,
111-
Start: sqlite_reserved_byte,
106+
Whence: seekSet,
107+
Start: sqliteReservedByte,
112108
Len: 1,
113109
}
114110
err := unix.FcntlFlock(f.f.Fd(), unix.F_GETLK, lock)

db/pager_windows.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ import (
1717
"golang.org/x/sys/windows"
1818
)
1919

20-
const (
21-
sqlite_pending_byte = 0x40000000
22-
sqlite_reserved_byte = sqlite_pending_byte + 1
23-
sqlite_shared_first = sqlite_pending_byte + 2
24-
sqlite_shared_size = 510
25-
)
26-
2720
const (
2821
reserved uint32 = 0
2922
)
@@ -94,7 +87,7 @@ func (f *filePager) RLock() error {
9487
//
9588
// source: https://github.com/sqlite/sqlite/blob/c398c65bee850b6b8f24a44852872a27f114535d/src/os_win.c#L3280
9689
for i := 0; i < 3; i++ {
97-
err := f.lock(sqlite_pending_byte, 1)
90+
err := f.lock(sqlitePendingByte, 1)
9891
if err == nil {
9992
break
10093
}
@@ -106,11 +99,11 @@ func (f *filePager) RLock() error {
10699

107100
defer func() {
108101
// - drop the pending lock. No idea what to do with the error :/
109-
f.unlock(sqlite_pending_byte, 1)
102+
f.unlock(sqlitePendingByte, 1)
110103
}()
111104

112105
// Get the read-lock
113-
if err := f.lock(sqlite_shared_first, sqlite_shared_size); err != nil {
106+
if err := f.lock(sqliteSharedFirst, sqliteSharedSize); err != nil {
114107
return err
115108
}
116109
f.state = Locked
@@ -121,7 +114,7 @@ func (f *filePager) RUnlock() error {
121114
if f.state == Unlocked {
122115
return errors.New("trying to unlock an unlocked lock") // panic?
123116
}
124-
if err := f.unlock(sqlite_shared_first, sqlite_shared_size); err != nil {
117+
if err := f.unlock(sqliteSharedFirst, sqliteSharedSize); err != nil {
125118
return err
126119
}
127120
f.state = Unlocked
@@ -131,9 +124,9 @@ func (f *filePager) RUnlock() error {
131124
// True if there is a 'reserved' lock on the database, by any process.
132125
func (f *filePager) CheckReservedLock() (bool, error) {
133126
// per SQLite's winCheckReservedLock()
134-
err := f.lock(sqlite_reserved_byte, 1)
127+
err := f.lock(sqliteReservedByte, 1)
135128
if err == nil {
136-
f.unlock(sqlite_reserved_byte, 1)
129+
f.unlock(sqliteReservedByte, 1)
137130
}
138131
return err == nil, err
139132
}

0 commit comments

Comments
 (0)