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:
kortschak 2014-08-08 08:36:49 +09:30
parent 7cd740aa7b
commit 2d4c07b56d
7 changed files with 36 additions and 21 deletions

View file

@ -134,6 +134,6 @@ func main() {
flag.Usage() flag.Usage()
} }
if err != nil { if err != nil {
glog.Fatalln(err) glog.Errorln(err)
} }
} }

View file

@ -15,11 +15,20 @@
package db package db
import ( import (
"errors"
"fmt"
"github.com/google/cayley/config" "github.com/google/cayley/config"
"github.com/google/cayley/graph" "github.com/google/cayley/graph"
) )
var ErrNotPersistent = errors.New("database type is not persistent")
func Init(cfg *config.Config, triplePath string) error { 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) err := graph.InitTripleStore(cfg.DatabaseType, cfg.DatabasePath, cfg.DatabaseOptions)
if err != nil { if err != nil {
return err return err

View file

@ -28,8 +28,7 @@ func Open(cfg *config.Config) (graph.TripleStore, error) {
return nil, err return nil, err
} }
// Memstore is not persistent, so it MUST be loaded. if !graph.IsPersistent(cfg.DatabaseType) {
if cfg.DatabaseType == "memstore" {
err = Load(ts, cfg, cfg.DatabasePath) err = Load(ts, cfg, cfg.DatabasePath)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -34,7 +34,7 @@ import (
) )
func init() { func init() {
graph.RegisterTripleStore("leveldb", newTripleStore, createNewLevelDB) graph.RegisterTripleStore("leveldb", true, newTripleStore, createNewLevelDB)
} }
const ( const (

View file

@ -26,7 +26,7 @@ import (
) )
func init() { 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 return newTripleStore(), nil
}, nil) }, nil)
} }

View file

@ -30,7 +30,7 @@ import (
) )
func init() { func init() {
graph.RegisterTripleStore("mongo", newTripleStore, createNewMongoGraph) graph.RegisterTripleStore("mongo", true, newTripleStore, createNewMongoGraph)
} }
// Guarantee we satisfy graph.Bulkloader. // Guarantee we satisfy graph.Bulkloader.

View file

@ -136,38 +136,45 @@ type BulkLoader interface {
type NewStoreFunc func(string, Options) (TripleStore, error) type NewStoreFunc func(string, Options) (TripleStore, error)
type InitStoreFunc func(string, Options) error type InitStoreFunc func(string, Options) error
var storeRegistry = make(map[string]NewStoreFunc) type register struct {
var storeInitRegistry = make(map[string]InitStoreFunc) 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 { if _, found := storeRegistry[name]; found {
panic("already registered TripleStore " + name) panic("already registered TripleStore " + name)
} }
storeRegistry[name] = newFunc storeRegistry[name] = register{
if initFunc != nil { newFunc: newFunc,
storeInitRegistry[name] = initFunc initFunc: initFunc,
isPersistent: persists,
} }
} }
func NewTripleStore(name, dbpath string, opts Options) (TripleStore, error) { func NewTripleStore(name, dbpath string, opts Options) (TripleStore, error) {
newFunc, hasNew := storeRegistry[name] r, registered := storeRegistry[name]
if !hasNew { if !registered {
return nil, errors.New("triplestore: name '" + name + "' is not 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 { func InitTripleStore(name, dbpath string, opts Options) error {
initFunc, hasInit := storeInitRegistry[name] r, registered := storeRegistry[name]
if hasInit { if registered {
return initFunc(dbpath, opts) return r.initFunc(dbpath, opts)
}
if _, isRegistered := storeRegistry[name]; isRegistered {
return nil
} }
return errors.New("triplestore: name '" + name + "' is not registered") return errors.New("triplestore: name '" + name + "' is not registered")
} }
func IsPersistent(name string) bool {
return storeRegistry[name].isPersistent
}
func TripleStores() []string { func TripleStores() []string {
t := make([]string, 0, len(storeRegistry)) t := make([]string, 0, len(storeRegistry))
for n := range storeRegistry { for n := range storeRegistry {