/* putting together base engine for GoShare dbpath, httpuri, httpport, rep_port, req_port *string */ func GoShareEngine(config Config) { runtime.GOMAXPROCS(runtime.NumCPU()) // remember it will be same DB instance shared across goshare package db = abkleveldb.CreateDB(*config["dbpath"]) if *config["cpuprofile"] != "" { f, err := os.Create(*config["cpuprofile"]) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) go func() { time.Sleep(100 * time.Second) pprof.StopCPUProfile() }() } _httpport, err_httpport := strconv.Atoi(*config["httpport"]) _req_port, err_req_port := strconv.Atoi(*config["req_port"]) _rep_port, err_rep_port := strconv.Atoi(*config["rep_port"]) if err_httpport == nil && err_rep_port == nil && err_req_port == nil { go GoShareHTTP(*config["httpuri"], _httpport) go GoShareZMQ(_req_port, _rep_port) } else { golerror.Boohoo("Port parameters to bind, error-ed while conversion to number.", true) } }
/* DelKey deletes key from provided DB handle. */ func DelKey(key string, db *levigo.DB) bool { writer := levigo.NewWriteOptions() defer writer.Close() err := db.Delete(writer, []byte(key)) if err != nil { golerror.Boohoo("Key "+key+" query failed.", false) return false } return true }
/* GetVal gets value of Key from provided db handle. */ func GetVal(key string, db *levigo.DB) string { reader := levigo.NewReadOptions() defer reader.Close() data, err := db.Get(reader, []byte(key)) if err != nil { golerror.Boohoo("Key "+key+" query failed.", false) return "" } return string(data) }
/* CreateDB creates a db at provided dbpath. */ func CreateDB(dbpath string) *levigo.DB { opts := levigo.NewOptions() opts.SetCache(levigo.NewLRUCache(1 << 10)) opts.SetCreateIfMissing(true) db, err := levigo.Open(dbpath, opts) if err != nil { errMsg := fmt.Sprintf("DB %s Creation failed. %q", dbpath, err) golerror.Boohoo(errMsg, true) } return db }
func ZmqRead(key_type string, key string) string { rep_port, err_rep_port := strconv.Atoi(*LifeCycleConfig["db_rep_port"]) req_port, err_req_port := strconv.Atoi(*LifeCycleConfig["db_req_port"]) if err_rep_port != nil || err_req_port != nil { golerror.Boohoo("Port parameters to bind, error-ed while conversion to number.", true) } sock := golzmq.ZmqRequestSocket(*LifeCycleConfig["db_uri"], rep_port, req_port) response, _ := golzmq.ZmqRequest(sock, "read", key_type, key) return response }
/* PushKeyVal push KeyVal in provided DB handle. */ func PushKeyVal(key string, val string, db *levigo.DB) bool { writer := levigo.NewWriteOptions() defer writer.Close() keyname := []byte(key) value := []byte(val) err := db.Put(writer, keyname, value) if err != nil { golerror.Boohoo("Key "+key+" insertion failed. It's value was "+val, false) return false } return true }
/* Lifecycle Handler for HTTP Requests */ func LifeCycleHTTP() { httpport_num, err_httpport := strconv.Atoi(*LifeCycleConfig["httpport"]) if err_httpport != nil { golerror.Boohoo("Port parameters to bind, error-ed while conversion to number.", true) } runtime.GOMAXPROCS(runtime.NumCPU()) http.HandleFunc("/", Index) http.HandleFunc("/data", Data) http.HandleFunc("/help", F1) http.HandleFunc("/status", Status) srv := &http.Server{ Addr: fmt.Sprintf("%s:%d", *LifeCycleConfig["httpuri"], httpport_num), Handler: http.DefaultServeMux, ReadTimeout: time.Duration(5) * time.Second, } fmt.Printf("access your lifecycle at http://%s:%d\n", *LifeCycleConfig["httpuri"], httpport_num) err := srv.ListenAndServe() fmt.Println("Game Over:", err) }