func configFrom(file string) *config.Config { // Find the file... if file != "" { if _, err := os.Stat(file); os.IsNotExist(err) { glog.Fatalln("Cannot find specified configuration file", file, ", aborting.") } } else if _, err := os.Stat(os.Getenv("CAYLEY_CFG")); err == nil { file = os.Getenv("CAYLEY_CFG") } else if _, err := os.Stat("/etc/cayley.cfg"); err == nil { file = "/etc/cayley.cfg" } if file == "" { glog.Infoln("Couldn't find a config file in either $CAYLEY_CFG or /etc/cayley.cfg. Going by flag defaults only.") } cfg, err := config.Load(file) if err != nil { glog.Fatalln(err) } if cfg.DatabasePath == "" { cfg.DatabasePath = *databasePath } if cfg.DatabaseType == "" { cfg.DatabaseType = *databaseBackend } return cfg }
func init() { glog.SetToStderr(true) cfg, err := configFrom("cayley_appengine.cfg") if err != nil { glog.Fatalln("Error loading config:", err) } handle, err := db.Open(cfg) if err != nil { glog.Fatalln("Error opening database:", err) } http.SetupRoutes(handle, cfg) }
func ReadTriplesFromFile(c chan *graph.Triple, tripleFile string) { f, err := os.Open(tripleFile) if err != nil { glog.Fatalln("Couldn't open file", tripleFile) } defer func() { if err := f.Close(); err != nil { glog.Fatalln(err) } }() nquads.ReadNQuadsFromReader(c, f) }
func ReadNQuadsFromReader(c chan *graph.Triple, reader io.Reader) { bf := bufio.NewReader(reader) nTriples := 0 line := "" for { l, pre, err := bf.ReadLine() if err == io.EOF { break } if err != nil { glog.Fatalln("Something bad happened while reading file " + err.Error()) } line += string(l) if pre { continue } triple := Parse(line) line = "" if triple != nil { nTriples++ c <- triple } } glog.Infoln("Read", nTriples, "triples") close(c) }
func ParseConfigFromFile(filename string) *CayleyConfig { config := &CayleyConfig{} if filename == "" { return config } f, err := os.Open(filename) if err != nil { glog.Fatalln("Couldn't open config file", filename) } defer f.Close() dec := json.NewDecoder(f) err = dec.Decode(config) if err != nil { glog.Fatalln("Couldn't read config file:", err) } return config }
func findAssetsPath() string { if *assetsPath != "" { if hasAssets(*assetsPath) { return *assetsPath } glog.Fatalln("Cannot find assets at", *assetsPath, ".") } if hasAssets(".") { return "." } gopathPath := os.ExpandEnv("$GOPATH/src/github.com/google/cayley") if hasAssets(gopathPath) { return gopathPath } glog.Fatalln("Cannot find assets in any of the default search paths. Please run in the same directory, in a Go workspace, or set --assets .") panic("cannot reach") }
func ParseConfigFromFlagsAndFile(fileFlag string) *Config { // Find the file... var trueFilename string if fileFlag != "" { if _, err := os.Stat(fileFlag); os.IsNotExist(err) { glog.Fatalln("Cannot find specified configuration file", fileFlag, ", aborting.") } else { trueFilename = fileFlag } } else { if _, err := os.Stat(os.Getenv("CAYLEY_CFG")); err == nil { trueFilename = os.Getenv("CAYLEY_CFG") } else { if _, err := os.Stat("/etc/cayley.cfg"); err == nil { trueFilename = "/etc/cayley.cfg" } } } if trueFilename == "" { glog.Infoln("Couldn't find a config file in either $CAYLEY_CFG or /etc/cayley.cfg. Going by flag defaults only.") } config := ParseConfigFromFile(trueFilename) if config.DatabasePath == "" { config.DatabasePath = *databasePath } if config.DatabaseType == "" { config.DatabaseType = *databaseBackend } if config.ReplicationType == "" { config.ReplicationType = *replicationBackend } if config.ListenHost == "" { config.ListenHost = *host } if config.ListenPort == "" { config.ListenPort = *port } if config.Timeout == 0 { config.Timeout = *timeout } if config.LoadSize == 0 { config.LoadSize = *loadSize } config.ReadOnly = config.ReadOnly || *readOnly return config }
func (d OptionsDict) GetIntKey(key string) (int, bool) { if val, ok := d[key]; ok { switch vv := val.(type) { case float64: return int(vv), true default: glog.Fatalln("Invalid", key, "parameter type from config.") } } return 0, false }
func (d OptionsDict) GetStringKey(key string) (string, bool) { if val, ok := d[key]; ok { switch vv := val.(type) { case string: return vv, true default: glog.Fatalln("Invalid", key, "parameter type from config.") } } return "", false }
func (d Options) BoolKey(key string) (bool, bool) { if val, ok := d[key]; ok { switch vv := val.(type) { case bool: return vv, true default: glog.Fatalln("Invalid", key, "parameter type from config.") } } return false, false }
func (api *API) ServeV1WriteNQuad(w http.ResponseWriter, r *http.Request, params httprouter.Params) int { if api.config.ReadOnly { return jsonResponse(w, 400, "Database is read-only.") } formFile, _, err := r.FormFile("NQuadFile") if err != nil { glog.Errorln(err) return jsonResponse(w, 500, "Couldn't read file: "+err.Error()) } defer formFile.Close() blockSize, blockErr := strconv.ParseInt(r.URL.Query().Get("block_size"), 10, 64) if blockErr != nil { blockSize = int64(api.config.LoadSize) } quadReader, err := internal.Decompressor(formFile) // TODO(kortschak) Make this configurable from the web UI. dec := cquads.NewDecoder(quadReader) h, err := api.GetHandleForRequest(r) if err != nil { return jsonResponse(w, 400, err) } var ( n int block = make([]quad.Quad, 0, blockSize) ) for { t, err := dec.Unmarshal() if err != nil { if err == io.EOF { break } glog.Fatalln("what can do this here?", err) // FIXME(kortschak) } block = append(block, t) n++ if len(block) == cap(block) { h.QuadWriter.AddQuadSet(block) block = block[:0] } } h.QuadWriter.AddQuadSet(block) fmt.Fprintf(w, "{\"result\": \"Successfully wrote %d quads.\"}", n) return 200 }
func Load(ts graph.TripleStore, cfg *config.Config, path string) error { f, err := os.Open(path) if err != nil { return fmt.Errorf("could not open file %q: %v", path, err) } defer f.Close() r, err := decompressor(f) if err != nil { glog.Fatalln(err) } dec := cquads.NewDecoder(r) bulker, canBulk := ts.(graph.BulkLoader) if canBulk { err = bulker.BulkLoad(dec) if err == nil { return nil } if err == graph.ErrCannotBulkLoad { err = nil } } if err != nil { return err } block := make([]*quad.Quad, 0, cfg.LoadSize) for { t, err := dec.Unmarshal() if err != nil { if err == io.EOF { break } return err } block = append(block, t) if len(block) == cap(block) { ts.AddTripleSet(block) block = block[:0] } } ts.AddTripleSet(block) return nil }
func main() { // No command? It's time for usage. if len(os.Args) == 1 { Usage() os.Exit(1) } cmd := os.Args[1] var newargs []string newargs = append(newargs, os.Args[0]) newargs = append(newargs, os.Args[2:]...) os.Args = newargs flag.Parse() var buildString string if VERSION != "" { buildString = fmt.Sprint("Cayley ", VERSION, " built ", BUILD_DATE) glog.Infoln(buildString) } cfg := config.ParseConfigFromFlagsAndFile(*configFile) if os.Getenv("GOMAXPROCS") == "" { runtime.GOMAXPROCS(runtime.NumCPU()) glog.Infoln("Setting GOMAXPROCS to", runtime.NumCPU()) } else { glog.Infoln("GOMAXPROCS currently", os.Getenv("GOMAXPROCS"), " -- not adjusting") } var ( ts graph.TripleStore err error ) switch cmd { case "version": if VERSION != "" { fmt.Println(buildString) } else { fmt.Println("Cayley snapshot") } os.Exit(0) case "init": err = db.Init(cfg, *tripleFile) case "load": ts, err = db.Open(cfg) if err != nil { break } err = db.Load(ts, cfg, *tripleFile) if err != nil { break } ts.Close() case "repl": ts, err = db.Open(cfg) if err != nil { break } err = db.Repl(ts, *queryLanguage, cfg) if err != nil { break } ts.Close() case "http": ts, err = db.Open(cfg) if err != nil { break } http.Serve(ts, cfg) ts.Close() default: fmt.Println("No command", cmd) flag.Usage() } if err != nil { glog.Fatalln(err) } }
func (it *AllIterator) Result() graph.Value { if it.result == nil { glog.Fatalln("result was nil", it) } return it.result }
func main() { // No command? It's time for usage. if len(os.Args) == 1 { Usage() os.Exit(1) } cmd := os.Args[1] newargs := make([]string, 0) newargs = append(newargs, os.Args[0]) newargs = append(newargs, os.Args[2:]...) os.Args = newargs flag.Parse() cfg := config.ParseConfigFromFlagsAndFile(*configFile) if os.Getenv("GOMAXPROCS") == "" { runtime.GOMAXPROCS(runtime.NumCPU()) glog.Infoln("Setting GOMAXPROCS to", runtime.NumCPU()) } else { glog.Infoln("GOMAXPROCS currently", os.Getenv("GOMAXPROCS"), " -- not adjusting") } var ( ts graph.TripleStore err error ) switch cmd { case "init": err = db.Init(cfg, *tripleFile) case "load": ts, err = db.Open(cfg) if err != nil { break } err = db.Load(ts, cfg, *tripleFile) if err != nil { break } ts.Close() case "repl": ts, err = db.Open(cfg) if err != nil { break } err = db.Repl(ts, *queryLanguage, cfg) if err != nil { break } ts.Close() case "http": ts, err = db.Open(cfg) if err != nil { break } http.Serve(ts, cfg) ts.Close() default: fmt.Println("No command", cmd) flag.Usage() } if err != nil { glog.Fatalln(err) } }