mirror of
https://github.com/chrislusf/seaweedfs
synced 2024-07-05 00:26:51 +02:00
fix bucket creation
This commit is contained in:
parent
fa0c8d5283
commit
93b3adba98
|
@ -20,13 +20,16 @@ type SqlGenerator interface {
|
||||||
GetSqlDeleteFolderChildren(bucket string) string
|
GetSqlDeleteFolderChildren(bucket string) string
|
||||||
GetSqlListExclusive(bucket string) string
|
GetSqlListExclusive(bucket string) string
|
||||||
GetSqlListInclusive(bucket string) string
|
GetSqlListInclusive(bucket string) string
|
||||||
|
GetSqlCreateTable(bucket string) string
|
||||||
|
GetSqlDropTable(bucket string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
type AbstractSqlStore struct {
|
type AbstractSqlStore struct {
|
||||||
DB *sql.DB
|
|
||||||
SqlGenerator
|
SqlGenerator
|
||||||
dbs map[string]bool
|
DB *sql.DB
|
||||||
dbsLock sync.Mutex
|
SupportBucketTable bool
|
||||||
|
dbs map[string]bool
|
||||||
|
dbsLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -74,6 +77,10 @@ func (store *AbstractSqlStore) getTxOrDB(ctx context.Context, fullpath util.Full
|
||||||
txOrDB = store.DB
|
txOrDB = store.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !store.SupportBucketTable {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(string(fullpath), "/buckets/") {
|
if !strings.HasPrefix(string(fullpath), "/buckets/") {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -98,7 +105,7 @@ func (store *AbstractSqlStore) getTxOrDB(ctx context.Context, fullpath util.Full
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, found := store.dbs[bucket]; !found {
|
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
|
store.dbs[bucket] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -234,7 +241,7 @@ func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpat
|
||||||
}
|
}
|
||||||
|
|
||||||
if isValidBucket(bucket) && shortPath == "/" {
|
if isValidBucket(bucket) && shortPath == "/" {
|
||||||
if store.deleteTable(bucket) {
|
if err = store.deleteTable(ctx, bucket); err != nil {
|
||||||
store.dbsLock.Lock()
|
store.dbsLock.Lock()
|
||||||
delete(store.dbs, bucket)
|
delete(store.dbs, bucket)
|
||||||
store.dbsLock.Unlock()
|
store.dbsLock.Unlock()
|
||||||
|
@ -311,10 +318,18 @@ func isValidBucket(bucket string) bool {
|
||||||
return bucket != DEFAULT_TABLE && bucket != ""
|
return bucket != DEFAULT_TABLE && bucket != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *AbstractSqlStore) createTable(bucket string) error {
|
func (store *AbstractSqlStore) createTable(ctx context.Context, bucket string) error {
|
||||||
return nil
|
if !store.SupportBucketTable {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
_, err := store.DB.ExecContext(ctx, store.SqlGenerator.GetSqlCreateTable(bucket))
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *AbstractSqlStore) deleteTable(bucket string) bool {
|
func (store *AbstractSqlStore) deleteTable(ctx context.Context, bucket string) error {
|
||||||
return false
|
if !store.SupportBucketTable {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
_, err := store.DB.ExecContext(ctx, store.SqlGenerator.GetSqlDropTable(bucket))
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,8 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type SqlGenMysql struct {
|
type SqlGenMysql struct {
|
||||||
|
createTableSqlTemplate string
|
||||||
|
dropTableSqlTemplate string
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
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 ?"
|
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() {
|
func init() {
|
||||||
filer.Stores = append(filer.Stores, &MysqlStore{})
|
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,
|
func (store *MysqlStore) initialize(user, password, hostname string, port int, database string, maxIdle, maxOpen,
|
||||||
maxLifetimeSeconds int, interpolateParams bool) (err error) {
|
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)
|
sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, user, password, hostname, port, database)
|
||||||
if interpolateParams {
|
if interpolateParams {
|
||||||
|
|
|
@ -15,6 +15,8 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type SqlGenPostgres struct {
|
type SqlGenPostgres struct {
|
||||||
|
createTableSqlTemplate string
|
||||||
|
dropTableSqlTemplate string
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
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"
|
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() {
|
func init() {
|
||||||
filer.Stores = append(filer.Stores, &PostgresStore{})
|
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) {
|
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)
|
sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode)
|
||||||
if user != "" {
|
if user != "" {
|
||||||
|
|
Loading…
Reference in a new issue