mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* filer/store_test: conformance suite for filer stores Entries in and out of buckets, byte-ordered and prefixed listings, key-value records, transactions, bucket tables and the snapshot scan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer: run postgres2 and mysql2 through the conformance suite RUN_POSTGRES_TESTS and RUN_MYSQL_TESTS enable them, as RUN_REDIS_TESTS does the Redis stores; each test gets a schema or database of its own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * ci: give the go test job Postgres and MySQL services Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: generators bind the arguments of row statements Insert, update, find and delete now take the row key and return their arguments with the query, so a generator decides how a key maps to its columns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: generators bind the arguments of directory statements Listing and deleting a directory's children take the directory and return their arguments with the query. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: batch lookup takes row keys and matches rows by name and directory A key-value key's directory column already determines its dirhash, so the batch read no longer selects dirhash to match its rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: scan pages take the row key of the last row as their cursor Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: test the arguments each generator binds Each statement's arguments must match its placeholders in count and column. Check them for every generator method, including the Postgres UPDATE whose parameters are now numbered key first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres: escape the table name inside its quotes A double quote in it is doubled, as PostgreSQL reads a quoted identifier. Bucket names cannot hold one; the statements no longer rely on that. Reported by CodeQL (go/unsafe-quoting) on the lines this branch changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres: keep the nameExpr doc comment on nameExpr Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
134 lines
4.0 KiB
Go
134 lines
4.0 KiB
Go
//go:build (linux || darwin || windows) && sqlite
|
|
|
|
// limited GOOS due to modernc.org/libc/unistd
|
|
|
|
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer/mysql"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
func init() {
|
|
filer.Stores = append(filer.Stores, &SqliteStore{})
|
|
}
|
|
|
|
type SqliteStore struct {
|
|
abstract_sql.AbstractSqlStore
|
|
}
|
|
|
|
func (store *SqliteStore) GetName() string {
|
|
return "sqlite"
|
|
}
|
|
|
|
// sqliteSqlGen is the MySQL generator with SQLite's own page cursor: SQLite
|
|
// seeks a row-value comparison on the primary key but scans the OR form.
|
|
type sqliteSqlGen struct {
|
|
*mysql.SqlGenMysql
|
|
}
|
|
|
|
func (gen sqliteSqlGen) GetSqlScanPage(tableName string, after abstract_sql.RowKey, limit int) (string, []interface{}) {
|
|
return fmt.Sprintf("SELECT `dirhash`, `name`, `directory`, `meta` FROM `%s` WHERE (`dirhash`, `name`) > (?, ?) ORDER BY `dirhash` ASC, `name` ASC LIMIT ?", tableName),
|
|
[]interface{}{after.DirHash, after.Name, limit}
|
|
}
|
|
|
|
// ScanEntries offers the snapshot scan only in WAL mode: with a rollback
|
|
// journal a long read transaction holds a shared lock and writers fail after
|
|
// the busy timeout.
|
|
func (store *SqliteStore) ScanEntries(ctx context.Context, dirPath util.FullPath, fn func(ctx context.Context, entry *filer.Entry) error) error {
|
|
var mode string
|
|
if err := store.DB.QueryRowContext(ctx, "PRAGMA journal_mode").Scan(&mode); err != nil {
|
|
return fmt.Errorf("%w: journal_mode: %v", filer.ErrUnsupportedEntryScan, err)
|
|
}
|
|
if !strings.EqualFold(mode, "wal") {
|
|
return fmt.Errorf("%w: sqlite journal_mode is %q, the scan needs wal", filer.ErrUnsupportedEntryScan, mode)
|
|
}
|
|
return store.AbstractSqlStore.ScanEntries(ctx, dirPath, fn)
|
|
}
|
|
|
|
func (store *SqliteStore) Initialize(configuration util.Configuration, prefix string) (err error) {
|
|
dbFile := configuration.GetString(prefix + "dbFile")
|
|
createTable := `CREATE TABLE IF NOT EXISTS "%s" (
|
|
dirhash BIGINT,
|
|
name VARCHAR(1000),
|
|
directory TEXT,
|
|
meta BLOB,
|
|
PRIMARY KEY (dirhash, name)
|
|
) WITHOUT ROWID;`
|
|
upsertQuery := `INSERT INTO "%s"(dirhash,name,directory,meta)VALUES(?,?,?,?)
|
|
ON CONFLICT(dirhash,name) DO UPDATE SET
|
|
directory=excluded.directory,
|
|
meta=excluded.meta;
|
|
`
|
|
return store.initialize(
|
|
dbFile,
|
|
createTable,
|
|
upsertQuery,
|
|
)
|
|
}
|
|
|
|
// sqliteDSN keeps the store's two pools on one database and lets a writer that
|
|
// meets the other pool's reader wait for it instead of failing outright. A bare
|
|
// :memory: is private to each connection, so it has to be named and shared.
|
|
func sqliteDSN(dbFile string) string {
|
|
dsn := dbFile
|
|
if dsn == ":memory:" {
|
|
dsn = fmt.Sprintf("file:seaweedfs%d?mode=memory&cache=shared", time.Now().UnixNano())
|
|
}
|
|
separator := "?"
|
|
if strings.Contains(dsn, "?") {
|
|
separator = "&"
|
|
}
|
|
return dsn + separator + "_pragma=busy_timeout(10000)"
|
|
}
|
|
|
|
func (store *SqliteStore) initialize(dbFile, createTable, upsertQuery string) (err error) {
|
|
|
|
store.SupportBucketTable = true
|
|
store.SqlGenerator = sqliteSqlGen{&mysql.SqlGenMysql{
|
|
CreateTableSqlTemplate: createTable,
|
|
DropTableSqlTemplate: "drop table if exists `%s`",
|
|
UpsertQueryTemplate: upsertQuery,
|
|
}}
|
|
|
|
dsn := sqliteDSN(dbFile)
|
|
|
|
db, dbErr := sql.Open("sqlite", dsn)
|
|
if dbErr != nil {
|
|
if db != nil {
|
|
db.Close()
|
|
}
|
|
return fmt.Errorf("can not connect to %s error:%v", dbFile, dbErr)
|
|
}
|
|
|
|
if err = db.Ping(); err != nil {
|
|
return fmt.Errorf("connect to %s error:%v", dbFile, err)
|
|
}
|
|
|
|
if err = store.UseConnectionPools(db, func() (*sql.DB, error) {
|
|
return sql.Open("sqlite", dsn)
|
|
}, 1, 1, 0); err != nil {
|
|
return err
|
|
}
|
|
// The main pool is one connection, which a long scan must not hold.
|
|
if store.ScanDB, err = sql.Open("sqlite", dsn); err != nil {
|
|
return fmt.Errorf("open scan connection to %s: %v", dbFile, err)
|
|
}
|
|
store.ScanDB.SetMaxOpenConns(1)
|
|
|
|
if err = store.CreateTable(context.Background(), abstract_sql.DEFAULT_TABLE); err != nil {
|
|
return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err)
|
|
}
|
|
|
|
return nil
|
|
}
|