2014-07-25 13:58:00 +04:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/siddontang/ledisdb/store/driver"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2014-07-25 20:46:03 +04:00
|
|
|
const DefaultStoreName = "goleveldb"
|
2014-07-25 13:58:00 +04:00
|
|
|
|
|
|
|
type Store interface {
|
|
|
|
Open(cfg *Config) (driver.IDB, error)
|
|
|
|
Repair(cfg *Config) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var dbs = map[string]Store{}
|
|
|
|
|
|
|
|
func Register(name string, store Store) {
|
|
|
|
if _, ok := dbs[name]; ok {
|
2014-07-25 20:46:03 +04:00
|
|
|
panic(fmt.Errorf("store %s is registered", name))
|
2014-07-25 13:58:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
dbs[name] = store
|
|
|
|
}
|
|
|
|
|
2014-07-25 20:46:03 +04:00
|
|
|
func ListStores() []string {
|
|
|
|
s := []string{}
|
|
|
|
for k, _ := range dbs {
|
|
|
|
s = append(s, k)
|
2014-07-25 13:58:00 +04:00
|
|
|
}
|
|
|
|
|
2014-07-25 20:46:03 +04:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func Open(cfg *Config) (*DB, error) {
|
2014-07-25 13:58:00 +04:00
|
|
|
if len(cfg.Name) == 0 {
|
|
|
|
cfg.Name = DefaultStoreName
|
|
|
|
}
|
|
|
|
|
|
|
|
s, ok := dbs[cfg.Name]
|
|
|
|
if !ok {
|
2014-07-25 20:46:03 +04:00
|
|
|
return nil, fmt.Errorf("store %s is not registered", cfg.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(cfg.Path, os.ModePerm); err != nil {
|
|
|
|
return nil, err
|
2014-07-25 13:58:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
idb, err := s.Open(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
db := &DB{idb}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Repair(cfg *Config) error {
|
|
|
|
if len(cfg.Name) == 0 {
|
|
|
|
cfg.Name = DefaultStoreName
|
|
|
|
}
|
|
|
|
|
|
|
|
s, ok := dbs[cfg.Name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("db %s is not registered", cfg.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Repair(cfg)
|
|
|
|
}
|