2022-11-30 18:15:47 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-03-14 12:33:52 +00:00
|
|
|
kv "git.sr.ht/~ashkeel/kilovolt/v12"
|
2022-11-30 18:15:47 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2024-02-25 13:58:35 +00:00
|
|
|
// Driver is a driver wrapping a supported database
|
|
|
|
type Driver interface {
|
2022-11-30 18:15:47 +00:00
|
|
|
Hub() *kv.Hub
|
|
|
|
Close() error
|
|
|
|
Import(map[string]string) error
|
|
|
|
Export(io.Writer) error
|
|
|
|
Restore(io.Reader) error
|
|
|
|
Backup(io.Writer) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type BackupOptions struct {
|
|
|
|
BackupDir string
|
|
|
|
BackupInterval int
|
|
|
|
MaxBackups int
|
|
|
|
}
|
|
|
|
|
|
|
|
const databaseDefaultDriver = "pebble"
|
|
|
|
|
|
|
|
func getDatabaseDriverName(ctx *cli.Context) string {
|
|
|
|
driver := ctx.String("driver")
|
|
|
|
if driver != "auto" {
|
|
|
|
return driver
|
|
|
|
}
|
|
|
|
|
|
|
|
dbDirectory := ctx.String("database-dir")
|
|
|
|
file, err := os.ReadFile(filepath.Join(dbDirectory, "stul-driver"))
|
|
|
|
if err != nil {
|
|
|
|
// No driver file found (or file corrupted), use default driver
|
|
|
|
return databaseDefaultDriver
|
|
|
|
}
|
|
|
|
return string(file)
|
|
|
|
}
|
|
|
|
|
2024-02-25 13:58:35 +00:00
|
|
|
func GetDatabaseDriver(ctx *cli.Context) (Driver, error) {
|
2022-11-30 18:15:47 +00:00
|
|
|
name := getDatabaseDriverName(ctx)
|
|
|
|
dbDirectory := ctx.String("database-dir")
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "badger":
|
|
|
|
return nil, cli.Exit("Badger is not supported anymore as a database driver", 64)
|
|
|
|
case "pebble":
|
2024-03-14 12:33:52 +00:00
|
|
|
db, err := NewPebble(dbDirectory)
|
2023-04-18 13:05:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, cli.Exit(err.Error(), 64)
|
|
|
|
}
|
|
|
|
return db, nil
|
2022-11-30 18:15:47 +00:00
|
|
|
default:
|
|
|
|
return nil, cli.Exit(fmt.Sprintf("Unknown database driver: %s", name), 64)
|
|
|
|
}
|
|
|
|
}
|