func (a *analyser) GetAN003TaxonomyData(taxonomy string) func(request *restful.Request, response *restful.Response) { return func(request *restful.Request, response *restful.Response) { subset := request.PathParameter("subset") var keyreply, datareply []interface{} var err error rcon := a.pool.Get() defer rcon.Close() keyreply, err = redis.Values(rcon.Do("ZRANGE", an003+":"+taxonomy+":"+subset, 0, -1)) if err != nil { response.WriteError(http.StatusInternalServerError, err) return } var key string var responseset []URLCheckRecord // TODO: Nested redis calls. Better to use MULTI here? for len(keyreply) > 0 { if keyreply, err = redis.Scan(keyreply, &key); err != nil { response.WriteError(http.StatusInternalServerError, err) return } datareply, err = redis.Values(rcon.Do("LRANGE", an003+":"+key, 0, -1)) if err != nil { response.WriteError(http.StatusInternalServerError, err) return } var data string for len(datareply) > 0 { if datareply, err = redis.Scan(datareply, &data); err != nil { response.WriteError(http.StatusInternalServerError, err) return } var item URLCheckRecord if err = json.Unmarshal([]byte(data), &item); err != nil { response.WriteError(http.StatusInternalServerError, err) return } responseset = append(responseset, item) } } response.WriteEntity(responseset) } }
func (a *analyser) GetAN003Data(request *restful.Request, response *restful.Response) { id := request.PathParameter("id") var reply []interface{} var err error rcon := a.pool.Get() defer rcon.Close() reply, err = redis.Values(rcon.Do("LRANGE", an003+":"+id, 0, -1)) if err != nil { response.WriteError(http.StatusInternalServerError, err) return } var responseset []URLCheckRecord var s string for len(reply) > 0 { if reply, err = redis.Scan(reply, &s); err != nil { response.WriteError(http.StatusInternalServerError, err) return } var item URLCheckRecord if err := json.Unmarshal([]byte(s), &item); err != nil { response.WriteError(http.StatusInternalServerError, err) return } responseset = append(responseset, item) } response.WriteEntity(responseset) }
func (c RedisConn) DeleteKeyPattern(s ...string) error { for _, pattern := range s { reply, err := redis.Values(c.Do("KEYS", pattern)) if err != nil { return err } var key string if len(reply) > 0 { if err := c.Send("MULTI"); err != nil { return err } for len(reply) > 0 { reply, err = redis.Scan(reply, &key) if err != nil { return err } if err := c.Send("DEL", key); err != nil { return err } } if _, err := c.Do("EXEC"); err != nil { return err } } } return nil }
func TestScanConversionError(t *testing.T) { for _, tt := range scanConversionErrorTests { values := []interface{}{tt.src} dest := reflect.New(reflect.TypeOf(tt.dest)) values, err := redis.Scan(values, dest.Interface()) if err == nil { t.Errorf("Scan(%v) did not return error", tt) } } }
func (a *analyser) GetCheckResult(request *restful.Request, response *restful.Response) { id := request.PathParameter("id") var reply []interface{} var err error rcon := a.pool.Get() defer rcon.Close() reply, err = redis.Values(rcon.Do("HMGET", checkkey+":"+id, "CheckStatus", "CKANID", "Hittime")) if err != nil { response.WriteError(http.StatusInternalServerError, err) return } if len(reply) == 0 { response.WriteError(http.StatusInternalServerError, fmt.Errorf("Record not found")) return } var ( CKANID string Hittime string Status string ) if _, err = redis.Scan(reply, &Status, &CKANID, &Hittime); err != nil { response.WriteError(http.StatusInternalServerError, err) return } var checkStatus []CheckStatus checkrecord := CheckRecord{CKANID: CKANID} if len(Status) > 0 { if err := json.Unmarshal([]byte(Status), &checkStatus); err != nil { response.WriteError(http.StatusInternalServerError, err) return } checkrecord.CheckStatus = checkStatus } if len(Hittime) > 0 { hittime, err := time.Parse(RedigoTimestamp, Hittime) if err != nil { response.WriteError(http.StatusInternalServerError, err) return } checkrecord.Hittime = hittime } response.WriteEntity(checkrecord) }
func (a *analyser) GetSortedSet(key string) func(request *restful.Request, response *restful.Response) { return func(request *restful.Request, response *restful.Response) { getentity := request.QueryParameter("id") sortorder := request.QueryParameter("sortorder") var entity string nums := -1 var reply []interface{} var err error resultset := make([]IDNums, 0) rcon := a.pool.Get() defer rcon.Close() if len(getentity) > 0 { snums, err := redis.String(rcon.Do("ZSCORE", key, getentity)) if err != nil { response.WriteError(http.StatusInternalServerError, err) return } if len(snums) > 0 { if i, err := strconv.ParseInt(snums, 10, 0); err == nil { resultset = append(resultset, IDNums{ID: getentity, Numsets: int(i)}) } } } else { if sortorder == "asc" { reply, err = redis.Values(rcon.Do("ZRANGE", key, 0, -1, "WITHSCORES")) } else { reply, err = redis.Values(rcon.Do("ZREVRANGE", key, 0, -1, "WITHSCORES")) } if err != nil { response.WriteError(http.StatusInternalServerError, err) return } for len(reply) > 0 { reply, err = redis.Scan(reply, &entity, &nums) if err != nil { response.WriteError(http.StatusInternalServerError, err) return } resultset = append(resultset, IDNums{ID: entity, Numsets: nums}) } } response.WriteEntity(resultset) } }
func TestScanConversion(t *testing.T) { for _, tt := range scanConversionTests { values := []interface{}{tt.src} dest := reflect.New(reflect.TypeOf(tt.dest)) values, err := redis.Scan(values, dest.Interface()) if err != nil { t.Errorf("Scan(%v) returned error %v", tt, err) continue } if !reflect.DeepEqual(tt.dest, dest.Elem().Interface()) { t.Errorf("Scan(%v) returned %v, want %v", tt, dest.Elem().Interface(), tt.dest) } } }
func ExampleScan() { c, err := dial() if err != nil { panic(err) } defer c.Close() c.Send("HMSET", "album:1", "title", "Red", "rating", 5) c.Send("HMSET", "album:2", "title", "Earthbound", "rating", 1) c.Send("HMSET", "album:3", "title", "Beat") c.Send("LPUSH", "albums", "1") c.Send("LPUSH", "albums", "2") c.Send("LPUSH", "albums", "3") values, err := redis.Values(c.Do("SORT", "albums", "BY", "album:*->rating", "GET", "album:*->title", "GET", "album:*->rating")) if err != nil { panic(err) } for len(values) > 0 { var title string rating := -1 // initialize to illegal value to detect nil. values, err = redis.Scan(values, &title, &rating) if err != nil { panic(err) } if rating == -1 { fmt.Println(title, "not-rated") } else { fmt.Println(title, rating) } } // Output: // Beat not-rated // Earthbound 1 // Red 5 }
func (a *analyser) GetDataset(request *restful.Request, response *restful.Response) { id := request.PathParameter("id") var reply []interface{} var err error rcon := a.pool.Get() defer rcon.Close() reply, err = redis.Values(rcon.Do("HMGET", datasetkey+":"+id, "ID", "CKANID", "Publisher", "Contact", "Description", "Version", "Category", "GeoBBox", "GeoToponym")) if err != nil { response.WriteError(http.StatusInternalServerError, err) return } if len(reply) == 0 { response.WriteError(http.StatusInternalServerError, fmt.Errorf("Record not found")) return } var ( ID, CKANID string Publisher string Contact string Description string Version string Category string GeoBBox string GeoToponym string ) if _, err = redis.Scan(reply, &ID, &CKANID, &Publisher, &Contact, &Description, &Version, &Category, &GeoBBox, &GeoToponym); err != nil { response.WriteError(http.StatusInternalServerError, err) return } ds := Dataset{ID: ID, CKANID: CKANID, Publisher: Publisher, Contact: Contact, Description: Description, Version: Version, GeoBBox: GeoBBox, GeoToponym: GeoToponym} if len(Category) > 0 { var strcats []string if err := json.Unmarshal([]byte(Category), &strcats); err != nil { response.WriteError(http.StatusInternalServerError, err) return } ds.Category = strcats } response.WriteEntity(ds) }