mvcc

package
v0.29.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2025 License: MIT Imports: 12 Imported by: 0

README

Go mvcc SQLite VFS

This package implements the EXPERIMENTAL "mvcc" in-memory SQLite VFS.

It has some benefits over the "memdb" VFS:

  • panics do not corrupt a shared database,
  • single-writer not blocked by readers,
  • readers never block,
  • instant snapshots.

mvcc.TestDB is the preferred way to setup an in-memory database for testing when you intend to leverage snapshots, e.g. to setup many independent copies of a database, such as one for each subtest.

Documentation

Overview

Package mvcc implements the "mvcc" SQLite VFS.

The "mvcc" vfs.VFS allows the same in-memory database to be shared among multiple database connections in the same process, as long as the database name begins with "/".

Importing package mvcc registers the VFS:

import _ "github.com/ncruces/go-sqlite3/vfs/mvcc"
Example
package main

import (
	"database/sql"
	_ "embed"
	"fmt"
	"log"

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

//go:embed testdata/test.db
var testDB string

func main() {
	mvcc.Create("test.db", mvcc.NewSnapshot(testDB))

	db, err := sql.Open("sqlite3", "file:/test.db?vfs=mvcc")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	_, err = db.Exec(`INSERT INTO users (id, name) VALUES (3, 'rust')`)
	if err != nil {
		log.Fatal(err)
	}

	rows, err := db.Query(`SELECT id, name FROM users`)
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	for rows.Next() {
		var id, name string
		err = rows.Scan(&id, &name)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s %s\n", id, name)
	}
}
Output:

0 go
1 zig
2 whatever
3 rust

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(name string, snapshot Snapshot)

Create creates a shared memory database, using a snapshot as its initial contents.

func Delete

func Delete(name string)

Delete deletes a shared memory database.

func TestDB added in v0.29.1

func TestDB(tb testing.TB, snapshot Snapshot, params ...url.Values) string

TestDB creates a shared database from a snapshot for the test to use. The database is automatically deleted when the test and all its subtests complete. Returns a URI filename appropriate to call Open with. Each subsequent call to TestDB returns a unique database.

func Test_something(t *testing.T) {
	t.Parallel()
	dsn := mvcc.TestDB(t, snapshot, url.Values{
		"_pragma": {"busy_timeout(1000)"},
	})

	db, err := sql.Open("sqlite3", dsn)
	if err != nil {
		t.Fatal(err)
	}
	defer db.Close()

	// ...
}

Types

type Snapshot

type Snapshot struct {
	*wbt.Tree[int64, string]
}

Snapshot represents a database snapshot.

func NewSnapshot added in v0.29.1

func NewSnapshot(data string) Snapshot

NewSnapshot creates a snapshot from data.

func TakeSnapshot added in v0.29.1

func TakeSnapshot(name string) Snapshot

TakeSnapshot takes a snapshot of a database. Name may be a URI filename.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL