Documentation
¶
Index ¶
- Variables
- type BufferConfig
- type DB
- func (db *DB) Close() error
- func (db *DB) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (db *DB) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (db *DB) QueryRow(query string, args ...interface{}) *sql.Row
- func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- type Savepoint
- type Task
- type TaskArgs
- type TaskFunc
- type TaskResult
- type Tx
- type Writer
- func (w *Writer) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (w *Writer) Close() error
- func (w *Writer) Commit() error
- func (w *Writer) Exec(query string, args ...any) (sql.Result, error)
- func (w *Writer) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("sqlite: writer is closed")
var ErrTxNotready = errors.New("sqlite: transaction not ready")
Functions ¶
This section is empty.
Types ¶
type BufferConfig ¶
type BufferConfig struct {
// Size is the number of statements to buffer before triggering an
// automatic flush. A transaction is committed when this threshold
// is reached. Default: 100.
Size int
// FlushInterval is the maximum duration between commits. The writer
// flushes pending statements if this interval elapses since the last
// commit, even if Size has not been reached. Default: 100ms.
FlushInterval time.Duration
}
BufferConfig holds batch writer configuration. The writer buffers incoming statements and flushes them in batches to reduce transaction overhead and improve throughput.
func (*BufferConfig) Validate ¶
func (c *BufferConfig) Validate()
Validate sets default values for any zero fields and ensures the configuration is usable. It is called automatically by NewWriter; you only need to invoke it if you wish to check a config before use.
type DB ¶
DB wraps a SQLite database with logical read/write separation. All writes go through the buffered Writer; all reads go directly to the Reader. This separation allows concurrent reads while writes are being buffered and committed by the background flush goroutine.
func New ¶
New opens a SQLite database from the given DSN. If DSN is empty, it defaults to ":memory:" (an in-memory database). For file paths, the file is created if it does not exist. This is an alias for Open; prefer Open for clarity in application code.
func Open ¶
Open opens a SQLite database from the given DSN. If DSN is empty, it defaults to ":memory:" (an in-memory database). For file paths, the file is created if it does not exist. This is the preferred constructor.
func (*DB) Close ¶
Close shuts down the writer flush goroutine (committing any pending writes) and closes both the Writer and Reader database connections. Always call Close when you are done with the DB to avoid leaking goroutines or connections.
func (*DB) Exec ¶
Exec delegates to Writer.Exec, executing a write statement (INSERT, UPDATE, DELETE, CREATE TABLE, etc.) asynchronously with buffering. See Writer.Exec for details on buffering behavior.
func (*DB) ExecContext ¶
func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
ExecContext delegates to Writer.ExecContext, executing a write statement with context cancellation support. See Writer.ExecContext for details.
func (*DB) Query ¶
Query delegates to Reader.Query, executing a read-only query on the dedicated reader connection pool. Returns *sql.Rows that must be closed. Reads are served concurrently with writes and do not block the Writer.
func (*DB) QueryContext ¶
func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryContext delegates to Reader.QueryContext, executing a read-only query with context cancellation support. Returns *sql.Rows that must be closed.
type Savepoint ¶
type Savepoint struct {
// contains filtered or unexported fields
}
Savepoint represents a named SQLite savepoint within a transaction. Savepoints allow partial rollback of a transaction without aborting it entirely. The savepoint is created with a cryptographically random name to avoid collisions when multiple savepoints are used concurrently.
func NewSavepoint ¶
NewSavepoint creates and names a new SQLite savepoint within the given transaction. The savepoint is immediately active. Use Release() to commit the savepoint or Rollback() to undo all statements since it was created.
func (*Savepoint) Name ¶
Name returns the SQLite savepoint name. Useful for debugging and for executing raw ROLLBACK TO or RELEASE statements if needed.
type Task ¶
type Task struct {
// contains filtered or unexported fields
}
Task is a concrete implementation of TaskFunc that wraps a query and its execution function. It is created by the package-level helper functions Query, QueryRow, Exec, and Commit.
func (*Task) Exec ¶
func (t *Task) Exec(tx *sql.Tx) TaskResult
Exec implements TaskFunc by running the stored execution function.
func (*Task) Notify ¶
func (t *Task) Notify() chan TaskResult
Notify implements TaskFunc by returning the result channel.
type TaskArgs ¶
type TaskArgs struct {
// contains filtered or unexported fields
}
TaskArgs bundles a query string with its parameters for deferred execution.
type TaskFunc ¶
type TaskFunc interface {
// Exec executes the task using the provided transaction. It returns
// the result of the execution (e.g., sql.Result for writes) or an error.
Exec(w *sql.Tx) TaskResult
// Notify returns a channel that receives the TaskResult once Exec completes.
// The caller uses this to wait for the task to finish.
Notify() chan TaskResult
// Flush returns true if this task should trigger an immediate commit.
// Used by Commit task to force a flush after buffering completes.
Flush() bool
}
TaskFunc is the interface implemented by tasks submitted to the Writer. It represents a unit of work to be executed within a SQLite transaction.
func Commit ¶
Commit creates a TaskFunc that commits the current transaction. It also sets Flush() to true, ensuring any pending buffered writes are flushed before this task is processed.
func Exec ¶
Exec creates a TaskFunc that executes a write statement (INSERT, UPDATE, DELETE, etc.) with the given arguments. The statement is not executed until the Writer's flush goroutine processes the task.
type TaskResult ¶
type TaskResult struct {
Result any // the execution result, typically a sql.Result or *sql.Row
Error error // any error that occurred during execution
}
TaskResult holds the outcome of a task execution.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is a buffered transaction that batches multiple write statements and executes them atomically on Commit. Unlike a traditional database transaction, Tx does not hold a lock on the database; statements are buffered locally and submitted to the Writer's shared transaction on Commit.
func (*Tx) Commit ¶
Commit executes all buffered statements atomically in the Writer's global transaction. All statements are committed together or rolled back together on error. After Commit is called, the Tx can no longer be used.
func (*Tx) Exec ¶
Exec buffers a write statement for later execution. The statement is NOT executed immediately; it is held in memory until Commit is called, at which point all buffered statements are executed atomically within a single SQLite transaction. Returns (nil, nil) because execution is deferred.
type Writer ¶
Writer provides transparent batch writing with channel-based coordination. All writes are serialized through a single goroutine via cmdCh.
func NewWriter ¶
func NewWriter(db *sql.DB, cfg BufferConfig) *Writer
NewWriter creates a Writer wrapping the provided *sql.DB. The returned Writer starts a background flush goroutine immediately. Close must be called to shut it down cleanly.
func (*Writer) BeginTx ¶
BeginTx starts a new buffered transaction. The returned Tx does not acquire a database lock immediately; statements are buffered locally and only committed when Tx.Commit is called. The opts parameter is accepted for compatibility with database/sql but is not used (savepoints handle the transactional semantics internally).
func (*Writer) Close ¶
Close signals the flush goroutine to stop, waits for it to drain and commit any remaining buffered work, then marks the writer as closed. It is safe to call Close multiple times concurrently; subsequent calls return nil immediately.
func (*Writer) Commit ¶
Commit flushes any pending buffered statements by committing the current transaction. If there is no pending work, it is a no-op. After a successful commit, a new lazy transaction is started for subsequent writes.
func (*Writer) Exec ¶
Exec executes a write statement (INSERT, UPDATE, DELETE, etc.) asynchronously. The statement is buffered and flushed according to the BufferConfig settings. This is the non-context variant; use ExecContext if you need cancellation or deadline control.
func (*Writer) ExecContext ¶
ExecContext executes a write statement (INSERT, UPDATE, DELETE, etc.) asynchronously. The statement is buffered and flushed according to the BufferConfig settings. If the context is cancelled before the statement is processed, ctx.Err() is returned.