mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
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
This commit is contained in:
1 parent
4ae00a4728
commit
38aa347c35
7 files changed
+56
-29
No files matched your search
@@ -41,9 +41,11 @@ type SqlGenerator interface {
|
||||
// matching count (dirhash, name, directory) triples, as GetSqlFind does one.
|
||||
GetSqlFindBatch(tableName string, count int) string
|
||||
GetSqlDelete(tableName string, key RowKey) (string, []interface{})
|
||||
GetSqlDeleteFolderChildren(tableName string) string
|
||||
GetSqlListExclusive(tableName string) string
|
||||
GetSqlListInclusive(tableName string) string
|
||||
GetSqlDeleteFolderChildren(tableName string, dir string) (string, []interface{})
|
||||
// GetSqlListExclusive selects name and meta of the entries in dir after
|
||||
// startFileName whose name is LIKE namePattern, in name order.
|
||||
GetSqlListExclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{})
|
||||
GetSqlListInclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{})
|
||||
// GetSqlScanPage returns the query and arguments selecting dirhash, name,
|
||||
// directory and meta of up to limit rows after the (dirhash, name) cursor.
|
||||
GetSqlScanPage(tableName string, dirhash int64, name string, limit int) (string, []interface{})
|
||||
@@ -416,8 +418,9 @@ func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpat
|
||||
}
|
||||
}
|
||||
|
||||
glog.V(4).InfofCtx(ctx, "delete %s SQL %s %d", string(shortPath), store.GetSqlDeleteFolderChildren(bucket), util.HashStringToLong(string(shortPath)))
|
||||
res, err := db.ExecContext(ctx, store.GetSqlDeleteFolderChildren(bucket), util.HashStringToLong(string(shortPath)), string(shortPath))
|
||||
sqlText, args := store.GetSqlDeleteFolderChildren(bucket, string(shortPath))
|
||||
glog.V(4).InfofCtx(ctx, "delete %s SQL %s %v", string(shortPath), sqlText, args)
|
||||
res, err := db.ExecContext(ctx, sqlText, args...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("deleteFolderChildren %s: %w", fullpath, err)
|
||||
}
|
||||
@@ -453,12 +456,13 @@ func (store *AbstractSqlStore) ListDirectoryPrefixedEntries(ctx context.Context,
|
||||
return lastFileName, fmt.Errorf("findDB %s : %v", dirPath, err)
|
||||
}
|
||||
|
||||
sqlText := store.GetSqlListExclusive(bucket)
|
||||
getSqlList := store.GetSqlListExclusive
|
||||
if includeStartFile {
|
||||
sqlText = store.GetSqlListInclusive(bucket)
|
||||
getSqlList = store.GetSqlListInclusive
|
||||
}
|
||||
sqlText, args := getSqlList(bucket, string(shortPath), startFileName, LikePattern(prefix), limit+1)
|
||||
|
||||
rows, err := db.QueryContext(ctx, sqlText, util.HashStringToLong(string(shortPath)), startFileName, string(shortPath), LikePattern(prefix), limit+1)
|
||||
rows, err := db.QueryContext(ctx, sqlText, args...)
|
||||
if err != nil {
|
||||
return lastFileName, fmt.Errorf("list %s : %v", dirPath, err)
|
||||
}
|
||||
|
||||
@@ -228,7 +228,8 @@ func (store *AbstractSqlStore) listBucketNames(ctx context.Context, tx *sql.Tx,
|
||||
}
|
||||
var rows []row
|
||||
if err := func() error {
|
||||
res, err := tx.QueryContext(ctx, store.GetSqlListExclusive(DEFAULT_TABLE), util.HashStringToLong(bucketsRoot), startFrom, bucketsRoot, "%", page)
|
||||
sqlText, args := store.GetSqlListExclusive(DEFAULT_TABLE, bucketsRoot, startFrom, "%", page)
|
||||
res, err := tx.QueryContext(ctx, sqlText, args...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list %s: %v", bucketsRoot, err)
|
||||
}
|
||||
|
||||
@@ -149,8 +149,12 @@ func TestBucketHooksSkipInvalidBucketNames(t *testing.T) {
|
||||
|
||||
type listGen struct{ SqlGenerator }
|
||||
|
||||
func (listGen) GetSqlListExclusive(string) string { return "SELECT name, meta" }
|
||||
func (listGen) GetSqlListInclusive(string) string { return "SELECT name, meta" }
|
||||
func (listGen) GetSqlListExclusive(string, string, string, string, int64) (string, []interface{}) {
|
||||
return "SELECT name, meta", nil
|
||||
}
|
||||
func (listGen) GetSqlListInclusive(string, string, string, string, int64) (string, []interface{}) {
|
||||
return "SELECT name, meta", nil
|
||||
}
|
||||
|
||||
// A listing that loses its connection partway through must report the error,
|
||||
// not return the rows read so far as if the directory ended there.
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
)
|
||||
|
||||
type SqlGenMysql struct {
|
||||
@@ -57,8 +58,8 @@ func (gen *SqlGenMysql) GetSqlDelete(tableName string, key abstract_sql.RowKey)
|
||||
return fmt.Sprintf("DELETE FROM `%s` WHERE `dirhash` = ? AND `name` = ? AND `directory` = ?", tableName), key.Args()
|
||||
}
|
||||
|
||||
func (gen *SqlGenMysql) GetSqlDeleteFolderChildren(tableName string) string {
|
||||
return fmt.Sprintf("DELETE FROM `%s` WHERE `dirhash` = ? AND `directory` = ?", tableName)
|
||||
func (gen *SqlGenMysql) GetSqlDeleteFolderChildren(tableName string, dir string) (string, []interface{}) {
|
||||
return fmt.Sprintf("DELETE FROM `%s` WHERE `dirhash` = ? AND `directory` = ?", tableName), []interface{}{util.HashStringToLong(dir), dir}
|
||||
}
|
||||
|
||||
// nameExpr forces byte ordering on a non-binary column at the cost of a filesort.
|
||||
@@ -78,14 +79,16 @@ func (gen *SqlGenMysql) GetSqlScanPage(tableName string, dirhash int64, name str
|
||||
[]interface{}{dirhash, dirhash, name, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenMysql) GetSqlListExclusive(tableName string) string {
|
||||
func (gen *SqlGenMysql) GetSqlListExclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf("SELECT `name`, `meta` FROM `%s` WHERE `dirhash` = ? AND %s > ? AND `directory` = ? AND %s LIKE ? ESCAPE '!' ORDER BY %s ASC LIMIT ?", tableName, name, name, name)
|
||||
return fmt.Sprintf("SELECT `name`, `meta` FROM `%s` WHERE `dirhash` = ? AND %s > ? AND `directory` = ? AND %s LIKE ? ESCAPE '!' ORDER BY %s ASC LIMIT ?", tableName, name, name, name),
|
||||
[]interface{}{util.HashStringToLong(dir), startFileName, dir, namePattern, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenMysql) GetSqlListInclusive(tableName string) string {
|
||||
func (gen *SqlGenMysql) GetSqlListInclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf("SELECT `name`, `meta` FROM `%s` WHERE `dirhash` = ? AND %s >= ? AND `directory` = ? AND %s LIKE ? ESCAPE '!' ORDER BY %s ASC LIMIT ?", tableName, name, name, name)
|
||||
return fmt.Sprintf("SELECT `name`, `meta` FROM `%s` WHERE `dirhash` = ? AND %s >= ? AND `directory` = ? AND %s LIKE ? ESCAPE '!' ORDER BY %s ASC LIMIT ?", tableName, name, name, name),
|
||||
[]interface{}{util.HashStringToLong(dir), startFileName, dir, namePattern, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenMysql) GetSqlCreateTable(tableName string) string {
|
||||
|
||||
@@ -42,7 +42,7 @@ func TestEmptyUpsertTemplateFallsBackToPlainInsert(t *testing.T) {
|
||||
|
||||
func TestListSqlDefaultOrderingFollowsColumn(t *testing.T) {
|
||||
gen := &SqlGenMysql{}
|
||||
for _, got := range []string{gen.GetSqlListExclusive("filemeta"), gen.GetSqlListInclusive("filemeta")} {
|
||||
for _, got := range listQueries(gen) {
|
||||
if strings.Contains(got, "BINARY") {
|
||||
t.Fatalf("default list query should not force BINARY, got: %s", got)
|
||||
}
|
||||
@@ -54,7 +54,7 @@ func TestListSqlDefaultOrderingFollowsColumn(t *testing.T) {
|
||||
|
||||
func TestListSqlBinaryOrderingOnNonBinaryColumn(t *testing.T) {
|
||||
gen := &SqlGenMysql{ForceBinaryCollation: true}
|
||||
for _, got := range []string{gen.GetSqlListExclusive("filemeta"), gen.GetSqlListInclusive("filemeta")} {
|
||||
for _, got := range listQueries(gen) {
|
||||
if !strings.Contains(got, "ORDER BY BINARY `name` ASC") {
|
||||
t.Fatalf("expected BINARY ordering, got: %s", got)
|
||||
}
|
||||
@@ -69,7 +69,7 @@ func TestListSqlBinaryOrderingOnNonBinaryColumn(t *testing.T) {
|
||||
|
||||
func TestListSqlEscapesLikeWildcards(t *testing.T) {
|
||||
gen := &SqlGenMysql{}
|
||||
for _, got := range []string{gen.GetSqlListExclusive("filemeta"), gen.GetSqlListInclusive("filemeta")} {
|
||||
for _, got := range listQueries(gen) {
|
||||
if !strings.Contains(got, "LIKE ? ESCAPE '!'") {
|
||||
t.Fatalf("list query must declare the escape char matching LikePattern, got: %s", got)
|
||||
}
|
||||
@@ -90,3 +90,9 @@ func TestIsBinaryCollation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func listQueries(gen *SqlGenMysql) []string {
|
||||
exclusive, _ := gen.GetSqlListExclusive("filemeta", "/dir", "", "%", 10)
|
||||
inclusive, _ := gen.GetSqlListInclusive("filemeta", "/dir", "", "%", 10)
|
||||
return []string{exclusive, inclusive}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
)
|
||||
|
||||
type SqlGenPostgres struct {
|
||||
@@ -71,8 +72,8 @@ func (gen *SqlGenPostgres) GetSqlDelete(tableName string, key abstract_sql.RowKe
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableName), key.Args()
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlDeleteFolderChildren(tableName string) string {
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND directory=$2`, tableName)
|
||||
func (gen *SqlGenPostgres) GetSqlDeleteFolderChildren(tableName string, dir string) (string, []interface{}) {
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND directory=$2`, tableName), []interface{}{util.HashStringToLong(dir), dir}
|
||||
}
|
||||
|
||||
// nameExpr forces byte ordering on a locale-aware column via COLLATE "C".
|
||||
@@ -91,14 +92,16 @@ func (gen *SqlGenPostgres) GetSqlScanPage(tableName string, dirhash int64, name
|
||||
[]interface{}{dirhash, name, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlListExclusive(tableName string) string {
|
||||
func (gen *SqlGenPostgres) GetSqlListExclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableName, name, name, name)
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableName, name, name, name),
|
||||
[]interface{}{util.HashStringToLong(dir), startFileName, dir, namePattern, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlListInclusive(tableName string) string {
|
||||
func (gen *SqlGenPostgres) GetSqlListInclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>=$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableName, name, name, name)
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>=$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableName, name, name, name),
|
||||
[]interface{}{util.HashStringToLong(dir), startFileName, dir, namePattern, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlCreateTable(tableName string) string {
|
||||
|
||||
@@ -39,7 +39,7 @@ func TestEmptyUpsertTemplateFallsBackToPlainInsert(t *testing.T) {
|
||||
|
||||
func TestListSqlDefaultOrderingFollowsColumn(t *testing.T) {
|
||||
gen := &SqlGenPostgres{}
|
||||
for _, got := range []string{gen.GetSqlListExclusive("filemeta"), gen.GetSqlListInclusive("filemeta")} {
|
||||
for _, got := range listQueries(gen) {
|
||||
if strings.Contains(got, "COLLATE") {
|
||||
t.Fatalf("default list query should not force a collation, got: %s", got)
|
||||
}
|
||||
@@ -51,7 +51,7 @@ func TestListSqlDefaultOrderingFollowsColumn(t *testing.T) {
|
||||
|
||||
func TestListSqlBinaryOrderingOnNonBinaryColumn(t *testing.T) {
|
||||
gen := &SqlGenPostgres{ForceBinaryCollation: true}
|
||||
for _, got := range []string{gen.GetSqlListExclusive("filemeta"), gen.GetSqlListInclusive("filemeta")} {
|
||||
for _, got := range listQueries(gen) {
|
||||
if !strings.Contains(got, `ORDER BY name COLLATE "C" ASC`) {
|
||||
t.Fatalf(`expected COLLATE "C" ordering, got: %s`, got)
|
||||
}
|
||||
@@ -66,7 +66,7 @@ func TestListSqlBinaryOrderingOnNonBinaryColumn(t *testing.T) {
|
||||
|
||||
func TestListSqlEscapesLikeWildcards(t *testing.T) {
|
||||
gen := &SqlGenPostgres{}
|
||||
for _, got := range []string{gen.GetSqlListExclusive("filemeta"), gen.GetSqlListInclusive("filemeta")} {
|
||||
for _, got := range listQueries(gen) {
|
||||
if !strings.Contains(got, "like $4 ESCAPE '!'") {
|
||||
t.Fatalf("list query must declare the escape char matching LikePattern, got: %s", got)
|
||||
}
|
||||
@@ -103,3 +103,9 @@ func TestResolveCreateTableQuery(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func listQueries(gen *SqlGenPostgres) []string {
|
||||
exclusive, _ := gen.GetSqlListExclusive("filemeta", "/dir", "", "%", 10)
|
||||
inclusive, _ := gen.GetSqlListInclusive("filemeta", "/dir", "", "%", 10)
|
||||
return []string{exclusive, inclusive}
|
||||
}
|
||||
Reference in new issue
Block a user