mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
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
This commit is contained in:
1 parent
a341909a81
commit
cba6549067
2 files changed
+125
No files matched your search
@@ -0,0 +1,83 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
||||
)
|
||||
|
||||
// SqlGenPostgresFlat keys each row by its full path. The (directory, name)
|
||||
// unique constraint is the index directory listings read.
|
||||
type SqlGenPostgresFlat struct {
|
||||
SqlGenPostgres
|
||||
}
|
||||
|
||||
const DefaultFlatCreateTableQuery = `CREATE TABLE IF NOT EXISTS "%s" (path TEXT COLLATE "C" PRIMARY KEY, directory TEXT COLLATE "C" NOT NULL, name TEXT COLLATE "C" NOT NULL, meta bytea, UNIQUE (directory, name))`
|
||||
|
||||
// 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{})
|
||||
|
||||
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, tableName), args
|
||||
}
|
||||
return fmt.Sprintf(`INSERT INTO "%s" (path,directory,name,meta) VALUES($1,$2,$3,$4)`, 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`, 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`, 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)`, 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`, tableName), []interface{}{flatPath(key)}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgresFlat) GetSqlDeleteFolderChildren(tableName string, dir string) (string, []interface{}) {
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE directory=$1`, tableName), []interface{}{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`, tableName),
|
||||
[]interface{}{flatPath(after), limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgresFlat) GetSqlListExclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf(`SELECT name, meta FROM "%s" WHERE directory=$1 AND %s>$2 AND %s like $3 ESCAPE '!' ORDER BY %s ASC LIMIT $4`, tableName, name, name, name),
|
||||
[]interface{}{dir, startFileName, namePattern, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgresFlat) GetSqlListInclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf(`SELECT name, meta FROM "%s" WHERE directory=$1 AND %s>=$2 AND %s like $3 ESCAPE '!' ORDER BY %s ASC LIMIT $4`, tableName, name, name, name),
|
||||
[]interface{}{dir, startFileName, namePattern, limit}
|
||||
}
|
||||
Reference in new issue
Block a user