2023-06-01 18:11:37 +01:00
|
|
|
// Package readervfs implements an SQLite VFS for immutable databases.
|
2023-05-31 18:45:45 +01:00
|
|
|
//
|
2023-06-01 18:11:37 +01:00
|
|
|
// The "reader" [vfs.VFS] permits accessing any [io.ReaderAt]
|
2023-05-31 18:45:45 +01:00
|
|
|
// as an immutable SQLite database.
|
|
|
|
|
//
|
2024-04-27 16:31:32 +01:00
|
|
|
// Importing package readervfs registers the VFS:
|
2023-05-31 18:45:45 +01:00
|
|
|
//
|
2023-06-01 18:11:37 +01:00
|
|
|
// import _ "github.com/ncruces/go-sqlite3/vfs/readervfs"
|
|
|
|
|
package readervfs
|
2023-05-31 18:45:45 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"sync"
|
|
|
|
|
|
2024-01-03 00:54:30 +00:00
|
|
|
"github.com/ncruces/go-sqlite3/util/ioutil"
|
2023-06-01 18:11:37 +01:00
|
|
|
"github.com/ncruces/go-sqlite3/vfs"
|
2023-05-31 18:45:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
2023-06-01 18:11:37 +01:00
|
|
|
vfs.Register("reader", readerVFS{})
|
2023-05-31 18:45:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
readerMtx sync.RWMutex
|
2023-06-02 03:32:13 +01:00
|
|
|
// +checklocks:readerMtx
|
2024-01-03 00:54:30 +00:00
|
|
|
readerDBs = map[string]ioutil.SizeReaderAt{}
|
2023-05-31 18:45:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Create creates an immutable database from reader.
|
2023-06-12 13:04:37 +01:00
|
|
|
// The caller should ensure that data from reader does not mutate,
|
2023-05-31 18:45:45 +01:00
|
|
|
// otherwise SQLite might return incorrect query results and/or [sqlite3.CORRUPT] errors.
|
2024-01-03 00:54:30 +00:00
|
|
|
func Create(name string, reader ioutil.SizeReaderAt) {
|
2023-05-31 18:45:45 +01:00
|
|
|
readerMtx.Lock()
|
|
|
|
|
readerDBs[name] = reader
|
2025-12-19 16:37:47 +00:00
|
|
|
readerMtx.Unlock()
|
2023-05-31 18:45:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete deletes a shared memory database.
|
|
|
|
|
func Delete(name string) {
|
|
|
|
|
readerMtx.Lock()
|
|
|
|
|
delete(readerDBs, name)
|
2025-12-19 16:37:47 +00:00
|
|
|
readerMtx.Unlock()
|
2023-05-31 18:45:45 +01:00
|
|
|
}
|