mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +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}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
||||
)
|
||||
|
||||
func TestFlatPath(t *testing.T) {
|
||||
for _, c := range []struct{ dir, name, want string }{
|
||||
{"/", "", "/"},
|
||||
{"/", "a.txt", "/a.txt"},
|
||||
{"/dir/sub", "a.txt", "/dir/sub/a.txt"},
|
||||
{"", "", ""},
|
||||
} {
|
||||
if got := flatPath(abstract_sql.EntryRowKey(c.dir, c.name)); got != c.want {
|
||||
t.Errorf("flatPath(%q, %q) = %q, want %q", c.dir, c.name, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlatStatementsAddressRowsByPath(t *testing.T) {
|
||||
gen := &SqlGenPostgresFlat{SqlGenPostgres{UpsertQueryTemplate: DefaultFlatUpsertQuery}}
|
||||
key := abstract_sql.EntryRowKey("/dir", "a.txt")
|
||||
insert, args := gen.GetSqlInsert("filemeta", key, []byte("m"))
|
||||
if !strings.Contains(insert, "ON CONFLICT (path)") || args[0] != "/dir/a.txt" || args[1] != "/dir" || args[2] != "a.txt" {
|
||||
t.Fatalf("insert %s %v", insert, args)
|
||||
}
|
||||
update, args := gen.GetSqlUpdate("filemeta", key, []byte("m"))
|
||||
if !strings.Contains(update, "WHERE path=$1") || args[0] != "/dir/a.txt" {
|
||||
t.Fatalf("update %s %v", update, args)
|
||||
}
|
||||
list, args := gen.GetSqlListExclusive("filemeta", "/dir", "", "%", 10)
|
||||
if !strings.Contains(list, "WHERE directory=$1") || args[0] != "/dir" {
|
||||
t.Fatalf("list %s %v", list, args)
|
||||
}
|
||||
gen.ForceBinaryCollation = true
|
||||
if list, _ = gen.GetSqlListInclusive("filemeta", "/dir", "", "%", 10); !strings.Contains(list, `ORDER BY name COLLATE "C"`) {
|
||||
t.Fatalf("list without a byte-ordered column: %s", list)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user