fix bucket creation

This commit is contained in:
Chris Lu 2021-01-19 15:55:51 -08:00
parent fa0c8d5283
commit 93b3adba98
3 changed files with 54 additions and 11 deletions

View file

@ -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
}

View file

@ -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 {

View file

@ -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 != "" {