diff --git a/db/load.go b/db/load.go index d4d2bd40e..d81ee35da 100644 --- a/db/load.go +++ b/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) diff --git a/graph/leveldb/triplestore.go b/graph/leveldb/triplestore.go index 798895a6d..75c995c4f 100644 --- a/graph/leveldb/triplestore.go +++ b/graph/leveldb/triplestore.go @@ -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) } diff --git a/graph/memstore/triplestore.go b/graph/memstore/triplestore.go index 29fe94cea..5b3413e6c 100644 --- a/graph/memstore/triplestore.go +++ b/graph/memstore/triplestore.go @@ -272,5 +272,5 @@ func (ts *TripleStore) Close() {} func init() { graph.RegisterTripleStore("memstore", func(string, graph.Options) (graph.TripleStore, error) { return newTripleStore(), nil - }) + }, nil) } diff --git a/graph/mongo/triplestore.go b/graph/mongo/triplestore.go index 7ff220f17..81b1ceb4a 100644 --- a/graph/mongo/triplestore.go +++ b/graph/mongo/triplestore.go @@ -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) } diff --git a/graph/triplestore.go b/graph/triplestore.go index f8ddef947..9b73735f1 100644 --- a/graph/triplestore.go +++ b/graph/triplestore.go @@ -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 +} + +type NewStoreFunc func(string, Options) (TripleStore, error) +type InitStoreFunc func(string, Options) error -func RegisterTripleStore(name string, getter TripleStoreGetter, initer ...TripleStoreInit) { +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) }