Skip to content

Commit

Permalink
More.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Apr 3, 2024
1 parent b76d164 commit a5310ff
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 67 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ require (

retract v0.4.0 // tagged from the wrong branch

replace github.com/tetratelabs/wazero => github.com/ncruces/wazero v0.0.0-20240327075005-1e61961e4b45
replace github.com/tetratelabs/wazero => github.com/ncruces/wazero v0.0.0-20240401071028-c9d05f062413
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M=
github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
github.com/ncruces/wazero v0.0.0-20240327075005-1e61961e4b45 h1:S7fG4oq29CgXycris/pkmon/gbKFufnU8PTaRv5ewFs=
github.com/ncruces/wazero v0.0.0-20240327075005-1e61961e4b45/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
github.com/ncruces/wazero v0.0.0-20240401071028-c9d05f062413 h1:wUE8Bx0aoqNFkRu+GbC3/51kyq0hmE4txY8Z2EqEHhM=
github.com/ncruces/wazero v0.0.0-20240401071028-c9d05f062413/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
github.com/psanford/httpreadat v0.1.0 h1:VleW1HS2zO7/4c7c7zNl33fO6oYACSagjJIyMIwZLUE=
github.com/psanford/httpreadat v0.1.0/go.mod h1:Zg7P+TlBm3bYbyHTKv/EdtSJZn3qwbPwpfZ/I9GKCRE=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
Expand Down
4 changes: 2 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/ncruces/wazero v0.0.0-20240327075005-1e61961e4b45 h1:S7fG4oq29CgXycris/pkmon/gbKFufnU8PTaRv5ewFs=
github.com/ncruces/wazero v0.0.0-20240327075005-1e61961e4b45/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
github.com/ncruces/wazero v0.0.0-20240401071028-c9d05f062413 h1:wUE8Bx0aoqNFkRu+GbC3/51kyq0hmE4txY8Z2EqEHhM=
github.com/ncruces/wazero v0.0.0-20240401071028-c9d05f062413/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
Expand Down
2 changes: 1 addition & 1 deletion sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var instance struct {

func compileSQLite() {
if RuntimeConfig == nil {
RuntimeConfig = wazero.NewRuntimeConfig().WithMemoryLimitPages(1024).WithMemoryCapacityFromMax(true)
RuntimeConfig = wazero.NewRuntimeConfig()
}

ctx := context.Background()
Expand Down
6 changes: 6 additions & 0 deletions tests/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ncruces/go-sqlite3"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/vfs"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
)

Expand Down Expand Up @@ -36,6 +37,11 @@ func TestDB_nolock(t *testing.T) {
}

func TestDB_wal(t *testing.T) {
// TODO: reconsider this.
if !vfs.SupportsSharedMemory {
t.Skip("skipping without shared memory")
}

t.Parallel()
tmp := filepath.Join(t.TempDir(), "test.db")
err := os.WriteFile(tmp, waldb, 0666)
Expand Down
15 changes: 15 additions & 0 deletions tests/parallel/parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ import (

"github.com/ncruces/go-sqlite3"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/vfs"
"github.com/ncruces/go-sqlite3/vfs/memdb"
"github.com/tetratelabs/wazero"
)

func TestMain(m *testing.M) {
if vfs.SupportsSharedMemory {
sqlite3.RuntimeConfig = wazero.NewRuntimeConfig().
WithMemoryCapacityFromMax(true).
WithMemoryLimitPages(1024)
}
os.Exit(m.Run())
}

func TestParallel(t *testing.T) {
var iter int
if testing.Short() {
Expand All @@ -33,6 +44,10 @@ func TestParallel(t *testing.T) {
}

func TestWAL(t *testing.T) {
if !vfs.SupportsSharedMemory {
t.Skip("skipping without shared memory")
}

name := "file:" +
filepath.Join(t.TempDir(), "test.db") +
"?_pragma=busy_timeout(10000)" +
Expand Down
10 changes: 10 additions & 0 deletions vfs/lock.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
//go:build (linux || darwin || windows || freebsd || openbsd || netbsd || dragonfly || illumos) && !sqlite3_nosys

package vfs

import "github.com/ncruces/go-sqlite3/internal/util"

// SupportsFileLocking is false on platforms that do not support file locking.
// To open a database file on those platforms,
// you need to use the [nolock] or [immutable] URI parameters.
//
// [nolock]: https://sqlite.org/uri.html#urinolock
// [immutable]: https://sqlite.org/uri.html#uriimmutable
const SupportsFileLocking = true

const (
_PENDING_BYTE = 0x40000000
_RESERVED_BYTE = (_PENDING_BYTE + 1)
Expand Down
23 changes: 23 additions & 0 deletions vfs/lock_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build !(linux || darwin || windows || freebsd || openbsd || netbsd || dragonfly || illumos) || sqlite3_nosys

package vfs

// SupportsFileLocking is false on platforms that do not support file locking.
// To open a database file on those platforms,
// you need to use the [nolock] or [immutable] URI parameters.
//
// [nolock]: https://sqlite.org/uri.html#urinolock
// [immutable]: https://sqlite.org/uri.html#uriimmutable
const SupportsFileLocking = false

func (f *vfsFile) Lock(LockLevel) error {
return _IOERR_LOCK
}

func (f *vfsFile) Unlock(LockLevel) error {
return _IOERR_UNLOCK
}

func (f *vfsFile) CheckReservedLock() (bool, error) {
return false, _IOERR_CHECKRESERVEDLOCK
}
41 changes: 0 additions & 41 deletions vfs/os_nolock.go

This file was deleted.

8 changes: 0 additions & 8 deletions vfs/os_unix_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ import (
"golang.org/x/sys/unix"
)

// SupportsFileLocking is false on platforms that do not support file locking.
// To open a database file in one such platform,
// you need to use the [nolock] or [immutable] URI parameters.
//
// [nolock]: https://sqlite.org/uri.html#urinolock
// [immutable]: https://sqlite.org/uri.html#uriimmutable
const SupportsFileLocking = true

func osGetSharedLock(file *os.File) _ErrorCode {
// Test the PENDING lock before acquiring a new SHARED lock.
if pending, _ := osCheckLock(file, _PENDING_BYTE, 1); pending {
Expand Down
8 changes: 0 additions & 8 deletions vfs/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ import (
"golang.org/x/sys/windows"
)

// SupportsFileLocking is false on platforms that do not support file locking.
// To open a database file in one such platform,
// you need to use the [nolock] or [immutable] URI parameters.
//
// [nolock]: https://sqlite.org/uri.html#urinolock
// [immutable]: https://sqlite.org/uri.html#uriimmutable
const SupportsFileLocking = true

func osGetSharedLock(file *os.File) _ErrorCode {
// Acquire the PENDING lock temporarily before acquiring a new SHARED lock.
rc := osReadLock(file, _PENDING_BYTE, 1, 0)
Expand Down
12 changes: 11 additions & 1 deletion vfs/shm.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build (linux || darwin) && (amd64 || arm64) && !sqlite3_nosys
//go:build (linux || darwin) && (amd64 || arm64) && !sqlite3_flock && !sqlite3_nosys

package vfs

Expand All @@ -13,6 +13,16 @@ import (
"golang.org/x/sys/unix"
)

// SupportsSharedMemory is true on platforms that support shared memory.
// To enable shared memory support on those platforms,
// you need to set the appropriate [wazero.RuntimeConfig];
// otherwise, [EXCLUSIVE locking mode] is activated automatically
// to use [WAL without shared-memory].
//
// [WAL without shared-memory]: https://sqlite.org/wal.html#noshm
// [EXCLUSIVE locking mode]: https://sqlite.org/pragma.html#pragma_locking_mode
const SupportsSharedMemory = true

type vfsShm struct {
*os.File
regions []shmRegion
Expand Down
12 changes: 11 additions & 1 deletion vfs/shm_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !(linux || darwin) || !(amd64 || arm64) || sqlite3_nosys
//go:build !(linux || darwin) || !(amd64 || arm64) || sqlite3_flock || sqlite3_nosys

package vfs

Expand All @@ -8,6 +8,16 @@ import (
"github.com/tetratelabs/wazero/api"
)

// SupportsSharedMemory is true on platforms that support shared memory.
// To enable shared memory support on those platforms,
// you need to set the appropriate [wazero.RuntimeConfig];
// otherwise, [EXCLUSIVE locking mode] is activated automatically
// to use [WAL without shared-memory].
//
// [WAL without shared-memory]: https://sqlite.org/wal.html#noshm
// [EXCLUSIVE locking mode]: https://sqlite.org/pragma.html#pragma_locking_mode
const SupportsSharedMemory = false

type vfsShm struct{}

func (f *vfsFile) ShmMap(ctx context.Context, mod api.Module, id, size uint32, extend bool) (uint32, error) {
Expand Down
22 changes: 20 additions & 2 deletions vfs/tests/mptest/mptest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ var (

func TestMain(m *testing.M) {
ctx := context.Background()
rt = wazero.NewRuntimeWithConfig(ctx,
wazero.NewRuntimeConfig().WithMemoryLimitPages(1024).WithMemoryCapacityFromMax(true))
cfg := wazero.NewRuntimeConfig()
if vfs.SupportsSharedMemory {
cfg = cfg.WithMemoryLimitPages(1024).WithMemoryCapacityFromMax(true)
}
rt = wazero.NewRuntimeWithConfig(ctx, cfg)
wasi_snapshot_preview1.MustInstantiate(ctx, rt)

env := vfs.ExportHostFunctions(rt.NewHostModuleBuilder("env"))
Expand Down Expand Up @@ -189,6 +192,11 @@ func Test_multiwrite01_memory(t *testing.T) {
}

func Test_config01_wal(t *testing.T) {
// TODO: reconsider this.
if !vfs.SupportsSharedMemory {
t.Skip("skipping without shared memory")
}

ctx := util.NewContext(newContext(t))
name := filepath.Join(t.TempDir(), "test.db")
cfg := config(ctx).WithArgs("mptest", name, "config01.test",
Expand All @@ -201,6 +209,11 @@ func Test_config01_wal(t *testing.T) {
}

func Test_crash01_wal(t *testing.T) {
// TODO: reconsider this.
if !vfs.SupportsSharedMemory {
t.Skip("skipping without shared memory")
}

if testing.Short() {
t.Skip("skipping in short mode")
}
Expand All @@ -220,6 +233,11 @@ func Test_crash01_wal(t *testing.T) {
}

func Test_multiwrite01_wal(t *testing.T) {
// TODO: reconsider this.
if !vfs.SupportsSharedMemory {
t.Skip("skipping without shared memory")
}

if testing.Short() {
t.Skip("skipping in short mode")
}
Expand Down

0 comments on commit a5310ff

Please sign in to comment.