From 93b3adba9864dd278a778ccfe2165bcadb010574 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 19 Jan 2021 15:55:51 -0800 Subject: [PATCH] fix bucket creation --- weed/filer/abstract_sql/abstract_sql_store.go | 33 ++++++++++++++----- weed/filer/mysql/mysql_store.go | 16 ++++++++- weed/filer/postgres/postgres_store.go | 16 ++++++++- 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/weed/filer/abstract_sql/abstract_sql_store.go b/weed/filer/abstract_sql/abstract_sql_store.go index 878386cf7..aada9cd4a 100644 --- a/weed/filer/abstract_sql/abstract_sql_store.go +++ b/weed/filer/abstract_sql/abstract_sql_store.go @@ -20,13 +20,16 @@ type SqlGenerator interface { GetSqlDeleteFolderChildren(bucket string) string GetSqlListExclusive(bucket string) string GetSqlListInclusive(bucket string) string + GetSqlCreateTable(bucket string) string + GetSqlDropTable(bucket string) string } type AbstractSqlStore struct { - DB *sql.DB SqlGenerator - dbs map[string]bool - dbsLock sync.Mutex + DB *sql.DB + SupportBucketTable bool + dbs map[string]bool + dbsLock sync.Mutex } const ( @@ -74,6 +77,10 @@ func (store *AbstractSqlStore) getTxOrDB(ctx context.Context, fullpath util.Full txOrDB = store.DB } + if !store.SupportBucketTable { + return + } + if !strings.HasPrefix(string(fullpath), "/buckets/") { return } @@ -98,7 +105,7 @@ func (store *AbstractSqlStore) getTxOrDB(ctx context.Context, fullpath util.Full } if _, found := store.dbs[bucket]; !found { - if err = store.createTable(bucket); err != nil { + if err = store.createTable(ctx, bucket); err != nil { store.dbs[bucket] = true } } @@ -234,7 +241,7 @@ func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpat } if isValidBucket(bucket) && shortPath == "/" { - if store.deleteTable(bucket) { + if err = store.deleteTable(ctx, bucket); err != nil { store.dbsLock.Lock() delete(store.dbs, bucket) store.dbsLock.Unlock() @@ -311,10 +318,18 @@ func isValidBucket(bucket string) bool { return bucket != DEFAULT_TABLE && bucket != "" } -func (store *AbstractSqlStore) createTable(bucket string) error { - return nil +func (store *AbstractSqlStore) createTable(ctx context.Context, bucket string) error { + if !store.SupportBucketTable { + return nil + } + _, err := store.DB.ExecContext(ctx, store.SqlGenerator.GetSqlCreateTable(bucket)) + return err } -func (store *AbstractSqlStore) deleteTable(bucket string) bool { - return false +func (store *AbstractSqlStore) deleteTable(ctx context.Context, bucket string) error { + if !store.SupportBucketTable { + return nil + } + _, err := store.DB.ExecContext(ctx, store.SqlGenerator.GetSqlDropTable(bucket)) + return err } diff --git a/weed/filer/mysql/mysql_store.go b/weed/filer/mysql/mysql_store.go index 479d2eed1..0e29852f5 100644 --- a/weed/filer/mysql/mysql_store.go +++ b/weed/filer/mysql/mysql_store.go @@ -16,6 +16,8 @@ const ( ) type SqlGenMysql struct { + createTableSqlTemplate string + dropTableSqlTemplate string } var ( @@ -50,6 +52,14 @@ func (gen *SqlGenMysql) GetSqlListInclusive(bucket string) string { return "SELECT NAME, meta FROM filemeta WHERE dirhash=? AND name>=? AND directory=? AND name like ? ORDER BY NAME ASC LIMIT ?" } +func (gen *SqlGenMysql) GetSqlCreateTable(bucket string) string { + return fmt.Sprintf(gen.createTableSqlTemplate, bucket) +} + +func (gen *SqlGenMysql) GetSqlDropTable(bucket string) string { + return fmt.Sprintf(gen.dropTableSqlTemplate, bucket) +} + func init() { filer.Stores = append(filer.Stores, &MysqlStore{}) } @@ -79,7 +89,11 @@ func (store *MysqlStore) Initialize(configuration util.Configuration, prefix str func (store *MysqlStore) initialize(user, password, hostname string, port int, database string, maxIdle, maxOpen, maxLifetimeSeconds int, interpolateParams bool) (err error) { - store.SqlGenerator = &SqlGenMysql{} + store.SupportBucketTable = false + store.SqlGenerator = &SqlGenMysql{ + createTableSqlTemplate: "", + dropTableSqlTemplate: "drop table %s", + } sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, user, password, hostname, port, database) if interpolateParams { diff --git a/weed/filer/postgres/postgres_store.go b/weed/filer/postgres/postgres_store.go index 783f27c10..8c36b8672 100644 --- a/weed/filer/postgres/postgres_store.go +++ b/weed/filer/postgres/postgres_store.go @@ -15,6 +15,8 @@ const ( ) type SqlGenPostgres struct { + createTableSqlTemplate string + dropTableSqlTemplate string } var ( @@ -49,6 +51,14 @@ func (gen *SqlGenPostgres) GetSqlListInclusive(bucket string) string { return "SELECT NAME, meta FROM filemeta WHERE dirhash=$1 AND name>=$2 AND directory=$3 AND name like $4 ORDER BY NAME ASC LIMIT $5" } +func (gen *SqlGenPostgres) GetSqlCreateTable(bucket string) string { + return fmt.Sprintf(gen.createTableSqlTemplate, bucket) +} + +func (gen *SqlGenPostgres) GetSqlDropTable(bucket string) string { + return fmt.Sprintf(gen.dropTableSqlTemplate, bucket) +} + func init() { filer.Stores = append(filer.Stores, &PostgresStore{}) } @@ -76,7 +86,11 @@ func (store *PostgresStore) Initialize(configuration util.Configuration, prefix func (store *PostgresStore) initialize(user, password, hostname string, port int, database, sslmode string, maxIdle, maxOpen int) (err error) { - store.SqlGenerator = &SqlGenPostgres{} + store.SupportBucketTable = false + store.SqlGenerator = &SqlGenPostgres{ + createTableSqlTemplate: "", + dropTableSqlTemplate: "drop table %s", + } sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode) if user != "" {