Skip to content

Commit beba988

Browse files
committed
Multiple fixes.
1 parent 992676d commit beba988

File tree

10 files changed

+58
-57
lines changed

10 files changed

+58
-57
lines changed

driver/driver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name
327327
return newResult(c.Conn), nil
328328
}
329329

330-
func (*conn) CheckNamedValue(arg *driver.NamedValue) error {
330+
func (c *conn) CheckNamedValue(arg *driver.NamedValue) error {
331331
return nil
332332
}
333333

internal/util/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88

99
type moduleKey struct{}
1010
type moduleState struct {
11-
handleState
1211
mmapState
12+
handleState
1313
}
1414

1515
func NewContext(ctx context.Context) context.Context {

tests/conn_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestConn_SetInterrupt(t *testing.T) {
139139
SELECT 0, 1
140140
UNION ALL
141141
SELECT next, curr + next FROM fibonacci
142-
LIMIT 1e6
142+
LIMIT 1e7
143143
)
144144
SELECT min(curr) FROM fibonacci
145145
`)

vfs/adiantum/hbsh.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ func (h *hbshVFS) OpenFilename(name *vfs.Filename, flags vfs.OpenFlag) (file vfs
5151
}
5252

5353
const (
54-
blockSize = 4096
5554
tweakSize = 8
55+
blockSize = 4096
5656
)
5757

5858
type hbshFile struct {
5959
vfs.File
6060
hbsh *hbsh.HBSH
6161
reset HBSHCreator
62-
block [blockSize]byte
6362
tweak [tweakSize]byte
63+
block [blockSize]byte
6464
}
6565

6666
func (h *hbshFile) Pragma(name string, value string) (string, error) {
@@ -95,8 +95,8 @@ func (h *hbshFile) ReadAt(p []byte, off int64) (n int, err error) {
9595
return 0, sqlite3.CANTOPEN
9696
}
9797

98-
min := (off) &^ (blockSize - 1) // round down
99-
max := (off + int64(len(p)) + blockSize - 1) &^ (blockSize - 1) // round up
98+
min := (off) &^ (blockSize - 1) // round down
99+
max := (off + int64(len(p)) + (blockSize - 1)) &^ (blockSize - 1) // round up
100100

101101
// Read one block at a time.
102102
for ; min < max; min += blockSize {
@@ -125,8 +125,8 @@ func (h *hbshFile) WriteAt(p []byte, off int64) (n int, err error) {
125125
return 0, sqlite3.READONLY
126126
}
127127

128-
min := (off) &^ (blockSize - 1) // round down
129-
max := (off + int64(len(p)) + blockSize - 1) &^ (blockSize - 1) // round up
128+
min := (off) &^ (blockSize - 1) // round down
129+
max := (off + int64(len(p)) + (blockSize - 1)) &^ (blockSize - 1) // round up
130130

131131
// Write one block at a time.
132132
for ; min < max; min += blockSize {
@@ -143,13 +143,13 @@ func (h *hbshFile) WriteAt(p []byte, off int64) (n int, err error) {
143143
// Writing past the EOF:
144144
// We're either appending an entirely new block,
145145
// or the final block was only partially written.
146-
// A partially written block can't be decripted,
146+
// A partially written block can't be decrypted,
147147
// and is as good as corrupt.
148148
// Either way, zero pad the file to the next block size.
149149
clear(data)
150+
} else {
151+
data = h.hbsh.Decrypt(h.block[:], h.tweak[:])
150152
}
151-
152-
data = h.hbsh.Decrypt(h.block[:], h.tweak[:])
153153
if off > min {
154154
data = data[off-min:]
155155
}
@@ -172,7 +172,7 @@ func (h *hbshFile) WriteAt(p []byte, off int64) (n int, err error) {
172172
}
173173

174174
func (h *hbshFile) Truncate(size int64) error {
175-
size = (size + blockSize - 1) &^ (blockSize - 1) // round up
175+
size = (size + (blockSize - 1)) &^ (blockSize - 1) // round up
176176
return h.File.Truncate(size)
177177
}
178178

@@ -188,25 +188,25 @@ func (h *hbshFile) DeviceCharacteristics() vfs.DeviceCharacteristic {
188188
vfs.IOCAP_BATCH_ATOMIC)
189189
}
190190

191+
// Wrap optional methods.
192+
191193
func (h *hbshFile) SharedMemory() vfs.SharedMemory {
192-
if shm, ok := h.File.(vfs.FileSharedMemory); ok {
193-
return shm.SharedMemory()
194+
if f, ok := h.File.(vfs.FileSharedMemory); ok {
195+
return f.SharedMemory()
194196
}
195197
return nil
196198
}
197199

198-
// Wrap optional methods.
199-
200200
func (h *hbshFile) ChunkSize(size int) {
201201
if f, ok := h.File.(vfs.FileChunkSize); ok {
202-
size = (size + blockSize - 1) &^ (blockSize - 1) // round up
202+
size = (size + (blockSize - 1)) &^ (blockSize - 1) // round up
203203
f.ChunkSize(size)
204204
}
205205
}
206206

207207
func (h *hbshFile) SizeHint(size int64) error {
208208
if f, ok := h.File.(vfs.FileSizeHint); ok {
209-
size = (size + blockSize - 1) &^ (blockSize - 1) // round up
209+
size = (size + (blockSize - 1)) &^ (blockSize - 1) // round up
210210
return f.SizeHint(size)
211211
}
212212
return sqlite3.NOTFOUND

vfs/api.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,16 @@ type FileCheckpoint interface {
157157

158158
// FileSharedMemory extends File to possibly implement
159159
// shared-memory for the WAL-index.
160+
// The same shared-memory instance must be returned
161+
// for the entire life of the file.
160162
// It's OK for SharedMemory to return nil.
161163
type FileSharedMemory interface {
162164
File
163165
SharedMemory() SharedMemory
164166
}
165167

166168
// SharedMemory is a shared-memory WAL-index implementation.
167-
// This cannot be externally implemented.
169+
// Use [NewSharedMemory] to create a shared-memory.
168170
type SharedMemory interface {
169171
shmMap(context.Context, api.Module, int32, int32, bool) (uint32, error)
170172
shmLock(int32, int32, _ShmFlag) error

vfs/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (f *vfsFile) Size() (int64, error) {
176176
return f.Seek(0, io.SeekEnd)
177177
}
178178

179-
func (*vfsFile) SectorSize() int {
179+
func (f *vfsFile) SectorSize() int {
180180
return _DEFAULT_SECTOR_SIZE
181181
}
182182

vfs/memdb/memdb.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func (memVFS) Open(name string, flags vfs.OpenFlag) (vfs.File, vfs.OpenFlag, err
2828
// We refuse to open all other file types,
2929
// but returning OPEN_MEMORY means SQLite won't ask us to.
3030
const types = vfs.OPEN_MAIN_DB |
31-
vfs.OPEN_TRANSIENT_DB |
3231
vfs.OPEN_TEMP_DB |
3332
vfs.OPEN_TEMP_JOURNAL
3433
if flags&types == 0 {
@@ -173,7 +172,7 @@ func (m *memFile) truncate(size int64) error {
173172
return nil
174173
}
175174

176-
func (*memFile) Sync(flag vfs.SyncFlag) error {
175+
func (m *memFile) Sync(flag vfs.SyncFlag) error {
177176
return nil
178177
}
179178

@@ -263,11 +262,11 @@ func (m *memFile) CheckReservedLock() (bool, error) {
263262
return m.reserved != nil, nil
264263
}
265264

266-
func (*memFile) SectorSize() int {
265+
func (m *memFile) SectorSize() int {
267266
return sectorSize
268267
}
269268

270-
func (*memFile) DeviceCharacteristics() vfs.DeviceCharacteristic {
269+
func (m *memFile) DeviceCharacteristics() vfs.DeviceCharacteristic {
271270
return vfs.IOCAP_ATOMIC |
272271
vfs.IOCAP_SEQUENTIAL |
273272
vfs.IOCAP_SAFE_APPEND |

vfs/shm.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ import (
1212
"golang.org/x/sys/unix"
1313
)
1414

15-
// SupportsSharedMemory is true on platforms that support shared memory.
16-
// To enable shared memory support on those platforms,
17-
// you need to set the appropriate [wazero.RuntimeConfig];
18-
// otherwise, [EXCLUSIVE locking mode] is activated automatically
19-
// to use [WAL without shared-memory].
15+
// SupportsSharedMemory is false on platforms that do not support shared memory.
16+
// To use [WAL without shared-memory], you need to set [EXCLUSIVE locking mode].
2017
//
2118
// [WAL without shared-memory]: https://sqlite.org/wal.html#noshm
2219
// [EXCLUSIVE locking mode]: https://sqlite.org/pragma.html#pragma_locking_mode
@@ -31,12 +28,14 @@ const (
3128
func (f *vfsFile) SharedMemory() SharedMemory { return f.shm }
3229

3330
// NewSharedMemory returns a shared-memory WAL-index
34-
// backed a file with the given path.
35-
// It may return nil if shared-memory is not supported,
31+
// backed by a file with the given path.
32+
// It will return nil if shared-memory is not supported,
3633
// or not appropriate for the given flags.
37-
// Only [OPEN_MAIN_DB] databases support WAL mode.
34+
// Only [OPEN_MAIN_DB] databases may need a WAL-index.
35+
// You must ensure all concurrent accesses to a database
36+
// use shared-memory instances created with the same path.
3837
func NewSharedMemory(path string, flags OpenFlag) SharedMemory {
39-
if flags&OPEN_MAIN_DB == 0 {
38+
if flags&OPEN_MAIN_DB == 0 || flags&(OPEN_DELETEONCLOSE|OPEN_MEMORY) != 0 {
4039
return nil
4140
}
4241
return &vfsShm{
@@ -47,8 +46,8 @@ func NewSharedMemory(path string, flags OpenFlag) SharedMemory {
4746

4847
type vfsShm struct {
4948
*os.File
50-
regions []*util.MappedRegion
5149
path string
50+
regions []*util.MappedRegion
5251
readOnly bool
5352
}
5453

@@ -154,6 +153,10 @@ func (s *vfsShm) shmLock(offset, n int32, flags _ShmFlag) error {
154153
}
155154

156155
func (s *vfsShm) shmUnmap(delete bool) {
156+
if s.File == nil {
157+
return
158+
}
159+
157160
// Unmap regions.
158161
for _, r := range s.regions {
159162
r.Unmap()
@@ -162,9 +165,9 @@ func (s *vfsShm) shmUnmap(delete bool) {
162165
s.regions = s.regions[:0]
163166

164167
// Close the file.
165-
if delete && s.File != nil {
168+
defer s.Close()
169+
if delete {
166170
os.Remove(s.Name())
167171
}
168-
s.Close()
169172
s.File = nil
170173
}

vfs/shm_other.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22

33
package vfs
44

5-
// SupportsSharedMemory is true on platforms that support shared memory.
6-
// To enable shared memory support on those platforms,
7-
// you need to set the appropriate [wazero.RuntimeConfig];
8-
// otherwise, [EXCLUSIVE locking mode] is activated automatically
9-
// to use [WAL without shared-memory].
5+
// SupportsSharedMemory is false on platforms that do not support shared memory.
6+
// To use [WAL without shared-memory], you need to set [EXCLUSIVE locking mode].
107
//
118
// [WAL without shared-memory]: https://sqlite.org/wal.html#noshm
129
// [EXCLUSIVE locking mode]: https://sqlite.org/pragma.html#pragma_locking_mode
1310
const SupportsSharedMemory = false
1411

1512
// NewSharedMemory returns a shared-memory WAL-index
16-
// backed a file with the given path.
17-
// It may return nil if shared-memory is not supported,
13+
// backed by a file with the given path.
14+
// It will return nil if shared-memory is not supported,
1815
// or not appropriate for the given flags.
19-
// Only [OPEN_MAIN_DB] databases support WAL mode.
16+
// Only [OPEN_MAIN_DB] databases may need a WAL-index.
17+
// You must ensure all concurrent accesses to a database
18+
// use shared-memory instances created with the same path.
2019
func NewSharedMemory(path string, flags OpenFlag) SharedMemory {
2120
return nil
2221
}

vfs/vfs.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ func vfsOpen(ctx context.Context, mod api.Module, pVfs, zPath, pFile uint32, fla
146146
} else {
147147
file, flags, err = vfs.Open(path, flags)
148148
}
149-
150149
if err != nil {
151150
return vfsErrorCode(err, _CANTOPEN)
152151
}
@@ -157,14 +156,13 @@ func vfsOpen(ctx context.Context, mod api.Module, pVfs, zPath, pFile uint32, fla
157156
file.SetPowersafeOverwrite(b)
158157
}
159158
}
160-
159+
if file, ok := file.(FileSharedMemory); ok &&
160+
pOutVFS != 0 && file.SharedMemory() != nil {
161+
util.WriteUint32(mod, pOutVFS, 1)
162+
}
161163
if pOutFlags != 0 {
162164
util.WriteUint32(mod, pOutFlags, uint32(flags))
163165
}
164-
if f, ok := file.(FileSharedMemory); ok && flags&OPEN_MAIN_DB != 0 &&
165-
pOutVFS != 0 && f.SharedMemory() != nil {
166-
util.WriteUint32(mod, pOutVFS, 1)
167-
}
168166
vfsFileRegister(ctx, mod, pFile, file)
169167
return _OK
170168
}
@@ -398,8 +396,8 @@ func vfsShmBarrier(ctx context.Context, mod api.Module, pFile uint32) {
398396
}
399397

400398
func vfsShmMap(ctx context.Context, mod api.Module, pFile uint32, iRegion, szRegion int32, bExtend, pp uint32) _ErrorCode {
401-
file := vfsFileGet(ctx, mod, pFile).(FileSharedMemory).SharedMemory()
402-
p, err := file.shmMap(ctx, mod, iRegion, szRegion, bExtend != 0)
399+
shm := vfsFileGet(ctx, mod, pFile).(FileSharedMemory).SharedMemory()
400+
p, err := shm.shmMap(ctx, mod, iRegion, szRegion, bExtend != 0)
403401
if err != nil {
404402
return vfsErrorCode(err, _IOERR_SHMMAP)
405403
}
@@ -408,14 +406,14 @@ func vfsShmMap(ctx context.Context, mod api.Module, pFile uint32, iRegion, szReg
408406
}
409407

410408
func vfsShmLock(ctx context.Context, mod api.Module, pFile uint32, offset, n int32, flags _ShmFlag) _ErrorCode {
411-
file := vfsFileGet(ctx, mod, pFile).(FileSharedMemory).SharedMemory()
412-
err := file.shmLock(offset, n, flags)
409+
shm := vfsFileGet(ctx, mod, pFile).(FileSharedMemory).SharedMemory()
410+
err := shm.shmLock(offset, n, flags)
413411
return vfsErrorCode(err, _IOERR_SHMLOCK)
414412
}
415413

416414
func vfsShmUnmap(ctx context.Context, mod api.Module, pFile, bDelete uint32) _ErrorCode {
417-
file := vfsFileGet(ctx, mod, pFile).(FileSharedMemory).SharedMemory()
418-
file.shmUnmap(bDelete != 0)
415+
shm := vfsFileGet(ctx, mod, pFile).(FileSharedMemory).SharedMemory()
416+
shm.shmUnmap(bDelete != 0)
419417
return _OK
420418
}
421419

0 commit comments

Comments
 (0)