From 1990eba0551b0b018c5b998f7a09dcd694f33b2a Mon Sep 17 00:00:00 2001 From: Andrew Dunham Date: Wed, 15 Apr 2015 14:07:45 -0700 Subject: [PATCH] Stop calling glog.Fatal* in a bunch of places --- graph/bolt/quadstore.go | 5 ++++- graph/iterator/value_comparison_iterator.go | 4 +--- graph/leveldb/quadstore.go | 10 ++++++++-- graph/mongo/quadstore.go | 10 ++++++++-- graph/quadstore.go | 26 +++++++++++++------------- query/mql/build_iterator.go | 3 +-- writer/single.go | 11 +++++++++-- 7 files changed, 44 insertions(+), 25 deletions(-) diff --git a/graph/bolt/quadstore.go b/graph/bolt/quadstore.go index 5b0e2e2..ecc9a81 100644 --- a/graph/bolt/quadstore.go +++ b/graph/bolt/quadstore.go @@ -92,7 +92,10 @@ func newQuadStore(path string, options graph.Options) (graph.QuadStore, error) { } qs.db = db // BoolKey returns false on non-existence. IE, Sync by default. - qs.db.NoSync, _ = options.BoolKey("nosync") + qs.db.NoSync, _, err = options.BoolKey("nosync") + if err != nil { + return nil, err + } err = qs.getMetadata() if err != nil { return nil, err diff --git a/graph/iterator/value_comparison_iterator.go b/graph/iterator/value_comparison_iterator.go index 9197172..bffd640 100644 --- a/graph/iterator/value_comparison_iterator.go +++ b/graph/iterator/value_comparison_iterator.go @@ -27,7 +27,6 @@ package iterator // In MQL terms, this is the [{"age>=": 21}] concept. import ( - "log" "strconv" "github.com/google/cayley/graph" @@ -107,8 +106,7 @@ func RunIntOp(a int64, op Operator, b int64) bool { case compareGTE: return a >= b default: - log.Fatal("Unknown operator type") - return false + panic("Unknown operator type") } } diff --git a/graph/leveldb/quadstore.go b/graph/leveldb/quadstore.go index 799688d..73cf386 100644 --- a/graph/leveldb/quadstore.go +++ b/graph/leveldb/quadstore.go @@ -90,7 +90,10 @@ func newQuadStore(path string, options graph.Options) (graph.QuadStore, error) { var err error qs.path = path cacheSize := DefaultCacheSize - if val, ok := options.IntKey("cache_size_mb"); ok { + val, ok, err := options.IntKey("cache_size_mb") + if err != nil { + return nil, err + } else if ok { cacheSize = val } qs.dbOpts = &opt.Options{ @@ -99,7 +102,10 @@ func newQuadStore(path string, options graph.Options) (graph.QuadStore, error) { qs.dbOpts.ErrorIfMissing = true writeBufferSize := DefaultWriteBufferSize - if val, ok := options.IntKey("writeBufferSize"); ok { + val, ok, err = options.IntKey("writeBufferSize") + if err != nil { + return nil, err + } else if ok { writeBufferSize = val } qs.dbOpts.WriteBuffer = writeBufferSize * opt.MiB diff --git a/graph/mongo/quadstore.go b/graph/mongo/quadstore.go index ce05efe..2a74be9 100644 --- a/graph/mongo/quadstore.go +++ b/graph/mongo/quadstore.go @@ -57,7 +57,10 @@ func createNewMongoGraph(addr string, options graph.Options) error { } conn.SetSafe(&mgo.Safe{}) dbName := DefaultDBName - if val, ok := options.StringKey("database_name"); ok { + val, ok, err := options.StringKey("database_name") + if err != nil { + return err + } else if ok { dbName = val } db := conn.DB(dbName) @@ -94,7 +97,10 @@ func newQuadStore(addr string, options graph.Options) (graph.QuadStore, error) { } conn.SetSafe(&mgo.Safe{}) dbName := DefaultDBName - if val, ok := options.StringKey("database_name"); ok { + val, ok, err := options.StringKey("database_name") + if err != nil { + return nil, err + } else if ok { dbName = val } qs.db = conn.DB(dbName) diff --git a/graph/quadstore.go b/graph/quadstore.go index e9a1a4f..8a89885 100644 --- a/graph/quadstore.go +++ b/graph/quadstore.go @@ -23,8 +23,8 @@ package graph import ( "errors" + "fmt" - "github.com/barakmich/glog" "github.com/google/cayley/quad" ) @@ -103,40 +103,40 @@ type QuadStore interface { type Options map[string]interface{} -func (d Options) IntKey(key string) (int, bool) { +func (d Options) IntKey(key string) (int, bool, error) { if val, ok := d[key]; ok { switch vv := val.(type) { case float64: - return int(vv), true + return int(vv), true, nil default: - glog.Fatalln("Invalid", key, "parameter type from config.") + return 0, false, fmt.Errorf("Invalid %s parameter type from config: %T", key, val) } } - return 0, false + return 0, false, nil } -func (d Options) StringKey(key string) (string, bool) { +func (d Options) StringKey(key string) (string, bool, error) { if val, ok := d[key]; ok { switch vv := val.(type) { case string: - return vv, true + return vv, true, nil default: - glog.Fatalln("Invalid", key, "parameter type from config.") + return "", false, fmt.Errorf("Invalid %s parameter type from config: %T", key, val) } } - return "", false + return "", false, nil } -func (d Options) BoolKey(key string) (bool, bool) { +func (d Options) BoolKey(key string) (bool, bool, error) { if val, ok := d[key]; ok { switch vv := val.(type) { case bool: - return vv, true + return vv, true, nil default: - glog.Fatalln("Invalid", key, "parameter type from config.") + return false, false, fmt.Errorf("Invalid %s parameter type from config: %T", key, val) } } - return false, false + return false, false, nil } var ErrCannotBulkLoad = errors.New("quadstore: cannot bulk load") diff --git a/query/mql/build_iterator.go b/query/mql/build_iterator.go index d1469d7..8d81717 100644 --- a/query/mql/build_iterator.go +++ b/query/mql/build_iterator.go @@ -17,7 +17,6 @@ package mql import ( "errors" "fmt" - "log" "math" "strings" @@ -93,7 +92,7 @@ func (q *Query) buildIteratorTreeInternal(query interface{}, path Path) (it grap it = q.buildResultIterator(path) optional = true default: - log.Fatal("Unknown JSON type?", query) + err = fmt.Errorf("Unknown JSON type: %T") } if err != nil { return nil, false, err diff --git a/writer/single.go b/writer/single.go index 53d46fa..49083c2 100644 --- a/writer/single.go +++ b/writer/single.go @@ -33,17 +33,24 @@ type Single struct { func NewSingleReplication(qs graph.QuadStore, opts graph.Options) (graph.QuadWriter, error) { var ignoreMissing, ignoreDuplicate bool + var err error if *graph.IgnoreMissing { ignoreMissing = true } else { - ignoreMissing, _ = opts.BoolKey("ignore_missing") + ignoreMissing, _, err = opts.BoolKey("ignore_missing") + if err != nil { + return nil, err + } } if *graph.IgnoreDup { ignoreDuplicate = true } else { - ignoreDuplicate, _ = opts.BoolKey("ignore_duplicate") + ignoreDuplicate, _, err = opts.BoolKey("ignore_duplicate") + if err != nil { + return nil, err + } } return &Single{