Documentation
¶
Index ¶
- Variables
- type Action
- type BufferConfig
- type BufferWriter
- func (bw *BufferWriter) Begin() (*Tx, error)
- func (bw *BufferWriter) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (bw *BufferWriter) Close() error
- func (bw *BufferWriter) Exec(query string, args ...any) (sql.Result, error)
- func (bw *BufferWriter) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (bw *BufferWriter) Flush() error
- type DB
- func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- 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) Flush() 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 TaskFunc
- type TaskResult
- type Tx
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 DefaultBufferConfig ¶ added in v0.0.3
func DefaultBufferConfig() BufferConfig
DefaultBufferConfig returns a BufferConfig with defaults applied, reading from SQLITE_BUFFER_SIZE and SQLITE_BUFFER_FLUSH_INTERVAL env vars when set. Call this at Open time to respect environment configuration.
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 BufferWriter ¶ added in v0.0.2
BufferWriter 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) *BufferWriter
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 (*BufferWriter) Begin ¶ added in v0.0.4
func (bw *BufferWriter) Begin() (*Tx, error)
func (*BufferWriter) BeginTx ¶ added in v0.0.2
BeginTx acquires an exclusive write lock, flushes any pending buffered writes, and returns a Tx that executes statements immediately through the Writer's background goroutine. No other writes via Exec/ExecContext can proceed until Tx.Commit or Tx.Rollback is called.
func (*BufferWriter) Close ¶ added in v0.0.2
func (bw *BufferWriter) Close() error
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 (*BufferWriter) Exec ¶ added in v0.0.2
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 (*BufferWriter) ExecContext ¶ added in v0.0.2
func (bw *BufferWriter) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
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.
func (*BufferWriter) Flush ¶ added in v0.0.2
func (bw *BufferWriter) Flush() error
type DB ¶
type DB struct {
Writer *BufferWriter
Reader *sql.DB
// contains filtered or unexported fields
}
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(bw *BufferWriter) 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 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(bw *BufferWriter) 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
Action() Action
}
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 ¶
func Commit() TaskFunc
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.
func Query ¶
Query creates a read-only TaskFunc that executes a query with the given arguments and returns a *sql.Row. The query 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
Buffered bool // true if the task was buffered and not executed immediately
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 an exclusive transaction that holds the Writer's write lock for its entire lifetime. All Exec calls are executed immediately through the Writer's background goroutine within the shared transaction. No other writes via Writer.Exec or Writer.ExecContext can proceed while a Tx is active. The lock is released when Commit or Rollback is called.
func (*Tx) Commit ¶
Commit flushes the exclusive transaction and releases the write lock. After Commit returns, the Tx cannot be reused.