Make db name-literal agnostic
Move the persistence characteristic of the store into the graph registry and provide an API hook to get that information. Add error return for init on a non-persistent store. Updates #35.
This commit is contained in:
parent
7cd740aa7b
commit
2d4c07b56d
7 changed files with 36 additions and 21 deletions
|
|
@ -134,6 +134,6 @@ func main() {
|
|||
flag.Usage()
|
||||
}
|
||||
if err != nil {
|
||||
glog.Fatalln(err)
|
||||
glog.Errorln(err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,11 +15,20 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/cayley/config"
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
var ErrNotPersistent = errors.New("database type is not persistent")
|
||||
|
||||
func Init(cfg *config.Config, triplePath string) error {
|
||||
if !graph.IsPersistent(cfg.DatabaseType) {
|
||||
return fmt.Errorf("ignoring unproductive database initialization request: %v", ErrNotPersistent)
|
||||
}
|
||||
|
||||
err := graph.InitTripleStore(cfg.DatabaseType, cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -28,8 +28,7 @@ func Open(cfg *config.Config) (graph.TripleStore, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// Memstore is not persistent, so it MUST be loaded.
|
||||
if cfg.DatabaseType == "memstore" {
|
||||
if !graph.IsPersistent(cfg.DatabaseType) {
|
||||
err = Load(ts, cfg, cfg.DatabasePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
graph.RegisterTripleStore("leveldb", newTripleStore, createNewLevelDB)
|
||||
graph.RegisterTripleStore("leveldb", true, newTripleStore, createNewLevelDB)
|
||||
}
|
||||
|
||||
const (
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
graph.RegisterTripleStore("memstore", func(string, graph.Options) (graph.TripleStore, error) {
|
||||
graph.RegisterTripleStore("memstore", false, func(string, graph.Options) (graph.TripleStore, error) {
|
||||
return newTripleStore(), nil
|
||||
}, nil)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
graph.RegisterTripleStore("mongo", newTripleStore, createNewMongoGraph)
|
||||
graph.RegisterTripleStore("mongo", true, newTripleStore, createNewMongoGraph)
|
||||
}
|
||||
|
||||
// Guarantee we satisfy graph.Bulkloader.
|
||||
|
|
|
|||
|
|
@ -136,38 +136,45 @@ type BulkLoader interface {
|
|||
type NewStoreFunc func(string, Options) (TripleStore, error)
|
||||
type InitStoreFunc func(string, Options) error
|
||||
|
||||
var storeRegistry = make(map[string]NewStoreFunc)
|
||||
var storeInitRegistry = make(map[string]InitStoreFunc)
|
||||
type register struct {
|
||||
newFunc NewStoreFunc
|
||||
initFunc InitStoreFunc
|
||||
isPersistent bool
|
||||
}
|
||||
|
||||
func RegisterTripleStore(name string, newFunc NewStoreFunc, initFunc InitStoreFunc) {
|
||||
var storeRegistry = make(map[string]register)
|
||||
|
||||
func RegisterTripleStore(name string, persists bool, newFunc NewStoreFunc, initFunc InitStoreFunc) {
|
||||
if _, found := storeRegistry[name]; found {
|
||||
panic("already registered TripleStore " + name)
|
||||
}
|
||||
storeRegistry[name] = newFunc
|
||||
if initFunc != nil {
|
||||
storeInitRegistry[name] = initFunc
|
||||
storeRegistry[name] = register{
|
||||
newFunc: newFunc,
|
||||
initFunc: initFunc,
|
||||
isPersistent: persists,
|
||||
}
|
||||
}
|
||||
|
||||
func NewTripleStore(name, dbpath string, opts Options) (TripleStore, error) {
|
||||
newFunc, hasNew := storeRegistry[name]
|
||||
if !hasNew {
|
||||
r, registered := storeRegistry[name]
|
||||
if !registered {
|
||||
return nil, errors.New("triplestore: name '" + name + "' is not registered")
|
||||
}
|
||||
return newFunc(dbpath, opts)
|
||||
return r.newFunc(dbpath, opts)
|
||||
}
|
||||
|
||||
func InitTripleStore(name, dbpath string, opts Options) error {
|
||||
initFunc, hasInit := storeInitRegistry[name]
|
||||
if hasInit {
|
||||
return initFunc(dbpath, opts)
|
||||
}
|
||||
if _, isRegistered := storeRegistry[name]; isRegistered {
|
||||
return nil
|
||||
r, registered := storeRegistry[name]
|
||||
if registered {
|
||||
return r.initFunc(dbpath, opts)
|
||||
}
|
||||
return errors.New("triplestore: name '" + name + "' is not registered")
|
||||
}
|
||||
|
||||
func IsPersistent(name string) bool {
|
||||
return storeRegistry[name].isPersistent
|
||||
}
|
||||
|
||||
func TripleStores() []string {
|
||||
t := make([]string, 0, len(storeRegistry))
|
||||
for n := range storeRegistry {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue