Documentation
¶
Overview ¶
Package vfs gives SQLite a VFS written in Go: a read-only one over a Go fs.FS, made by New, or one a database can be written through, made by Register from a VFS.
New registers the file system with SQLite and returns the name it was registered under. Passing that name as the vfs DSN query parameter opens a database read from the wrapped file system instead of from the host one. Paths are resolved by the wrapped fs.FS, so the database is named relative to its root:
name, fsvfs, err := vfs.New(os.DirFS(dir))
if err != nil {
return err
}
defer fsvfs.Close()
db, err := sql.Open("sqlite", "file:test.db?vfs="+name)
Any fs.FS will do, an embed.FS included, which is what makes this useful for shipping a database inside the binary. The VFS is read only: it has no write path and refuses to create a journal, so a database opened through it can only be read from. A hot journal next to the database, one SQLite would have to roll back first, makes reading it fail with SQLITE_READONLY_ROLLBACK, as a database opened with mode=ro does.
Register takes a VFS, whose files SQLite reads, writes, truncates, syncs and deletes: the database, its journal and the rest. It is the way to keep a database somewhere a file system does not reach. See Register for what the package does on top of the VFS, and for what it does not do yet.
Registration is process-global, as SQLite's own VFS registry is. Each New and each Register registers a separate VFS under a fresh name, and FS.Close unregisters it again. Close every database opened through an FS before closing it: while any is open, FS.Close refuses with an error wrapping ErrInUse.
Index ¶
- Constants
- Variables
- func Xsqlite3_fsFS(tls *libc.TLS, zName uintptr, pAppData uintptr) uintptr
- type AccessFlag
- type FS
- type File
- type Fts5ExtensionApi
- type Fts5ExtensionApi1
- type Fts5PhraseIter
- type Fts5PhraseIter1
- type LockLevel
- type LockOpener
- type Locker
- type OpenFlag
- type SyncFlag
- type VFS
- type VFSFile
- type VFSFile1
Constants ¶
const ( LockNone = LockLevel(lockNone) LockReserved = LockLevel(lockReserved) LockPending = LockLevel(lockPending) LockExclusive = LockLevel(lockExclusive) )
The lock levels. A database is read under LockShared and written under LockExclusive; LockReserved announces a writer while others still read, and LockPending keeps new readers out while the writer waits for the ones that are in.
const ( SIGEV_SIGNAL = 0 SIGEV_NONE = 1 SIGEV_THREAD = 2 SIGEV_THREAD_ID = 4 )
const ( SEGV_MAPERR = 1 SEGV_ACCERR = 2 SEGV_BNDERR = 3 SEGV_PKUERR = 4 SEGV_ACCADI = 5 SEGV_ADIDERR = 6 SEGV_ADIPERR = 7 )
const ( BUS_ADRALN = 1 BUS_ADRERR = 2 BUS_OBJERR = 3 BUS_MCEERR_AR = 4 BUS_MCEERR_AO = 5 )
const ( CLD_EXITED = 1 CLD_KILLED = 2 CLD_DUMPED = 3 CLD_TRAPPED = 4 CLD_STOPPED = 5 CLD_CONTINUED = 6 )
const ( POLL_IN = 1 POLL_OUT = 2 POLL_MSG = 3 POLL_ERR = 4 POLL_PRI = 5 POLL_HUP = 6 )
const ( SI_ASYNCNL = -60 SI_DETHREAD = -7 SI_TKILL = -6 SI_SIGIO = -5 SI_ASYNCIO = -4 SI_MESGQ = -3 SI_TIMER = -2 SI_QUEUE = -1 SI_USER = 0 SI_KERNEL = 128 )
const ( ILL_ILLOPC = 1 ILL_ILLOPN = 2 ILL_ILLADR = 3 ILL_ILLTRP = 4 ILL_PRVOPC = 5 ILL_PRVREG = 6 ILL_COPROC = 7 ILL_BADSTK = 8 ILL_BADIADDR = 9 )
const ( FPE_INTDIV = 1 FPE_INTOVF = 2 FPE_FLTDIV = 3 FPE_FLTOVF = 4 FPE_FLTUND = 5 FPE_FLTRES = 6 FPE_FLTINV = 7 FPE_FLTSUB = 8 FPE_FLTUNK = 14 FPE_CONDTRAP = 15 )
const ( SS_ONSTACK = 1 SS_DISABLE = 2 )
Variables ¶
var ErrBusy = errors.New("vfs: database is locked")
ErrBusy is what Locker.Lock returns, alone or wrapped, when another Locker holds a lock that is in the way. SQLite then reports SQLITE_BUSY, and its busy handler decides whether to ask again.
var ErrFull = errors.New("vfs: storage is full")
ErrFull is what [File.WriteAt] returns, alone or wrapped, when the storage has no room left. SQLite then reports SQLITE_FULL instead of an I/O error.
var ErrInUse = errors.New("vfs: file system is in use")
ErrInUse is returned, wrapped, by FS.Close while a database opened through the file system is still open.
Functions ¶
Types ¶
type AccessFlag ¶
type AccessFlag int32
AccessFlag is the question SQLite asks through VFS.Access. The values are SQLite's.
const ( AccessExists AccessFlag = sqlite3.SQLITE_ACCESS_EXISTS AccessReadWrite AccessFlag = sqlite3.SQLITE_ACCESS_READWRITE AccessRead AccessFlag = sqlite3.SQLITE_ACCESS_READ )
Values of AccessFlag.
type FS ¶
type FS struct {
// contains filtered or unexported fields
}
FS is a VFS registered with SQLite: a read-only one backed by a Go fs.FS, made by New, or one made by Register from a VFS.
func New ¶
New creates a new sqlite VFS and registers it. If successful, the file system can be used with the URI parameter `?vfs=<returned name>`.
func Register ¶
Register registers v with SQLite as a VFS a database can be read and written through, and returns the name it was registered under, to be given as the vfs DSN query parameter, and the handle to unregister it with:
name, fsvfs, err := vfs.Register(v)
if err != nil {
return err
}
defer fsvfs.Close()
db, err := sql.Open("sqlite", "file:test.db?vfs="+name)
The connections of this process that open one database through the returned VFS are kept from one another by the package, which runs SQLite's file locking between them: a File has no lock methods, and a database/sql pool of any size works on a VFS that is safe for concurrent use. A second process, or a second registration of the same storage, is kept away from the database only by a VFS that implements LockOpener: without it, two of them that write lose each other's transactions and can damage the database. Two names for one database are told apart only by VFS.FullPathname.
The VFS has no shared memory, so the write-ahead log works only for a connection that holds the database exclusively. With "PRAGMA locking_mode=EXCLUSIVE" run before the database is first read, "PRAGMA journal_mode=WAL" works; without it the pragma leaves the journal mode as it was, and a database that already is in WAL mode cannot be opened at all. A WAL database therefore needs the pragma in the DSN and a pool of one connection.
SQLite's temporary files are kept in memory and take that much of it for as long as they are open. VACUUM builds a copy of the whole database in one; a sort or a CREATE INDEX too large for SQLite's cache, a large temporary table and a large statement journal are others. Nothing bounds them: where SQLite's own VFS would fill a disk, SQL run through this one can make the process run out of memory.
Registration is process-global, and FS.Close has the same contract as for a file system made by New.
func (*FS) Close ¶
Close unregisters f and releases its resources.
Every database opened through f must be closed first. SQLite keeps a pointer to the VFS in each connection and calls through it for as long as the connection is open, so while any file is open through f, Close does nothing and returns an error wrapping ErrInUse; close the databases and call it again. Close must also not run concurrently with opening a database through f: SQLite looks the VFS up before it opens the file, and nothing can tell Close about an open that is between the two.
type File ¶
type File interface {
// ReadAt is [io.ReaderAt]. A read that ends past the end of the file
// returns what there is and io.EOF.
io.ReaderAt
// WriteAt is [io.WriterAt]. A write past the end of the file extends it,
// and the bytes between the old end and the write read as zeros.
io.WriterAt
// Close releases the file. SQLite does not look at the error.
io.Closer
// Truncate sets the size of the file.
Truncate(size int64) error
// Sync returns when everything written to the file is durable.
Sync(flags SyncFlag) error
// Size returns the size of the file in bytes.
Size() (int64, error)
}
File is an open file of a VFS.
SQLite owns the buffers it passes to ReadAt and WriteAt: a File must not keep them, and must not change the one given to WriteAt.
SQLite is told a sector of 4096 bytes and no property of the storage: a write cut short by a loss of power may damage the 4096-byte sectors it touches and nothing further, and what Sync returned from is safe.
type Fts5ExtensionApi ¶
type Fts5ExtensionApi = Fts5ExtensionApi1
type Fts5ExtensionApi1 ¶
type Fts5ExtensionApi1 = struct {
// contains filtered or unexported fields
}
type Fts5PhraseIter ¶
type Fts5PhraseIter = Fts5PhraseIter1
type Fts5PhraseIter1 ¶
type Fts5PhraseIter1 = struct {
// contains filtered or unexported fields
}
type LockLevel ¶
type LockLevel int32
LockLevel is a level of SQLite's file locking, described in https://www.sqlite.org/lockingv3.html. The values are SQLite's.
type LockOpener ¶
type LockOpener interface {
// OpenLock returns the lock of the database called name, a name
// [VFS.FullPathname] returned. It is called after the Open of the first
// file the registration has on the database, whatever the mode of that
// Open, and the Locker serves every connection that opens the database
// from then on; it is closed after the last file on the database. A nil
// Locker with a nil error means that nothing outside the registration
// can open this database. A Locker returned together with an error is
// closed.
//
// OpenLock and the Close of a Locker are never called at the same time,
// for any two databases of the registration: a database is not given a
// second Locker before its first one is closed.
OpenLock(name string) (Locker, error)
}
LockOpener is implemented by a VFS whose databases can be opened from outside the registration: by another process, or through a second registration of the same storage. Without it the package keeps apart only the connections that go through one registration, see Register.
type Locker ¶
type Locker interface {
// Lock raises the lock to level: to LockShared from LockNone, to
// LockReserved from LockShared, to LockPending from LockReserved, and to
// LockExclusive from LockPending or, to roll back the journal a dead
// writer left, straight from LockShared. It returns ErrBusy when another
// Locker is in the way. After any other error the package does not know
// what the Locker holds, see Unlock.
Lock(level LockLevel) error
// Unlock lowers the lock to LockShared or to LockNone.
//
// After an error from Unlock, or one other than ErrBusy from Lock, the
// package does not know what the Locker holds. While connections of the
// registration still hold the database, it fails every request for a
// higher lock and every read, write and sync of the database with an
// I/O error, and no read or write of the database is under way while
// Lock or Unlock runs. Once the connections have let go, it calls
// Unlock(LockNone), again if need be before the next Lock, and goes on
// when that succeeds.
Unlock(level LockLevel) error
// CheckReserved reports whether another Locker has taken LockReserved
// and not given it up: whether a writer is at work whose journal must be
// left alone. A Locker that went straight from LockShared to
// LockExclusive need not be reported. CheckReserved may be called while
// this Locker holds nothing, and it changes nothing of what the Locker
// holds, also when it fails.
CheckReserved() (bool, error)
// Close releases the Locker and what it still holds. SQLite does not
// look at the error.
io.Closer
}
Locker is the lock one registration holds on one database against every other Locker of that database, in this process or in another.
The package runs SQLite's locking between the connections of the registration itself and calls the Locker only when the lock of all of them together has to change: Lock(LockShared) for the first reader, Unlock(LockNone) after the last, and the steps of a writer in between. The calls for one database never overlap.
What a Locker must keep to, where held means held by another Locker:
- LockShared is refused while LockPending or LockExclusive is held. The decision is atomic: a reader that was let in is in before a writer can take LockExclusive.
- LockReserved is refused while LockReserved or more is held.
- LockPending is asked for only by the holder of LockReserved. It may be refused for a moment, as it is by the way SQLite's own VFS lets a reader in.
- LockExclusive is refused while any lock is held.
- A refusal changes nothing: the lock is what it was before the call.
- Unlock(LockShared) keeps the database locked for reading all the time: it must not release the lock and take it again.
- A Locker whose process died holds nothing.
- Two Lockers of one database are two lockers, also in one process.
The last rule is what POSIX record locks do not give. They belong to the process: two descriptors of one process never refuse each other, and closing any descriptor of the file drops every lock the process has on it. A Locker built on them needs one descriptor and one state per database for the whole process, shared by its registrations and by its Files, and not closed while a lock is held. Open file description locks on Linux and LockFileEx on Windows are held by the handle and need none of that. To share a database with programs that use SQLite's own VFS on unix, take the locks SQLite takes: the byte at offset 0x40000000 for LockPending, the next byte for LockReserved, and the 510 bytes after it for LockShared (a read lock) and LockExclusive (a write lock), entering LockShared under a read lock on the pending byte.
The methods are called on the goroutine that runs the statement. They must not wait for a lock and must not run SQL on a connection that goes through the same VFS, and a panic in one is not recovered.
type OpenFlag ¶
type OpenFlag int32
OpenFlag is the set of SQLITE_OPEN_ flags SQLite passes to VFS.Open. The values are SQLite's.
const ( OpenReadOnly OpenFlag = sqlite3.SQLITE_OPEN_READONLY OpenReadWrite OpenFlag = sqlite3.SQLITE_OPEN_READWRITE OpenCreate OpenFlag = sqlite3.SQLITE_OPEN_CREATE OpenDeleteOnClose OpenFlag = sqlite3.SQLITE_OPEN_DELETEONCLOSE OpenExclusive OpenFlag = sqlite3.SQLITE_OPEN_EXCLUSIVE OpenMainDB OpenFlag = sqlite3.SQLITE_OPEN_MAIN_DB OpenMainJournal OpenFlag = sqlite3.SQLITE_OPEN_MAIN_JOURNAL OpenSuperJournal OpenFlag = sqlite3.SQLITE_OPEN_SUPER_JOURNAL OpenWAL OpenFlag = sqlite3.SQLITE_OPEN_WAL )
Flags of VFS.Open. One of OpenMainDB, OpenMainJournal, OpenSuperJournal and OpenWAL says what kind of file SQLite is opening.
type SyncFlag ¶
type SyncFlag int32
SyncFlag says how much File.Sync has to make durable. The values are SQLite's.
const ( SyncNormal SyncFlag = sqlite3.SQLITE_SYNC_NORMAL SyncFull SyncFlag = sqlite3.SQLITE_SYNC_FULL SyncDataOnly SyncFlag = sqlite3.SQLITE_SYNC_DATAONLY )
Values of SyncFlag. SyncDataOnly is or-ed into one of the other two when only the content of the file, and not its metadata, has to reach the medium.
type VFS ¶
type VFS interface {
// Open opens the named file. Flags holds OpenReadWrite or OpenReadOnly;
// with OpenCreate the file is to be created if it does not exist, and
// with OpenCreate and OpenExclusive together the open must fail if it
// does. The returned flags say how the file was opened in fact: return
// OpenReadOnly for a file that cannot be written although OpenReadWrite
// was asked for, and flags otherwise. No other bit of the result is used.
// A File returned together with an error is closed.
Open(name string, flags OpenFlag) (File, OpenFlag, error)
// Delete removes the named file. With syncDir, the removal itself must be
// durable when Delete returns. Deleting a file that does not exist is an
// error for which errors.Is(err, fs.ErrNotExist) holds.
Delete(name string, syncDir bool) error
// Access reports whether the named file exists (AccessExists), exists
// and can be read (AccessRead), or exists and can be read and written
// (AccessReadWrite). A file that does not exist is not an error.
Access(name string, flags AccessFlag) (bool, error)
// FullPathname returns the canonical name of the database called name in
// the DSN. Two names of one database must give the same result: it is
// the name the other files are derived from, and the name under which the
// connections of this process are kept from one another.
FullPathname(name string) (string, error)
}
VFS is a file system SQLite can read and write through, registered with Register.
SQLite opens the database, its rollback journal, its super-journals and its write-ahead log through Open, each under the name FullPathname returned for the database with a suffix added: "-journal", "-wal", or "-mj" and nine more characters. Temporary files are not opened through the VFS: the package keeps them in memory, see Register.
The names come from SQLite and not all of them from the DSN: the name of a super-journal is read from the journal found next to a database, so whoever can put a journal there chooses a name that Open, Access and Delete are then called with. A VFS that maps names to something wider than its own storage has to confine them.
The methods are called on the goroutine that runs the statement, so the connections of a pool call them, and the methods of the Files of one database, at the same time. The package serializes nothing but the locking of SQLite: a VFS and whatever its Files share must be safe for concurrent use. A method must not run SQL on a connection that goes through the same VFS, and a panic in a method is not recovered: it leaves the connection, and with it the database, in an undefined state.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package xts keeps a database encrypted on its storage: a VFS that goes on top of another one, SQLite's own by default, and encrypts what SQLite writes through it with AES-256 in XTS mode.
|
Package xts keeps a database encrypted on its storage: a VFS that goes on top of another one, SQLite's own by default, and encrypts what SQLite writes through it with AES-256 in XTS mode. |