Skip to content

Commit 8fd878a

Browse files
committed
Internal API tweaks.
1 parent 9b769d9 commit 8fd878a

File tree

10 files changed

+51
-38
lines changed

10 files changed

+51
-38
lines changed

ext/stats/TODO.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ https://sqlite.org/lang_aggfunc.html
4646

4747
https://sqlite.org/windowfunctions.html#builtins
4848

49+
## Boolean aggregates
50+
51+
- [ ] `ALL(boolean)`
52+
- [ ] `ANY(boolean)`
53+
- [ ] `EVERY(boolean)`
54+
- [ ] `SOME(boolean)`
55+
4956
## Additional aggregates
5057

5158
- [X] `MEDIAN(expression)`

ext/stats/quantile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ func newQuantile(kind int) func() sqlite3.AggregateFunction {
2222
}
2323

2424
type quantile struct {
25-
kind int
2625
nums []float64
2726
arg1 []byte
27+
kind int
2828
}
2929

3030
func (q *quantile) Step(ctx sqlite3.Context, arg ...sqlite3.Value) {

internal/util/const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package util
22

3-
// https://sqlite.com/matrix/rescode.html
3+
// https://sqlite.com/rescode.html
44
const (
55
OK = 0 /* Successful result */
66

vfs/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ To allow `mmap` to work, each connection needs to reserve up to 4GB of address s
5454
To limit the address space each connection reserves,
5555
use [`WithMemoryLimitPages`](../tests/testcfg/testcfg.go).
5656

57+
With [BSD locks](https://man.freebsd.org/cgi/man.cgi?query=flock&sektion=2)
58+
a WAL database can only be accessed by a single proccess.
59+
Other processes that attempt to access a database locked with BSD locks,
60+
will fail with the `SQLITE_PROTOCOL` error code.
61+
5762
Otherwise, [WAL support is limited](https://sqlite.org/wal.html#noshm),
5863
and `EXCLUSIVE` locking mode must be set to create, read, and write WAL databases.
5964
To use `EXCLUSIVE` locking mode with the
@@ -79,8 +84,9 @@ The VFS can be customized with a few build tags:
7984
- `sqlite3_noshm` disables shared memory on all platforms.
8085

8186
> [!IMPORTANT]
82-
> The default configuration of this package is compatible with
83-
> the standard [Unix and Windows SQLite VFSes](https://sqlite.org/vfs.html#multiple_vfses);
84-
> `sqlite3_flock` is compatible with the [`unix-flock` VFS](https://sqlite.org/compile.html#enable_locking_style).
85-
> If incompatible file locking is used, accessing databases concurrently with _other_ SQLite libraries
86-
> will eventually corrupt data.
87+
> The default configuration of this package is compatible with the standard
88+
> [Unix and Windows SQLite VFSes](https://sqlite.org/vfs.html#multiple_vfses);
89+
> `sqlite3_flock` builds are compatible with the
90+
> [`unix-flock` VFS](https://sqlite.org/compile.html#enable_locking_style).
91+
> If incompatible file locking is used, accessing databases concurrently with
92+
> _other_ SQLite libraries will eventually corrupt data.

vfs/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ type FileSharedMemory interface {
168168
// SharedMemory is a shared-memory WAL-index implementation.
169169
// Use [NewSharedMemory] to create a shared-memory.
170170
type SharedMemory interface {
171-
shmMap(context.Context, api.Module, int32, int32, bool) (uint32, error)
172-
shmLock(int32, int32, _ShmFlag) error
171+
shmMap(context.Context, api.Module, int32, int32, bool) (uint32, _ErrorCode)
172+
shmLock(int32, int32, _ShmFlag) _ErrorCode
173173
shmUnmap(bool)
174174
io.Closer
175175
}

vfs/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const (
4747
_IOERR_SHMMAP _ErrorCode = util.IOERR_SHMMAP
4848
_IOERR_SEEK _ErrorCode = util.IOERR_SEEK
4949
_IOERR_DELETE_NOENT _ErrorCode = util.IOERR_DELETE_NOENT
50+
_IOERR_GETTEMPPATH _ErrorCode = util.IOERR_GETTEMPPATH
5051
_IOERR_BEGIN_ATOMIC _ErrorCode = util.IOERR_BEGIN_ATOMIC
5152
_IOERR_COMMIT_ATOMIC _ErrorCode = util.IOERR_COMMIT_ATOMIC
5253
_IOERR_ROLLBACK_ATOMIC _ErrorCode = util.IOERR_ROLLBACK_ATOMIC

vfs/file.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ func (vfsOS) OpenFilename(name *Filename, flags OpenFlag) (File, OpenFlag, error
9595
f, err = osutil.OpenFile(name.String(), oflags, 0666)
9696
}
9797
if err != nil {
98+
if name == nil {
99+
return nil, flags, _IOERR_GETTEMPPATH
100+
}
98101
if errors.Is(err, syscall.EISDIR) {
99102
return nil, flags, _CANTOPEN_ISDIR
100103
}

vfs/shm.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type vfsShm struct {
5151
readOnly bool
5252
}
5353

54-
func (s *vfsShm) shmOpen() error {
54+
func (s *vfsShm) shmOpen() _ErrorCode {
5555
if s.File == nil {
5656
var flag int
5757
if s.readOnly {
@@ -86,17 +86,17 @@ func (s *vfsShm) shmOpen() error {
8686
if rc := osReadLock(s.File, _SHM_DMS, 1, 0); rc != _OK {
8787
return rc
8888
}
89-
return nil
89+
return _OK
9090
}
9191

92-
func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, extend bool) (uint32, error) {
92+
func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, extend bool) (uint32, _ErrorCode) {
9393
// Ensure size is a multiple of the OS page size.
9494
if int(size)&(unix.Getpagesize()-1) != 0 {
9595
return 0, _IOERR_SHMMAP
9696
}
9797

98-
if err := s.shmOpen(); err != nil {
99-
return 0, err
98+
if rc := s.shmOpen(); rc != _OK {
99+
return 0, rc
100100
}
101101

102102
// Check if file is big enough.
@@ -106,7 +106,7 @@ func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, ext
106106
}
107107
if n := (int64(id) + 1) * int64(size); n > o {
108108
if !extend {
109-
return 0, nil
109+
return 0, _OK
110110
}
111111
err := osAllocate(s.File, n)
112112
if err != nil {
@@ -122,13 +122,13 @@ func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, ext
122122
}
123123
r, err := util.MapRegion(ctx, mod, s.File, int64(id)*int64(size), size, prot)
124124
if err != nil {
125-
return 0, err
125+
return 0, _IOERR_SHMMAP
126126
}
127127
s.regions = append(s.regions, r)
128-
return r.Ptr, nil
128+
return r.Ptr, _OK
129129
}
130130

131-
func (s *vfsShm) shmLock(offset, n int32, flags _ShmFlag) error {
131+
func (s *vfsShm) shmLock(offset, n int32, flags _ShmFlag) _ErrorCode {
132132
// Argument check.
133133
if n <= 0 || offset < 0 || offset+n > _SHM_NLOCK {
134134
panic(util.AssertErr())

vfs/shm_bsd.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ func (s *vfsShm) Close() error {
9696
return err
9797
}
9898

99-
func (s *vfsShm) shmOpen() (err error) {
99+
func (s *vfsShm) shmOpen() (rc _ErrorCode) {
100100
if s.vfsShmFile != nil {
101-
return nil
101+
return _OK
102102
}
103103

104104
// Open file read-write, as it will be shared.
@@ -123,13 +123,13 @@ func (s *vfsShm) shmOpen() (err error) {
123123
if g != nil && os.SameFile(fi, g.info) {
124124
g.refs++
125125
s.vfsShmFile = g
126-
return nil
126+
return _OK
127127
}
128128
}
129129

130130
// Lock and truncate the file, if not readonly.
131131
if s.readOnly {
132-
err = _READONLY_CANTINIT
132+
rc = _READONLY_CANTINIT
133133
} else {
134134
if rc := osWriteLock(f, 0, 0, 0); rc != _OK {
135135
return rc
@@ -156,17 +156,17 @@ func (s *vfsShm) shmOpen() (err error) {
156156
if add {
157157
vfsShmFiles = append(vfsShmFiles, s.vfsShmFile)
158158
}
159-
return err
159+
return rc
160160
}
161161

162-
func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, extend bool) (uint32, error) {
162+
func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, extend bool) (uint32, _ErrorCode) {
163163
// Ensure size is a multiple of the OS page size.
164164
if int(size)&(unix.Getpagesize()-1) != 0 {
165165
return 0, _IOERR_SHMMAP
166166
}
167167

168-
if err := s.shmOpen(); err != nil {
169-
return 0, err
168+
if rc := s.shmOpen(); rc != _OK {
169+
return 0, rc
170170
}
171171

172172
// Check if file is big enough.
@@ -176,7 +176,7 @@ func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, ext
176176
}
177177
if n := (int64(id) + 1) * int64(size); n > o {
178178
if !extend {
179-
return 0, nil
179+
return 0, _OK
180180
}
181181
err := osAllocate(s.File, n)
182182
if err != nil {
@@ -192,13 +192,13 @@ func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, ext
192192
}
193193
r, err := util.MapRegion(ctx, mod, s.File, int64(id)*int64(size), size, prot)
194194
if err != nil {
195-
return 0, err
195+
return 0, _IOERR_SHMMAP
196196
}
197197
s.regions = append(s.regions, r)
198-
return r.Ptr, nil
198+
return r.Ptr, _OK
199199
}
200200

201-
func (s *vfsShm) shmLock(offset, n int32, flags _ShmFlag) error {
201+
func (s *vfsShm) shmLock(offset, n int32, flags _ShmFlag) _ErrorCode {
202202
s.lockMtx.Lock()
203203
defer s.lockMtx.Unlock()
204204

@@ -235,7 +235,7 @@ func (s *vfsShm) shmLock(offset, n int32, flags _ShmFlag) error {
235235
}
236236
}
237237

238-
return nil
238+
return _OK
239239
}
240240

241241
func (s *vfsShm) shmUnmap(delete bool) {

vfs/vfs.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,18 +397,14 @@ func vfsShmBarrier(ctx context.Context, mod api.Module, pFile uint32) {
397397

398398
func vfsShmMap(ctx context.Context, mod api.Module, pFile uint32, iRegion, szRegion int32, bExtend, pp uint32) _ErrorCode {
399399
shm := vfsFileGet(ctx, mod, pFile).(FileSharedMemory).SharedMemory()
400-
p, err := shm.shmMap(ctx, mod, iRegion, szRegion, bExtend != 0)
401-
if err != nil {
402-
return vfsErrorCode(err, _IOERR_SHMMAP)
403-
}
400+
p, rc := shm.shmMap(ctx, mod, iRegion, szRegion, bExtend != 0)
404401
util.WriteUint32(mod, pp, p)
405-
return _OK
402+
return rc
406403
}
407404

408405
func vfsShmLock(ctx context.Context, mod api.Module, pFile uint32, offset, n int32, flags _ShmFlag) _ErrorCode {
409406
shm := vfsFileGet(ctx, mod, pFile).(FileSharedMemory).SharedMemory()
410-
err := shm.shmLock(offset, n, flags)
411-
return vfsErrorCode(err, _IOERR_SHMLOCK)
407+
return shm.shmLock(offset, n, flags)
412408
}
413409

414410
func vfsShmUnmap(ctx context.Context, mod api.Module, pFile, bDelete uint32) _ErrorCode {

0 commit comments

Comments
 (0)