// Create a new request object. // Values are all set to defaults func NewRequest(uri, method string) *Request { request := &Request{*dynmap.NewDynMap()} request.PutWithDot("strest.params", dynmap.NewDynMap()) request.PutWithDot("strest.v", StrestVersion) request.PutWithDot("strest.uri", uri) request.PutWithDot("strest.method", method) request.PutWithDot("strest.txn.accept", "single") return request }
// updates the revision to now func (this *RouterTable) UpdateRevision() (previous, current int64) { prev := this.Revision this.Revision = time.Now().Unix() //reset the dynmap this.DynMap = dynmap.NewDynMap() return prev, this.Revision }
// fills the passed in dynmap with the values from the yaml map func toDynMap(mp yaml.Map) *dynmap.DynMap { d := dynmap.NewDynMap() for k, v := range mp { d.Put(k, fromNode(v)) } return d }
// Creates a new server config with a default routematcher func NewServerConfig() *ServerConfig { return &ServerConfig{ dynmap.NewDynMap(), NewDefaultRouter(), make([]ControllerFilter, 0), } }
func (this *Services) Load() error { //attempt to load from datadir filename := fmt.Sprintf("%s/%s", this.DataDir, "services.json") bytes, err := ioutil.ReadFile(filename) if err != nil { return err } mp := dynmap.NewDynMap() err = mp.UnmarshalJSON(bytes) if err != nil { return err } for k, _ := range mp.Map { rt, ok := mp.GetDynMap(k) if !ok { continue } table, err := shards.ToRouterTable(rt) if err != nil { log.Println(err) continue } this.services[k] = table } return nil }
func (this *Request) Params() *dynmap.DynMap { m, ok := this.GetDynMap("strest.params") if !ok { this.PutIfAbsentWithDot("strest.params", dynmap.NewDynMap()) m, ok = this.GetDynMap("strest.params") } return m }
// We keep this private scope, so external controllers never use it directly // they should all use request.NewResponse func newResponse() *Response { response := &Response{*dynmap.NewDynMap()} response.SetStatusMessage("OK") response.SetStatusCode(200) response.SetTxnStatus("completed") response.PutWithDot("strest.v", StrestVersion) return response }
func NewRouterTable(service string) *RouterTable { return &RouterTable{ Service: service, DynMap: dynmap.NewDynMap(), Revision: time.Now().Unix(), TotalPartitions: 0, ReplicationFactor: 2, } }
func NewTxn(request *Request, writer Writer, filters []ControllerFilter, serverConfig *ServerConfig) *Txn { return &Txn{ Request: request, Writer: writer, Session: dynmap.NewDynMap(), Filters: filters, ServerConfig: serverConfig, } }
// We keep this private scope, so external controllers never use it directly // they should all use request.NewResponse func newResponse() *Response { response := &Response{ DynMap: *dynmap.NewDynMap(), statusMessage: "OK", statusCode: 200, txnStatus: "completed", } return response }
func (this *Services) Save() error { mp := dynmap.NewDynMap() for k, v := range this.services { mp.Put(k, v.ToDynMap()) } bytes, err := mp.MarshalJSON() if err != nil { this.Logger.Printf("Error marshalling services table -- %s", err) return err } err = ioutil.WriteFile(fmt.Sprintf("%s/%s", this.DataDir, "services.json"), bytes, 0644) return err }
//internal use. //skips the cached version func (this *RouterTable) toDynMap() *dynmap.DynMap { mp := dynmap.NewDynMap() mp.Put("service", this.Service) mp.Put("revision", this.Revision) mp.Put("total_partitions", this.TotalPartitions) mp.Put("replication_factor", this.ReplicationFactor) mp.Put("partition_keys", this.PartitionKeys) entries := make([]*dynmap.DynMap, 0) for _, e := range this.Entries { entries = append(entries, e.ToDynMap()) } mp.Put("entries", entries) this.DynMap = mp return mp }
//loads the stored version func (this *Manager) load() error { bytes, err := ioutil.ReadFile(this.filename()) if err != nil { return err } mp := dynmap.NewDynMap() err = mp.UnmarshalJSON(bytes) if err != nil { return err } table, err := ToRouterTable(mp) if err != nil { return err } this.SetRouterTable(table) return nil }
func (this *HttpClient) ApiCallSync(req *cheshire.Request, timeout time.Duration) (*cheshire.Response, error) { uri := req.Uri() pms, err := req.Params().MarshalURL() if err != nil { return nil, err } reqBody := strings.NewReader("") if req.Method() == "GET" { joiner := "&" //add params to the uri if !strings.Contains(uri, "?") { joiner = "?" } uri = fmt.Sprintf("%s%s%s", uri, joiner, pms) } else { reqBody = strings.NewReader(pms) } url := fmt.Sprintf("http://%s%s", this.Address, uri) //convert to an http.Request request, err := http.NewRequest(req.Method(), url, reqBody) if err != nil { return nil, err } res, err := http.DefaultClient.Do(request) if err != nil { return nil, err } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { return nil, err } //convert to a strest response2 var response = &cheshire.Response{*dynmap.NewDynMap()} err = response.UnmarshalJSON(body) if err != nil { return nil, err } return response, nil }
// Translate to a DynMap of the form: // { // "id" : "localhost:8009" // "address" : "localhost", // "ports" : { // "json" : 8009, // "http" : 8010 // } // "partitions" : [1,2,3,4,5,6,7,8,9] // } func (this *RouterEntry) ToDynMap() *dynmap.DynMap { if this.DynMap != nil && len(this.DynMap.Map) > 0 { return this.DynMap } mp := dynmap.NewDynMap() mp.Put("address", this.Address) if this.JsonPort > 0 { mp.PutWithDot("ports.json", this.JsonPort) } if this.HttpPort > 0 { mp.PutWithDot("ports.http", this.HttpPort) } mp.Put("id", this.Id()) mp.Put("partitions", this.Partitions) mp.Put("last_seen_at", this.LastSeenAt) this.DynMap = mp return mp }
func Flash(txn *Txn, severity, message string) { d := dynmap.NewDynMap() d.Put("severity", severity) d.Put("message", message) txn.Session.AddToSlice("_flash", d) }