Skip to content

Commit

Permalink
update names per discussion at cayleygraph#38
Browse files Browse the repository at this point in the history
  • Loading branch information
pbnjay committed Jul 18, 2014
1 parent d808d93 commit f9c60a5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 30 deletions.
19 changes: 9 additions & 10 deletions db/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

This comment has been minimized.

Copy link
@kortschak

kortschak Jul 18, 2014

Follow this with a return and delete the == nil conditional.

This comment has been minimized.

Copy link
@pbnjay

pbnjay Jul 18, 2014

Author Owner

It needs to fallback to standard loading if err == ErrCannotBulkLoad

This comment has been minimized.

Copy link
@kortschak

kortschak Jul 18, 2014

I should write comments at 3am. Sorry.

}

LoadTriplesInto(tChan, ts, cfg.LoadSize)
Expand Down
4 changes: 1 addition & 3 deletions graph/leveldb/triplestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion graph/memstore/triplestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,5 @@ func (ts *TripleStore) Close() {}
func init() {
graph.RegisterTripleStore("memstore", func(string, graph.Options) (graph.TripleStore, error) {
return newTripleStore(), nil
})
}, nil)
}
4 changes: 1 addition & 3 deletions graph/mongo/triplestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
38 changes: 25 additions & 13 deletions graph/triplestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

This comment has been minimized.

Copy link
@kortschak

kortschak Jul 18, 2014

s/cannot bulk load/triplestore: cannot bulk load/

This comment has been minimized.

Copy link
@kortschak

kortschak Jul 18, 2014

Oh, and use errors.NewError instead of fmt.Errorf.


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)
}
Expand Down

0 comments on commit f9c60a5

Please sign in to comment.