move to registry interface for backends

This commit is contained in:
Jeremy Jay 2014-07-16 16:49:55 -04:00
parent e780c1ceb9
commit d808d9347c
9 changed files with 117 additions and 64 deletions

View file

@ -48,12 +48,12 @@ type TripleStore struct {
readopts *opt.ReadOptions
}
func CreateNewLevelDB(path string) bool {
func createNewLevelDB(path string, _ graph.Options) error {
opts := &opt.Options{}
db, err := leveldb.OpenFile(path, opts)
if err != nil {
glog.Errorln("Error: couldn't create database: ", err)
return false
return err
}
defer db.Close()
ts := &TripleStore{}
@ -62,10 +62,10 @@ func CreateNewLevelDB(path string) bool {
Sync: true,
}
ts.Close()
return true
return nil
}
func NewTripleStore(path string, options graph.Options) *TripleStore {
func newTripleStore(path string, options graph.Options) (graph.TripleStore, error) {
var ts TripleStore
ts.path = path
cache_size := DefaultCacheSize
@ -94,7 +94,7 @@ func NewTripleStore(path string, options graph.Options) *TripleStore {
ts.db = db
glog.Infoln(ts.GetStats())
ts.getSize()
return &ts
return &ts, nil
}
func (ts *TripleStore) GetStats() string {
@ -443,3 +443,9 @@ func compareBytes(a, b graph.Value) bool {
func (ts *TripleStore) FixedIterator() graph.FixedIterator {
return iterator.NewFixedIteratorWithCompare(compareBytes)
}
func init() {
graph.RegisterTripleStore("leveldb",
graph.TripleStoreGetter(newTripleStore),
graph.TripleStoreInit(createNewLevelDB))
}

View file

@ -79,7 +79,7 @@ type TripleStore struct {
// vip_index map[string]map[int64]map[string]map[int64]*llrb.Tree
}
func NewTripleStore() *TripleStore {
func newTripleStore() *TripleStore {
var ts TripleStore
ts.idMap = make(map[string]int64)
ts.revIdMap = make(map[int64]string)
@ -268,3 +268,9 @@ func (ts *TripleStore) NodesAllIterator() graph.Iterator {
return NewMemstoreAllIterator(ts)
}
func (ts *TripleStore) Close() {}
func init() {
graph.RegisterTripleStore("memstore", func(string, graph.Options) (graph.TripleStore, error) {
return newTripleStore(), nil
})
}

View file

@ -37,11 +37,10 @@ type TripleStore struct {
idCache *IDLru
}
func CreateNewMongoGraph(addr string, options graph.Options) bool {
func createNewMongoGraph(addr string, options graph.Options) error {
conn, err := mgo.Dial(addr)
if err != nil {
glog.Fatal("Error connecting: ", err)
return false
return err
}
conn.SetSafe(&mgo.Safe{})
dbName := DefaultDBName
@ -63,14 +62,14 @@ func CreateNewMongoGraph(addr string, options graph.Options) bool {
db.C("triples").EnsureIndex(indexOpts)
indexOpts.Key = []string{"Provenance"}
db.C("triples").EnsureIndex(indexOpts)
return true
return nil
}
func NewTripleStore(addr string, options graph.Options) *TripleStore {
func newTripleStore(addr string, options graph.Options) (graph.TripleStore, error) {
var ts TripleStore
conn, err := mgo.Dial(addr)
if err != nil {
glog.Fatal("Error connecting: ", err)
return nil, err
}
conn.SetSafe(&mgo.Safe{})
dbName := DefaultDBName
@ -81,7 +80,7 @@ func NewTripleStore(addr string, options graph.Options) *TripleStore {
ts.session = conn
ts.hasher = sha1.New()
ts.idCache = NewIDLru(1 << 16)
return &ts
return &ts, nil
}
func (ts *TripleStore) getIdForTriple(t *graph.Triple) string {
@ -291,7 +290,11 @@ func (ts *TripleStore) TripleDirection(in graph.Value, d graph.Direction) graph.
return val
}
func (ts *TripleStore) BulkLoad(t_chan chan *graph.Triple) {
func (ts *TripleStore) BulkLoad(t_chan chan *graph.Triple) bool {
if ts.Size() != 0 {
return false
}
ts.session.SetSafe(nil)
for triple := range t_chan {
ts.writeTriple(triple)
@ -334,4 +337,11 @@ func (ts *TripleStore) BulkLoad(t_chan chan *graph.Triple) {
}) }`}, {"args", bson.D{}}}, nil)
ts.session.SetSafe(&mgo.Safe{})
return true
}
func init() {
graph.RegisterTripleStore("mongo",
graph.TripleStoreGetter(newTripleStore),
graph.TripleStoreInit(createNewMongoGraph))
}

View file

@ -22,6 +22,7 @@ package graph
// triple backing store we prefer.
import (
"fmt"
"github.com/barakmich/glog"
)
@ -117,3 +118,42 @@ func (d Options) StringKey(key string) (string, bool) {
}
return "", false
}
type TripleStoreGetter func(string, Options) (TripleStore, error)
type TripleStoreInit func(string, Options) error
var storeRegistry = make(map[string]TripleStoreGetter)
var storeInitRegistry = make(map[string]TripleStoreInit)
func RegisterTripleStore(name string, getter TripleStoreGetter, initer ...TripleStoreInit) {
if _, found := storeRegistry[name]; found {
panic("already registered TripleStore " + name)
}
storeRegistry[name] = getter
if len(initer) > 0 {
storeInitRegistry[name] = initer[0]
}
}
func NewTripleStore(name, dbpath string, opts Options) (TripleStore, error) {
getter, hasGetter := storeRegistry[name]
if !hasGetter {
return nil, fmt.Errorf("unknown triplestore '%s'", name)
}
return getter(dbpath, opts)
}
func InitTripleStore(name, dbpath string, opts Options) error {
initer, hasInit := storeInitRegistry[name]
if hasInit {
return initer(dbpath, opts)
}
return fmt.Errorf("unknown triplestore '%s'", name)
}
func TripleStores() (t []string) {
for n := range storeRegistry {
t = append(t, n)
}
return
}