Files
seaweedfs/weed/filer/postgres/postgres_flat_sql_gen_test.go
T
Chris LuandClaude Opus 5.5 38422bd1fa filer: RecursiveLister, a subtree in path order from one range of postgres_flat (#13261)
* 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>
2026-12-02 12:03:18 -08:00

103 lines
4.1 KiB
Go

package postgres
import (
"fmt"
"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", "b", "%", 10)
if !strings.Contains(list, "WHERE path>$2 AND path<$3 AND directory=$1") || !strings.Contains(list, "ORDER BY path ASC") ||
fmt.Sprint(args) != fmt.Sprint([]interface{}{"/dir", "/dir/b", "/dir0", "%", int64(10)}) {
t.Fatalf("list %s %v", list, args)
}
gen.ForceBinaryCollation = true
list, args = gen.GetSqlListInclusive("filemeta", "/", "", "%", 10)
if !strings.Contains(list, `WHERE path COLLATE "C">=$2`) || !strings.Contains(list, `ORDER BY path COLLATE "C"`) || args[1] != "/" || args[2] != "0" {
t.Fatalf("list without a byte-ordered column: %s %v", list, args)
}
}
func TestFlatRecursiveListing(t *testing.T) {
gen := &SqlGenPostgresFlat{}
list, args := gen.GetSqlListRecursive("bkt", "/a/b", "/a/", "/a0", 100)
if !strings.Contains(list, "WHERE path>$1 AND path>=$2 AND path<$3 ORDER BY path ASC LIMIT $4") || len(args) != 4 || args[0] != "/a/b" || args[2] != "/a0" {
t.Fatalf("list %s %v", list, args)
}
list, args = gen.GetSqlListRecursive("bkt", "", "/", "", 100)
if strings.Contains(list, "$4") || len(args) != 3 {
t.Fatalf("unbounded list %s %v", list, args)
}
gen.ForceBinaryCollation = true
if list, _ = gen.GetSqlListRecursive("bkt", "", "/", "/0", 1); !strings.Contains(list, `ORDER BY path COLLATE "C"`) {
t.Fatalf("list without a byte-ordered column: %s", list)
}
}
func TestFlatPathListing(t *testing.T) {
gen := &SqlGenPostgresFlat{}
list, args := gen.GetSqlListPath("bkt", "/a", "/a/b", "/a/", "/a0", 100)
if !strings.Contains(list, "CASE WHEN directory=$1 THEN meta END") || !strings.Contains(list, "WHERE path>$2 AND path>=$3 AND path<$4 ORDER BY path ASC LIMIT $5") ||
fmt.Sprint(args) != fmt.Sprint([]interface{}{"/a", "/a/b", "/a/", "/a0", int64(100)}) {
t.Fatalf("list %s %v", list, args)
}
gen.ForceBinaryCollation = true
if list, _ = gen.GetSqlListPath("bkt", "/", "", "/", "0", 1); !strings.Contains(list, `ORDER BY path COLLATE "C"`) {
t.Fatalf("list without a byte-ordered column: %s", list)
}
}
func TestFlatDeleteFolderChildrenReadsThePathRange(t *testing.T) {
gen := &SqlGenPostgresFlat{}
for dir, want := range map[string][]interface{}{
"/": {"/", "0", "/"},
"/a/b": {"/a/b/", "/a/b0", "/a/b"},
} {
del, args := gen.GetSqlDeleteFolderChildren("filemeta", dir)
if !strings.Contains(del, "WHERE path>=$1 AND path<$2 AND directory=$3") || fmt.Sprint(args) != fmt.Sprint(want) {
t.Fatalf("delete children of %s: %s %v", dir, del, args)
}
}
}
func TestFlatStatementsEscapeTheTableName(t *testing.T) {
gen := &SqlGenPostgresFlat{SqlGenPostgres{UpsertQueryTemplate: DefaultFlatUpsertQuery}}
for _, statement := range []string{
first(gen.GetSqlInsert(`a"b`, abstract_sql.EntryRowKey("/", "x"), nil)),
first(gen.GetSqlFind(`a"b`, abstract_sql.EntryRowKey("/", "x"))),
first(gen.GetSqlListExclusive(`a"b`, "/", "", "%", 1)),
} {
if !strings.Contains(statement, `"a""b"`) {
t.Fatalf("table name not escaped: %s", statement)
}
}
}
func first(s string, _ []interface{}) string { return s }