mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
filer: postgres_flat store
Entries are keyed by their full path, the object key inside a bucket's table; buckets get tables of their own as in postgres2. Key-value records keep the postgres2 layout in filemeta_kv. The store refuses a filemeta table of another layout, so it needs a schema of its own. 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
cba6549067
commit
cb1356f7f1
2 files changed
+173
No files matched your search
@@ -0,0 +1,99 @@
|
||||
// Package postgres_flat is a PostgreSQL filer store whose rows are keyed by
|
||||
// their full path, one table per bucket as in postgres2.
|
||||
package postgres_flat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer/postgres"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
)
|
||||
|
||||
const kvTable = "filemeta_kv"
|
||||
|
||||
var _ filer.BucketAware = (*PostgresFlatStore)(nil)
|
||||
|
||||
func init() {
|
||||
filer.Stores = append(filer.Stores, &PostgresFlatStore{})
|
||||
}
|
||||
|
||||
type PostgresFlatStore struct {
|
||||
abstract_sql.AbstractSqlStore
|
||||
}
|
||||
|
||||
func (store *PostgresFlatStore) GetName() string {
|
||||
return "postgres_flat"
|
||||
}
|
||||
|
||||
func (store *PostgresFlatStore) Initialize(configuration util.Configuration, prefix string) (err error) {
|
||||
configuration.SetDefault(prefix+"enableUpsert", true)
|
||||
return store.initialize(
|
||||
configuration.GetString(prefix+"createTable"),
|
||||
configuration.GetString(prefix+"upsertQuery"),
|
||||
configuration.GetBool(prefix+"enableUpsert"),
|
||||
postgres.ReadConnection(configuration, prefix),
|
||||
)
|
||||
}
|
||||
|
||||
func (store *PostgresFlatStore) initialize(createTable, upsertQuery string, enableUpsert bool, conn postgres.Connection) (err error) {
|
||||
|
||||
store.SupportBucketTable = true
|
||||
store.SkipDDL = createTable == "false"
|
||||
if createTable == "" || createTable == "true" || store.SkipDDL {
|
||||
createTable = postgres.DefaultFlatCreateTableQuery
|
||||
}
|
||||
if !enableUpsert {
|
||||
upsertQuery = ""
|
||||
} else if upsertQuery == "" {
|
||||
upsertQuery = postgres.DefaultFlatUpsertQuery
|
||||
}
|
||||
gen := &postgres.SqlGenPostgresFlat{SqlGenPostgres: postgres.SqlGenPostgres{
|
||||
CreateTableSqlTemplate: createTable,
|
||||
DropTableSqlTemplate: `drop table if exists "%s"`,
|
||||
UpsertQueryTemplate: upsertQuery,
|
||||
}}
|
||||
store.SqlGenerator = gen
|
||||
store.KvSqlGenerator = &postgres.SqlGenPostgres{
|
||||
CreateTableSqlTemplate: postgres.DefaultCreateTableQuery,
|
||||
UpsertQueryTemplate: postgres.DefaultUpsertQuery,
|
||||
}
|
||||
store.KvTable = kvTable
|
||||
|
||||
if err = conn.Open(&store.AbstractSqlStore); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
if !store.SkipDDL {
|
||||
if err = store.CreateTable(ctx, abstract_sql.DEFAULT_TABLE); err != nil {
|
||||
return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err)
|
||||
}
|
||||
if _, err = store.DB.ExecContext(ctx, store.KvSqlGenerator.GetSqlCreateTable(kvTable)); err != nil {
|
||||
return fmt.Errorf("init table %s: %v", kvTable, err)
|
||||
}
|
||||
}
|
||||
if err = checkFlatLayout(ctx, store.DB); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
postgres.ConfigureListOrdering(store.DB, &gen.SqlGenPostgres)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkFlatLayout refuses a filemeta table of the postgres or postgres2 layout.
|
||||
func checkFlatLayout(ctx context.Context, db *sql.DB) error {
|
||||
var found bool
|
||||
if err := db.QueryRowContext(ctx, `SELECT EXISTS (SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = current_schema() AND table_name = $1 AND column_name = 'path')`, abstract_sql.DEFAULT_TABLE).Scan(&found); err != nil {
|
||||
return fmt.Errorf("check table %s: %v", abstract_sql.DEFAULT_TABLE, err)
|
||||
}
|
||||
if !found {
|
||||
return fmt.Errorf("table %s has no path column: postgres_flat needs a database or schema of its own, apart from the postgres and postgres2 stores", abstract_sql.DEFAULT_TABLE)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package postgres_flat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer/postgres"
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer/store_test"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util/pgxutil"
|
||||
)
|
||||
|
||||
func newTestStore(t *testing.T, db pgxutil.DSNOptions) *PostgresFlatStore {
|
||||
store := &PostgresFlatStore{}
|
||||
if err := store.initialize("", "", true, postgres.Connection{DSNOptions: db, MaxIdle: 10, MaxLifetimeSeconds: 60}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(store.Shutdown)
|
||||
return store
|
||||
}
|
||||
|
||||
func TestStoreConformance(t *testing.T) {
|
||||
store := newTestStore(t, store_test.NewPostgresTestDatabase(t))
|
||||
defer abstract_sql.SetScanPageSizeForTest(abstract_sql.SetScanPageSizeForTest(3))
|
||||
store_test.TestFilerStoreConformance(t, store)
|
||||
}
|
||||
|
||||
func TestRowsAreKeyedByObjectKey(t *testing.T) {
|
||||
store := newTestStore(t, store_test.NewPostgresTestDatabase(t))
|
||||
ctx := context.Background()
|
||||
store.OnBucketCreation("photos")
|
||||
for _, p := range []string{"/buckets/photos/2026/01/a.jpg", "/topics/t.log"} {
|
||||
if err := store.InsertEntry(ctx, &filer.Entry{FullPath: util.FullPath(p)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := store.KvPut(ctx, []byte("hardlink-id"), []byte("record")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for table, want := range map[string]string{"photos": "/2026/01/a.jpg", abstract_sql.DEFAULT_TABLE: "/topics/t.log"} {
|
||||
var path, directory, name string
|
||||
if err := store.DB.QueryRow(`SELECT path, directory, name FROM "`+table+`"`).Scan(&path, &directory, &name); err != nil {
|
||||
t.Fatalf("%s: %v", table, err)
|
||||
}
|
||||
if path != want || directory+"/"+name != want {
|
||||
t.Errorf("%s row is %q (%q, %q), want %q", table, path, directory, name, want)
|
||||
}
|
||||
}
|
||||
var kvRows int
|
||||
if err := store.DB.QueryRow(`SELECT count(*) FROM "` + kvTable + `"`).Scan(&kvRows); err != nil || kvRows != 1 {
|
||||
t.Fatalf("%s holds %d rows, %v", kvTable, kvRows, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefusesAnotherLayout(t *testing.T) {
|
||||
db := store_test.NewPostgresTestDatabase(t)
|
||||
old := &abstract_sql.AbstractSqlStore{}
|
||||
if err := (postgres.Connection{DSNOptions: db}).Open(old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer old.Shutdown()
|
||||
if _, err := old.DB.Exec(strings.ReplaceAll(postgres.DefaultCreateTableQuery, "%s", abstract_sql.DEFAULT_TABLE)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := &PostgresFlatStore{}
|
||||
err := store.initialize("", "", true, postgres.Connection{DSNOptions: db})
|
||||
if err == nil || !strings.Contains(err.Error(), "no path column") {
|
||||
t.Fatalf("initialize over a postgres2 table: %v", err)
|
||||
}
|
||||
store.Shutdown()
|
||||
}
|
||||
Reference in new issue
Block a user