2022-04-21 16:22:38 +08:00
|
|
|
//go:build (linux || darwin || windows) && sqlite
|
2021-05-26 08:36:15 +08:00
|
|
|
// +build linux darwin windows
|
2022-04-21 16:22:38 +08:00
|
|
|
// +build sqlite
|
2021-05-26 08:36:15 +08:00
|
|
|
|
|
|
|
// limited GOOS due to modernc.org/libc/unistd
|
|
|
|
|
2021-05-24 14:58:28 +08:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer/mysql"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2021-05-24 14:58:28 +08:00
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
filer.Stores = append(filer.Stores, &SqliteStore{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type SqliteStore struct {
|
|
|
|
abstract_sql.AbstractSqlStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *SqliteStore) GetName() string {
|
|
|
|
return "sqlite"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *SqliteStore) Initialize(configuration util.Configuration, prefix string) (err error) {
|
|
|
|
dbFile := configuration.GetString(prefix + "dbFile")
|
|
|
|
createTable := `CREATE TABLE IF NOT EXISTS "%s" (
|
|
|
|
dirhash BIGINT,
|
|
|
|
name VARCHAR(1000),
|
|
|
|
directory TEXT,
|
|
|
|
meta BLOB,
|
|
|
|
PRIMARY KEY (dirhash, name)
|
|
|
|
) WITHOUT ROWID;`
|
|
|
|
upsertQuery := `INSERT INTO "%s"(dirhash,name,directory,meta)VALUES(?,?,?,?)
|
|
|
|
ON CONFLICT(dirhash,name) DO UPDATE SET
|
|
|
|
directory=excluded.directory,
|
|
|
|
meta=excluded.meta;
|
|
|
|
`
|
|
|
|
return store.initialize(
|
|
|
|
dbFile,
|
|
|
|
createTable,
|
|
|
|
upsertQuery,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *SqliteStore) initialize(dbFile, createTable, upsertQuery string) (err error) {
|
|
|
|
|
|
|
|
store.SupportBucketTable = true
|
|
|
|
store.SqlGenerator = &mysql.SqlGenMysql{
|
|
|
|
CreateTableSqlTemplate: createTable,
|
|
|
|
DropTableSqlTemplate: "drop table `%s`",
|
|
|
|
UpsertQueryTemplate: upsertQuery,
|
|
|
|
}
|
|
|
|
|
|
|
|
var dbErr error
|
|
|
|
store.DB, dbErr = sql.Open("sqlite", dbFile)
|
|
|
|
if dbErr != nil {
|
2022-10-12 15:03:14 +08:00
|
|
|
if store.DB != nil {
|
|
|
|
store.DB.Close()
|
|
|
|
store.DB = nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("can not connect to %s error:%v", dbFile, dbErr)
|
2021-05-24 14:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = store.DB.Ping(); err != nil {
|
|
|
|
return fmt.Errorf("connect to %s error:%v", dbFile, err)
|
|
|
|
}
|
|
|
|
|
2022-10-12 15:03:27 +08:00
|
|
|
store.DB.SetMaxOpenConns(1)
|
|
|
|
|
2021-05-24 14:58:28 +08:00
|
|
|
if err = store.CreateTable(context.Background(), abstract_sql.DEFAULT_TABLE); err != nil {
|
|
|
|
return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|