Skip to content

Commit

Permalink
VFS utilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Oct 18, 2024
1 parent f18561e commit fefee69
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 116 deletions.
2 changes: 1 addition & 1 deletion util/fsutil/mode.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package fsutil implements filesystem utility functions.
// Package fsutil implements filesystem utilities.
package fsutil

import (
Expand Down
2 changes: 1 addition & 1 deletion util/ioutil/ioutil.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Package ioutil implements I/O utility functions.
// Package ioutil implements I/O utilities.
package ioutil
2 changes: 1 addition & 1 deletion util/osutil/osutil.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Package osutil implements operating system utility functions.
// Package osutil implements operating system utilities.
package osutil
132 changes: 132 additions & 0 deletions util/vfsutil/wrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Package vtabutil implements virtual filesystem utilities.
package vfsutil

import (
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/vfs"
)

// WrapSharedMemory helps wrap [vfs.FileSharedMemory].
func WrapSharedMemory(f vfs.File) vfs.SharedMemory {
if f, ok := f.(vfs.FileSharedMemory); ok {
return f.SharedMemory()
}
return nil
}

// WrapChunkSize helps wrap [vfs.FileChunkSize].
func WrapChunkSize(f vfs.File, size int) {
if f, ok := f.(vfs.FileChunkSize); ok {
f.ChunkSize(size)
}
}

// WrapSizeHint helps wrap [vfs.FileSizeHint].
func WrapSizeHint(f vfs.File, size int64) error {
if f, ok := f.(vfs.FileSizeHint); ok {
return f.SizeHint(size)
}
return sqlite3.NOTFOUND
}

// WrapHasMoved helps wrap [vfs.FileHasMoved].
func WrapHasMoved(f vfs.File) (bool, error) {
if f, ok := f.(vfs.FileHasMoved); ok {
return f.HasMoved()
}
return false, sqlite3.NOTFOUND
}

// WrapOverwrite helps wrap [vfs.FileOverwrite].
func WrapOverwrite(f vfs.File) error {
if f, ok := f.(vfs.FileOverwrite); ok {
return f.Overwrite()
}
return sqlite3.NOTFOUND
}

// WrapPersistentWAL helps wrap [vfs.FilePersistentWAL].
func WrapPersistentWAL(f vfs.File) bool {
if f, ok := f.(vfs.FilePersistentWAL); ok {
return f.PersistentWAL()
}
return false
}

// WrapSetPersistentWAL helps wrap [vfs.FilePersistentWAL].
func WrapSetPersistentWAL(f vfs.File, keepWAL bool) {
if f, ok := f.(vfs.FilePersistentWAL); ok {
f.SetPersistentWAL(keepWAL)
}
}

// WrapPowersafeOverwrite helps wrap [vfs.FilePowersafeOverwrite].
func WrapPowersafeOverwrite(f vfs.File) bool {
if f, ok := f.(vfs.FilePowersafeOverwrite); ok {
return f.PowersafeOverwrite()
}
return false
}

// WrapSetPowersafeOverwrite helps wrap [vfs.FilePowersafeOverwrite].
func WrapSetPowersafeOverwrite(f vfs.File, psow bool) {
if f, ok := f.(vfs.FilePowersafeOverwrite); ok {
f.SetPowersafeOverwrite(psow)
}
}

// WrapCommitPhaseTwo helps wrap [vfs.FileCommitPhaseTwo].
func WrapCommitPhaseTwo(f vfs.File) error {
if f, ok := f.(vfs.FileCommitPhaseTwo); ok {
return f.CommitPhaseTwo()
}
return sqlite3.NOTFOUND
}

// WrapBeginAtomicWrite helps wrap [vfs.FileBatchAtomicWrite].
func WrapBeginAtomicWrite(f vfs.File) error {
if f, ok := f.(vfs.FileBatchAtomicWrite); ok {
return f.BeginAtomicWrite()
}
return sqlite3.NOTFOUND
}

// WrapCommitAtomicWrite helps wrap [vfs.FileBatchAtomicWrite].
func WrapCommitAtomicWrite(f vfs.File) error {
if f, ok := f.(vfs.FileBatchAtomicWrite); ok {
return f.CommitAtomicWrite()
}
return sqlite3.NOTFOUND
}

// WrapRollbackAtomicWrite helps wrap [vfs.FileBatchAtomicWrite].
func WrapRollbackAtomicWrite(f vfs.File) error {
if f, ok := f.(vfs.FileBatchAtomicWrite); ok {
return f.RollbackAtomicWrite()
}
return sqlite3.NOTFOUND
}

// WrapCheckpointDone helps wrap [vfs.FileCheckpoint].
func WrapCheckpointDone(f vfs.File) error {
if f, ok := f.(vfs.FileCheckpoint); ok {
return f.CheckpointDone()
}
return sqlite3.NOTFOUND
}

// WrapCheckpointStart helps wrap [vfs.FileCheckpoint].
func WrapCheckpointStart(f vfs.File) error {
if f, ok := f.(vfs.FileCheckpoint); ok {
return f.CheckpointStart()
}
return sqlite3.NOTFOUND
}

// WrapPragma helps wrap [vfs.FilePragma].
func WrapPragma(f vfs.File, name, value string) (string, error) {
if f, ok := f.(vfs.FilePragma); ok {
return f.Pragma(name, value)
}
return "", sqlite3.NOTFOUND
}
2 changes: 1 addition & 1 deletion util/vtabutil/vtabutil.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Package vtabutil implements virtual table utility functions.
// Package vtabutil implements virtual table utilities.
package vtabutil
73 changes: 17 additions & 56 deletions vfs/adiantum/hbsh.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/internal/util"
"github.com/ncruces/go-sqlite3/util/vfsutil"
"github.com/ncruces/go-sqlite3/vfs"
)

Expand Down Expand Up @@ -90,10 +91,7 @@ func (h *hbshFile) Pragma(name string, value string) (string, error) {
key = h.init.KDF(value)
}
default:
if f, ok := h.File.(vfs.FilePragma); ok {
return f.Pragma(name, value)
}
return "", sqlite3.NOTFOUND
return vfsutil.WrapPragma(h.File, name, value)
}

if h.hbsh = h.init.HBSH(key); h.hbsh != nil {
Expand Down Expand Up @@ -209,92 +207,55 @@ func (h *hbshFile) DeviceCharacteristics() vfs.DeviceCharacteristic {
// Wrap optional methods.

func (h *hbshFile) SharedMemory() vfs.SharedMemory {
if f, ok := h.File.(vfs.FileSharedMemory); ok {
return f.SharedMemory()
}
return nil
return vfsutil.WrapSharedMemory(h.File)
}

func (h *hbshFile) ChunkSize(size int) {
if f, ok := h.File.(vfs.FileChunkSize); ok {
size = (size + (blockSize - 1)) &^ (blockSize - 1) // round up
f.ChunkSize(size)
}
size = (size + (blockSize - 1)) &^ (blockSize - 1) // round up
vfsutil.WrapChunkSize(h.File, size)
}

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

func (h *hbshFile) HasMoved() (bool, error) {
if f, ok := h.File.(vfs.FileHasMoved); ok {
return f.HasMoved()
}
return false, sqlite3.NOTFOUND
return vfsutil.WrapHasMoved(h.File) // notest
}

func (h *hbshFile) Overwrite() error {
if f, ok := h.File.(vfs.FileOverwrite); ok {
return f.Overwrite()
}
return sqlite3.NOTFOUND
return vfsutil.WrapOverwrite(h.File) // notest
}

func (h *hbshFile) PersistentWAL() bool {
if f, ok := h.File.(vfs.FilePersistentWAL); ok {
return f.PersistentWAL()
}
return false
return vfsutil.WrapPersistentWAL(h.File) // notest
}

func (h *hbshFile) SetPersistentWAL(keepWAL bool) {
if f, ok := h.File.(vfs.FilePersistentWAL); ok {
f.SetPersistentWAL(keepWAL)
}
vfsutil.WrapSetPersistentWAL(h.File, keepWAL) // notest
}

func (h *hbshFile) CommitPhaseTwo() error {
if f, ok := h.File.(vfs.FileCommitPhaseTwo); ok {
return f.CommitPhaseTwo()
}
return sqlite3.NOTFOUND
return vfsutil.WrapCommitPhaseTwo(h.File) // notest
}

func (h *hbshFile) BeginAtomicWrite() error {
if f, ok := h.File.(vfs.FileBatchAtomicWrite); ok {
return f.BeginAtomicWrite()
}
return sqlite3.NOTFOUND
return vfsutil.WrapBeginAtomicWrite(h.File) // notest
}

func (h *hbshFile) CommitAtomicWrite() error {
if f, ok := h.File.(vfs.FileBatchAtomicWrite); ok {
return f.CommitAtomicWrite()
}
return sqlite3.NOTFOUND
return vfsutil.WrapCommitAtomicWrite(h.File) // notest
}

func (h *hbshFile) RollbackAtomicWrite() error {
if f, ok := h.File.(vfs.FileBatchAtomicWrite); ok {
return f.RollbackAtomicWrite()
}
return sqlite3.NOTFOUND
return vfsutil.WrapRollbackAtomicWrite(h.File) // notest
}

func (h *hbshFile) CheckpointDone() error {
if f, ok := h.File.(vfs.FileCheckpoint); ok {
return f.CheckpointDone()
}
return sqlite3.NOTFOUND
return vfsutil.WrapCheckpointDone(h.File) // notest
}

func (h *hbshFile) CheckpointStart() error {
if f, ok := h.File.(vfs.FileCheckpoint); ok {
return f.CheckpointStart()
}
return sqlite3.NOTFOUND
return vfsutil.WrapCheckpointStart(h.File) // notest
}
Loading

0 comments on commit fefee69

Please sign in to comment.