mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +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/postgres: build the connection string with pgxutil.BuildDSN The credential store already does; it quotes values per libpq, so a password or certificate path with spaces or quotes no longer breaks the connection string. The OpenPGXDB alias has no callers left. 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/postgres: postgres and postgres2 share their connection setup ReadConnection reads the settings and their defaults, Connection.Open opens the store's pools. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: key-value records may live in a table of their own A store whose entry tables have another layout keeps its key-value records in KvTable, written by KvSqlGenerator; its scans then read only entries and need not tell the records apart. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres: SQL generator keyed by the full path Rows have path as their primary key; a (directory, name) unique constraint serves the directory listings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer: postgres_flat store Entries are keyed by their full path, the object key inside a bucket's table; buckets get tables of their own as in postgres2. Key-value records keep the postgres2 layout in filemeta_kv. The store refuses a filemeta table of another layout, so it needs a schema of its own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer: register postgres_flat and add it to the filer.toml scaffold 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_flat: name a missing filemeta table instead of a foreign layout With createTable = false and no filemeta table, the layout check said the table had no path column and blamed a postgres2 layout. Report the missing table and the key-value table that also has to be created. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres: postgres_flat escapes the table name inside its quotes A double quote in it is doubled, as PostgreSQL reads a quoted identifier. Bucket names cannot hold one, but the statements no longer rely on that. Reported by CodeQL (go/unsafe-quoting). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * 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: postgres_flat shares the generator's tableIdent 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> * filer/postgres_flat: find filemeta along the search path when checking its layout The store's queries name filemeta unqualified, so with schema = "a,b" they reach a table in b; the layout check looked in current_schema() only and refused it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres_flat: force byte order when the path column is locale-ordered Directory listings order and bound rows by path, but only the name column's collation was checked. A createTable with a locale-ordered path column listed in locale order, and byte-order bounds missed entries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres: read the ordered columns' collation along the search path The collation check looked for filemeta in current_schema() only. With the tables in a later schema of the search path it found no column and took the database's order, so a locale-ordered path column next to a C database went unforced, and byte-ordered columns in a locale database got a needless sort. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres_flat: list a directory from its path range A directory's children are the rows of its path range whose directory is the listed one. The listing reads that range in growing batches and skips the rows of the children's subtrees in Go, so a versioned object's .versions rows cost no query of their own. Past 64 rows of one subtree it queries again from that subtree's end. A name prefix narrows the range. Listing no longer reads the (directory, name) index. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres_flat: find a folder's children in its path range Deleting a folder's children looked them up by directory alone, which only the (directory, name) index serves. Bound the delete by the folder's path range as well. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres_flat: key the table by path alone Listing and deleting a folder's children both read the path range now, so the (directory, name) unique index only cost writes and storage: flatPath already makes the path unique per (directory, name). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres: read the ICU or builtin database locale before datcollate An ICU-default database can report datcollate 'C' while its default collation orders by the ICU locale, so a name column on the database default looked byte-ordered and the listing kept locale order. Read datlocprovider and the provider locale (via to_jsonb, so servers before Postgres 15 still answer) and treat an ICU default as locale-ordered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres_flat: list a folder's children from its path range The bucket enumeration listed /buckets by directory and ordered by name, a sequential scan and sort now that path is the only index. Read the folder's path range in path order instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres_flat: only the path column needs byte order Listings now order and range by path alone, and directory and name are only compared for equality, which no deterministic collation changes. Drop their COLLATE "C" and check only the path column's order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
4.9 KiB
Go
112 lines
4.9 KiB
Go
package postgres
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
|
)
|
|
|
|
// SqlGenPostgresFlat keys each row by its full path, the only index. A
|
|
// directory listing reads the directory's path range.
|
|
type SqlGenPostgresFlat struct {
|
|
SqlGenPostgres
|
|
}
|
|
|
|
const DefaultFlatCreateTableQuery = `CREATE TABLE IF NOT EXISTS "%s" (path TEXT COLLATE "C" PRIMARY KEY, directory TEXT NOT NULL, name TEXT NOT NULL, meta bytea)`
|
|
|
|
// DefaultFlatUpsertQuery takes $1 path, $2 directory, $3 name and $4 meta.
|
|
const DefaultFlatUpsertQuery = `INSERT INTO "%s" (path,directory,name,meta) VALUES($1,$2,$3,$4) ON CONFLICT (path) DO UPDATE SET meta=EXCLUDED.meta`
|
|
|
|
var (
|
|
_ = abstract_sql.SqlGenerator(&SqlGenPostgresFlat{})
|
|
_ = abstract_sql.PathListSqlGenerator(&SqlGenPostgresFlat{})
|
|
)
|
|
|
|
func flatPath(key abstract_sql.RowKey) string {
|
|
switch key.Directory {
|
|
case "":
|
|
return key.Name
|
|
case "/":
|
|
return "/" + key.Name
|
|
}
|
|
return key.Directory + "/" + key.Name
|
|
}
|
|
|
|
func (gen *SqlGenPostgresFlat) GetSqlInsert(tableName string, key abstract_sql.RowKey, meta []byte) (string, []interface{}) {
|
|
args := []interface{}{flatPath(key), key.Directory, key.Name, meta}
|
|
if gen.UpsertQueryTemplate != "" {
|
|
return fmt.Sprintf(gen.UpsertQueryTemplate, tableIdent(tableName)), args
|
|
}
|
|
return fmt.Sprintf(`INSERT INTO "%s" (path,directory,name,meta) VALUES($1,$2,$3,$4)`, tableIdent(tableName)), args
|
|
}
|
|
|
|
func (gen *SqlGenPostgresFlat) GetSqlUpdate(tableName string, key abstract_sql.RowKey, meta []byte) (string, []interface{}) {
|
|
return fmt.Sprintf(`UPDATE "%s" SET meta=$2 WHERE path=$1`, tableIdent(tableName)), []interface{}{flatPath(key), meta}
|
|
}
|
|
|
|
func (gen *SqlGenPostgresFlat) GetSqlFind(tableName string, key abstract_sql.RowKey) (string, []interface{}) {
|
|
return fmt.Sprintf(`SELECT meta FROM "%s" WHERE path=$1`, tableIdent(tableName)), []interface{}{flatPath(key)}
|
|
}
|
|
|
|
func (gen *SqlGenPostgresFlat) GetSqlFindBatch(tableName string, keys []abstract_sql.RowKey) (string, []interface{}) {
|
|
params := make([]string, len(keys))
|
|
args := make([]interface{}, len(keys))
|
|
for i, key := range keys {
|
|
params[i] = fmt.Sprintf("$%d", i+1)
|
|
args[i] = flatPath(key)
|
|
}
|
|
return fmt.Sprintf(`SELECT name, directory, meta FROM "%s" WHERE path IN (%s)`, tableIdent(tableName), strings.Join(params, ",")), args
|
|
}
|
|
|
|
func (gen *SqlGenPostgresFlat) GetSqlDelete(tableName string, key abstract_sql.RowKey) (string, []interface{}) {
|
|
return fmt.Sprintf(`DELETE FROM "%s" WHERE path=$1`, tableIdent(tableName)), []interface{}{flatPath(key)}
|
|
}
|
|
|
|
// GetSqlDeleteFolderChildren finds the children in the directory's path range.
|
|
func (gen *SqlGenPostgresFlat) GetSqlDeleteFolderChildren(tableName string, dir string) (string, []interface{}) {
|
|
path := "path"
|
|
if gen.ForceBinaryCollation {
|
|
path = `path COLLATE "C"`
|
|
}
|
|
base := strings.TrimSuffix(dir, "/")
|
|
return fmt.Sprintf(`DELETE FROM "%s" WHERE %s>=$1 AND %s<$2 AND directory=$3`, tableIdent(tableName), path, path),
|
|
[]interface{}{base + "/", base + "0", dir}
|
|
}
|
|
|
|
// GetSqlScanPage pages by path; the dirhash column is a placeholder.
|
|
func (gen *SqlGenPostgresFlat) GetSqlScanPage(tableName string, after abstract_sql.RowKey, limit int) (string, []interface{}) {
|
|
return fmt.Sprintf(`SELECT 0::bigint, name, directory, meta FROM "%s" WHERE path > $1 ORDER BY path ASC LIMIT $2`, tableIdent(tableName)),
|
|
[]interface{}{flatPath(after), limit}
|
|
}
|
|
|
|
func (gen *SqlGenPostgresFlat) GetSqlListExclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
|
return gen.listChildren(tableName, ">", dir, startFileName, namePattern, limit)
|
|
}
|
|
|
|
func (gen *SqlGenPostgresFlat) GetSqlListInclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
|
return gen.listChildren(tableName, ">=", dir, startFileName, namePattern, limit)
|
|
}
|
|
|
|
// listChildren reads dir's path range, so the children come in path order.
|
|
func (gen *SqlGenPostgresFlat) listChildren(tableName, op, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
|
path := "path"
|
|
if gen.ForceBinaryCollation {
|
|
path = `path COLLATE "C"`
|
|
}
|
|
base := strings.TrimSuffix(dir, "/")
|
|
return fmt.Sprintf(`SELECT name, meta FROM "%s" WHERE %s%s$2 AND %s<$3 AND directory=$1 AND name like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableIdent(tableName), path, op, path, path),
|
|
[]interface{}{dir, base + "/" + startFileName, base + "0", namePattern, limit}
|
|
}
|
|
|
|
// GetSqlListPath leaves meta out of the rows below dir's children, which a
|
|
// directory listing skips.
|
|
func (gen *SqlGenPostgresFlat) GetSqlListPath(tableName, dir, after, from, to string, limit int64) (string, []interface{}) {
|
|
path := "path"
|
|
if gen.ForceBinaryCollation {
|
|
path = `path COLLATE "C"`
|
|
}
|
|
return fmt.Sprintf(`SELECT directory, name, CASE WHEN directory=$1 THEN meta END FROM "%s" WHERE %s>$2 AND %s>=$3 AND %s<$4 ORDER BY %s ASC LIMIT $5`, tableIdent(tableName), path, path, path, path),
|
|
[]interface{}{dir, after, from, to, limit}
|
|
}
|