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 de33f8a
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 60 deletions.
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
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
10 changes: 10 additions & 0 deletions vfs/shm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions vfs/shm_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 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

0 comments on commit de33f8a

Please sign in to comment.