move to registry interface for backends
This commit is contained in:
parent
e780c1ceb9
commit
d808d9347c
9 changed files with 117 additions and 64 deletions
22
db/init.go
22
db/init.go
|
|
@ -16,25 +16,19 @@ package db
|
|||
|
||||
import (
|
||||
"github.com/google/cayley/config"
|
||||
"github.com/google/cayley/graph/leveldb"
|
||||
"github.com/google/cayley/graph/mongo"
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
func Init(cfg *config.Config, triplePath string) bool {
|
||||
created := false
|
||||
dbpath := cfg.DatabasePath
|
||||
switch cfg.DatabaseType {
|
||||
case "mongo", "mongodb":
|
||||
created = mongo.CreateNewMongoGraph(dbpath, cfg.DatabaseOptions)
|
||||
case "leveldb":
|
||||
created = leveldb.CreateNewLevelDB(dbpath)
|
||||
case "mem":
|
||||
return true
|
||||
err := graph.InitTripleStore(cfg.DatabaseType, cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if created && triplePath != "" {
|
||||
|
||||
if triplePath != "" {
|
||||
ts := Open(cfg)
|
||||
Load(ts, cfg, triplePath, true)
|
||||
Load(ts, cfg, triplePath)
|
||||
ts.Close()
|
||||
}
|
||||
return created
|
||||
return true
|
||||
}
|
||||
|
|
|
|||
36
db/load.go
36
db/load.go
|
|
@ -21,30 +21,26 @@ import (
|
|||
|
||||
"github.com/google/cayley/config"
|
||||
"github.com/google/cayley/graph"
|
||||
"github.com/google/cayley/graph/mongo"
|
||||
"github.com/google/cayley/nquads"
|
||||
)
|
||||
|
||||
func Load(ts graph.TripleStore, cfg *config.Config, triplePath string, firstTime bool) {
|
||||
switch cfg.DatabaseType {
|
||||
case "mongo", "mongodb":
|
||||
if firstTime {
|
||||
loadMongo(ts.(*mongo.TripleStore), triplePath)
|
||||
} else {
|
||||
LoadTriplesFromFileInto(ts, triplePath, cfg.LoadSize)
|
||||
}
|
||||
case "leveldb":
|
||||
LoadTriplesFromFileInto(ts, triplePath, cfg.LoadSize)
|
||||
case "mem":
|
||||
LoadTriplesFromFileInto(ts, triplePath, cfg.LoadSize)
|
||||
}
|
||||
|
||||
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 loadMongo(ts *mongo.TripleStore, path string) {
|
||||
func Load(ts graph.TripleStore, cfg *config.Config, triplePath string) {
|
||||
tChan := make(chan *graph.Triple)
|
||||
go ReadTriplesFromFile(tChan, path)
|
||||
ts.BulkLoad(tChan)
|
||||
go ReadTriplesFromFile(tChan, triplePath)
|
||||
|
||||
bulker, canBulk := ts.(bulkLoadable)
|
||||
if canBulk && bulker.BulkLoad(tChan) {
|
||||
return
|
||||
}
|
||||
|
||||
LoadTriplesInto(tChan, ts, cfg.LoadSize)
|
||||
}
|
||||
|
||||
func ReadTriplesFromFile(c chan *graph.Triple, tripleFile string) {
|
||||
|
|
@ -62,9 +58,7 @@ func ReadTriplesFromFile(c chan *graph.Triple, tripleFile string) {
|
|||
nquads.ReadNQuadsFromReader(c, f)
|
||||
}
|
||||
|
||||
func LoadTriplesFromFileInto(ts graph.TripleStore, filename string, loadSize int) {
|
||||
tChan := make(chan *graph.Triple)
|
||||
go ReadTriplesFromFile(tChan, filename)
|
||||
func LoadTriplesInto(tChan chan *graph.Triple, ts graph.TripleStore, loadSize int) {
|
||||
tripleblock := make([]*graph.Triple, loadSize)
|
||||
i := 0
|
||||
for t := range tChan {
|
||||
|
|
|
|||
24
db/open.go
24
db/open.go
|
|
@ -19,22 +19,20 @@ import (
|
|||
|
||||
"github.com/google/cayley/config"
|
||||
"github.com/google/cayley/graph"
|
||||
"github.com/google/cayley/graph/leveldb"
|
||||
"github.com/google/cayley/graph/memstore"
|
||||
"github.com/google/cayley/graph/mongo"
|
||||
)
|
||||
|
||||
func Open(cfg *config.Config) graph.TripleStore {
|
||||
glog.Infof("Opening database \"%s\" at %s", cfg.DatabaseType, cfg.DatabasePath)
|
||||
switch cfg.DatabaseType {
|
||||
case "mongo", "mongodb":
|
||||
return mongo.NewTripleStore(cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
case "leveldb":
|
||||
return leveldb.NewTripleStore(cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
case "mem":
|
||||
ts := memstore.NewTripleStore()
|
||||
Load(ts, cfg, cfg.DatabasePath, true)
|
||||
return ts
|
||||
ts, err := graph.NewTripleStore(cfg.DatabaseType, cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
if err != nil {
|
||||
glog.Fatalln(err.Error())
|
||||
return nil
|
||||
}
|
||||
panic("Unsupported database backend " + cfg.DatabaseType)
|
||||
|
||||
// memstore is not persistent, so MUST be loaded
|
||||
if cfg.DatabaseType == "memstore" {
|
||||
Load(ts, cfg, cfg.DatabasePath)
|
||||
}
|
||||
|
||||
return ts
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue