mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
* filer: RecursiveLister, a store capability to list a subtree in path order The wrapper hands it to the default store, reading hard links as listings do, and declines it when path-specific stores could hold part of the subtree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: list a subtree with one range scan when rows are keyed by path AbstractSqlStore lists recursively when its generator implements RecursiveListSqlGenerator. The range runs from the directory and prefix to the least string past every path starting with them. A directory whose subtree spans the bucket tables is declined. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres: the postgres_flat generator lists a path range Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/store_test: check recursive listings Byte order across +, -, / and non-ASCII names, prefixes holding a slash, literal % and _, resuming after a path, pages of one and three, a directory outside the buckets, and the directories that span bucket tables. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: a recursive listing's start path outside the bucket table bounds it by full path A start path in another bucket was trimmed as if it were inside this one, so a start path past the bucket listed it all again, and one before it could skip entries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
126 lines
5.6 KiB
Go
126 lines
5.6 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{})
|
|
_ = abstract_sql.RecursiveListSqlGenerator(&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}
|
|
}
|
|
|
|
func (gen *SqlGenPostgresFlat) GetSqlListRecursive(tableName string, after, from, to string, limit int64) (string, []interface{}) {
|
|
path := "path"
|
|
if gen.ForceBinaryCollation {
|
|
path = `path COLLATE "C"`
|
|
}
|
|
if to == "" {
|
|
return fmt.Sprintf(`SELECT directory, name, meta FROM "%s" WHERE %s>$1 AND %s>=$2 ORDER BY %s ASC LIMIT $3`, tableIdent(tableName), path, path, path),
|
|
[]interface{}{after, from, limit}
|
|
}
|
|
return fmt.Sprintf(`SELECT directory, name, meta FROM "%s" WHERE %s>$1 AND %s>=$2 AND %s<$3 ORDER BY %s ASC LIMIT $4`, tableIdent(tableName), path, path, path, path),
|
|
[]interface{}{after, from, to, limit}
|
|
}
|