update names per discussion at google/cayley#38
This commit is contained in:
parent
d808d9347c
commit
f9c60a5f30
5 changed files with 37 additions and 30 deletions
19
db/load.go
19
db/load.go
|
|
@ -24,20 +24,19 @@ import (
|
|||
"github.com/google/cayley/nquads"
|
||||
)
|
||||
|
||||
type bulkLoadable interface {
|
||||
// BulkLoad loads Triples from a channel in bulk to the TripleStore. It
|
||||
// returns false if bulk loading is not possible (i.e. if you cannot load
|
||||
// in bulk to a non-empty database, and the current database is non-empty)
|
||||
BulkLoad(chan *graph.Triple) bool
|
||||
}
|
||||
|
||||
func Load(ts graph.TripleStore, cfg *config.Config, triplePath string) {
|
||||
tChan := make(chan *graph.Triple)
|
||||
go ReadTriplesFromFile(tChan, triplePath)
|
||||
|
||||
bulker, canBulk := ts.(bulkLoadable)
|
||||
if canBulk && bulker.BulkLoad(tChan) {
|
||||
return
|
||||
bulker, canBulk := ts.(graph.BulkLoader)
|
||||
if canBulk {
|
||||
err := bulker.BulkLoad(tChan)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if err != graph.ErrCannotBulkLoad {
|
||||
glog.Errorln("Error attempting to bulk load: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
LoadTriplesInto(tChan, ts, cfg.LoadSize)
|
||||
|
|
|
|||
|
|
@ -445,7 +445,5 @@ func (ts *TripleStore) FixedIterator() graph.FixedIterator {
|
|||
}
|
||||
|
||||
func init() {
|
||||
graph.RegisterTripleStore("leveldb",
|
||||
graph.TripleStoreGetter(newTripleStore),
|
||||
graph.TripleStoreInit(createNewLevelDB))
|
||||
graph.RegisterTripleStore("leveldb", newTripleStore, createNewLevelDB)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -272,5 +272,5 @@ func (ts *TripleStore) Close() {}
|
|||
func init() {
|
||||
graph.RegisterTripleStore("memstore", func(string, graph.Options) (graph.TripleStore, error) {
|
||||
return newTripleStore(), nil
|
||||
})
|
||||
}, nil)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -341,7 +341,5 @@ func (ts *TripleStore) BulkLoad(t_chan chan *graph.Triple) bool {
|
|||
}
|
||||
|
||||
func init() {
|
||||
graph.RegisterTripleStore("mongo",
|
||||
graph.TripleStoreGetter(newTripleStore),
|
||||
graph.TripleStoreInit(createNewMongoGraph))
|
||||
graph.RegisterTripleStore("mongo", newTripleStore, createNewMongoGraph)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -119,34 +119,46 @@ func (d Options) StringKey(key string) (string, bool) {
|
|||
return "", false
|
||||
}
|
||||
|
||||
type TripleStoreGetter func(string, Options) (TripleStore, error)
|
||||
type TripleStoreInit func(string, Options) error
|
||||
var ErrCannotBulkLoad = fmt.Errorf("cannot bulk load")
|
||||
|
||||
var storeRegistry = make(map[string]TripleStoreGetter)
|
||||
var storeInitRegistry = make(map[string]TripleStoreInit)
|
||||
type BulkLoader interface {
|
||||
// BulkLoad loads Triples from a channel in bulk to the TripleStore. It
|
||||
// returns ErrCannotBulkLoad if bulk loading is not possible (i.e. if you
|
||||
// cannot load in bulk to a non-empty database, and the db is non-empty)
|
||||
BulkLoad(chan *Triple) error
|
||||
}
|
||||
|
||||
func RegisterTripleStore(name string, getter TripleStoreGetter, initer ...TripleStoreInit) {
|
||||
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)
|
||||
|
||||
func RegisterTripleStore(name string, newFunc NewStoreFunc, initFunc InitStoreFunc) {
|
||||
if _, found := storeRegistry[name]; found {
|
||||
panic("already registered TripleStore " + name)
|
||||
}
|
||||
storeRegistry[name] = getter
|
||||
if len(initer) > 0 {
|
||||
storeInitRegistry[name] = initer[0]
|
||||
storeRegistry[name] = newFunc
|
||||
if initFunc != nil {
|
||||
storeInitRegistry[name] = initFunc
|
||||
}
|
||||
}
|
||||
|
||||
func NewTripleStore(name, dbpath string, opts Options) (TripleStore, error) {
|
||||
getter, hasGetter := storeRegistry[name]
|
||||
if !hasGetter {
|
||||
newFunc, hasNew := storeRegistry[name]
|
||||
if !hasNew {
|
||||
return nil, fmt.Errorf("unknown triplestore '%s'", name)
|
||||
}
|
||||
return getter(dbpath, opts)
|
||||
return newFunc(dbpath, opts)
|
||||
}
|
||||
|
||||
func InitTripleStore(name, dbpath string, opts Options) error {
|
||||
initer, hasInit := storeInitRegistry[name]
|
||||
initFunc, hasInit := storeInitRegistry[name]
|
||||
if hasInit {
|
||||
return initer(dbpath, opts)
|
||||
return initFunc(dbpath, opts)
|
||||
}
|
||||
if _, isRegistered := storeRegistry[name]; isRegistered {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("unknown triplestore '%s'", name)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue