2014-07-25 13:58:00 +04:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-08-07 12:49:48 +04:00
|
|
|
"path"
|
2014-07-25 13:58:00 +04:00
|
|
|
|
2020-04-24 09:10:03 +03:00
|
|
|
"github.com/ledisdb/ledisdb/config"
|
|
|
|
"github.com/ledisdb/ledisdb/store/driver"
|
2015-05-04 17:42:28 +03:00
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
_ "github.com/ledisdb/ledisdb/store/goleveldb" // register goleveldb
|
|
|
|
_ "github.com/ledisdb/ledisdb/store/leveldb" // register leveldb
|
|
|
|
_ "github.com/ledisdb/ledisdb/store/rocksdb" // register rocksdb
|
2014-08-15 20:08:01 +04:00
|
|
|
)
|
2014-08-07 12:49:48 +04:00
|
|
|
|
|
|
|
func getStorePath(cfg *config.Config) string {
|
2014-09-18 18:30:33 +04:00
|
|
|
if len(cfg.DBPath) > 0 {
|
|
|
|
return cfg.DBPath
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
return path.Join(cfg.DataDir, fmt.Sprintf("%s_data", cfg.DBName))
|
2014-08-07 12:49:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Open(cfg *config.Config) (*DB, error) {
|
2014-08-15 20:08:01 +04:00
|
|
|
s, err := driver.GetStore(cfg)
|
2014-08-07 12:49:48 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := getStorePath(cfg)
|
|
|
|
|
2014-09-18 17:27:43 +04:00
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
2014-07-25 20:46:03 +04:00
|
|
|
return nil, err
|
2014-07-25 13:58:00 +04:00
|
|
|
}
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
idb, err := s.Open(path, cfg)
|
2014-07-25 13:58:00 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-16 05:35:35 +04:00
|
|
|
db := new(DB)
|
|
|
|
db.db = idb
|
|
|
|
db.name = s.String()
|
|
|
|
db.st = &Stat{}
|
|
|
|
db.cfg = cfg
|
2014-07-25 13:58:00 +04:00
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
func Repair(cfg *config.Config) error {
|
2014-08-15 20:08:01 +04:00
|
|
|
s, err := driver.GetStore(cfg)
|
2014-08-07 12:49:48 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-07-25 13:58:00 +04:00
|
|
|
}
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
path := getStorePath(cfg)
|
2014-07-25 13:58:00 +04:00
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
return s.Repair(path, cfg)
|
2014-07-25 13:58:00 +04:00
|
|
|
}
|