Exemplo n.º 1
0
Arquivo: api.go Projeto: the42/ogdat
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)
	}
}
Exemplo n.º 2
0
Arquivo: api.go Projeto: the42/ogdat
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)
	}
}
Exemplo n.º 3
0
func ExampleScanSlice() {
	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", "rating", 4)
	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)
	}

	var albums []struct {
		Title  string
		Rating int
	}
	if err := redis.ScanSlice(values, &albums); err != nil {
		panic(err)
	}
	fmt.Printf("%v\n", albums)
	// Output:
	// [{Earthbound 1} {Beat 4} {Red 5}]
}
Exemplo n.º 4
0
Arquivo: api.go Projeto: the42/ogdat
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)
}
Exemplo n.º 5
0
Arquivo: util.go Projeto: the42/ogdat
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
}
Exemplo n.º 6
0
func TestBlankCommmand(t *testing.T) {
	c, err := redistest.Dial()
	if err != nil {
		t.Fatalf("error connection to database, %v", err)
	}
	defer c.Close()

	for _, cmd := range testCommands {
		if err := c.Send(cmd.args[0].(string), cmd.args[1:]...); err != nil {
			t.Fatalf("Send(%v) returned error %v", cmd.args, err)
		}
	}
	reply, err := redis.Values(c.Do(""))
	if err != nil {
		t.Fatalf("Do() returned error %v", err)
	}
	if len(reply) != len(testCommands) {
		t.Fatalf("len(reply)=%d, want %d", len(reply), len(testCommands))
	}
	for i, cmd := range testCommands {
		actual := reply[i]
		if !reflect.DeepEqual(actual, cmd.expected) {
			t.Errorf("Receive(%v) = %v, want %v", cmd.args, actual, cmd.expected)
		}
	}
}
Exemplo n.º 7
0
Arquivo: api.go Projeto: the42/ogdat
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)
}
Exemplo n.º 8
0
func ExampleArgs() {
	c, err := dial()
	if err != nil {
		panic(err)
	}
	defer c.Close()

	var p1, p2 struct {
		Title  string `redis:"title"`
		Author string `redis:"author"`
		Body   string `redis:"body"`
	}

	p1.Title = "Example"
	p1.Author = "Gary"
	p1.Body = "Hello"

	if _, err := c.Do("HMSET", redis.Args{}.Add("id1").AddFlat(&p1)...); err != nil {
		panic(err)
	}

	m := map[string]string{
		"title":  "Example2",
		"author": "Steve",
		"body":   "Map",
	}

	if _, err := c.Do("HMSET", redis.Args{}.Add("id2").AddFlat(m)...); err != nil {
		panic(err)
	}

	for _, id := range []string{"id1", "id2"} {

		v, err := redis.Values(c.Do("HGETALL", id))
		if err != nil {
			panic(err)
		}

		if err := redis.ScanStruct(v, &p2); err != nil {
			panic(err)
		}

		fmt.Printf("%+v\n", p2)
	}

	// Output:
	// {Title:Example Author:Gary Body:Hello}
	// {Title:Example2 Author:Steve Body:Map}
}
Exemplo n.º 9
0
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
}
Exemplo n.º 10
0
Arquivo: api.go Projeto: the42/ogdat
func (a *analyser) GetTaxonomyDatasets(request *restful.Request, response *restful.Response) {

	taxonomy := request.PathParameter("which")
	subset := request.PathParameter("subset")

	var reply []interface{}
	var err error

	rcon := a.pool.Get()
	defer rcon.Close()

	type internalDataset struct {
		ID, CKANID  string
		Publisher   string
		Contact     string
		Description string
		Version     string
		Category    string
		GeoBBox     string
		GeoToponym  string
	}
	var internalsets []internalDataset
	reply, err = redis.Values(rcon.Do("SORT", datasetskey+":"+taxonomy+":"+subset,
		"BY", "nosort",
		"GET", datasetkey+":*->ID",
		"GET", datasetkey+":*->CKANID",
		"GET", datasetkey+":*->Publisher",
		"GET", datasetkey+":*->Contact",
		"GET", datasetkey+":*->Description",
		"GET", datasetkey+":*->Version",
		"GET", datasetkey+":*->Category",
		"GET", datasetkey+":*->GeoBBox",
		"GET", datasetkey+":*->GeoToponym"))
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}
	if err = redis.ScanSlice(reply, &internalsets); err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}

	var responseset []Dataset
	for _, is := range internalsets {
		ds := Dataset{ID: is.ID,
			CKANID:      is.CKANID,
			Publisher:   is.Publisher,
			Contact:     is.Contact,
			Description: is.Description,
			Version:     is.Version,
			GeoBBox:     is.GeoBBox,
			GeoToponym:  is.GeoToponym}

		var strcats []string
		if len(is.Category) > 0 {
			if err := json.Unmarshal([]byte(is.Category), &strcats); err != nil {
				response.WriteError(http.StatusInternalServerError, err)
				return
			}
		}
		ds.Category = strcats
		responseset = append(responseset, ds)
	}
	response.WriteEntity(responseset)
}
Exemplo n.º 11
0
Arquivo: api.go Projeto: the42/ogdat
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)
}
Exemplo n.º 12
0
// TextExecError tests handling of errors in a transaction. See
// http://redis.io/topics/transactions for information on how Redis handles
// errors in a transaction.
func TestExecError(t *testing.T) {
	c, err := redistest.Dial()
	if err != nil {
		t.Fatalf("error connection to database, %v", err)
	}
	defer c.Close()

	// Execute commands that fail before EXEC is called.

	c.Do("ZADD", "k0", 0, 0)
	c.Send("MULTI")
	c.Send("NOTACOMMAND", "k0", 0, 0)
	c.Send("ZINCRBY", "k0", 0, 0)
	v, err := c.Do("EXEC")
	if err == nil {
		t.Fatalf("EXEC returned values %v, expected error", v)
	}

	// Execute commands that fail after EXEC is called. The first command
	// returns an error.

	c.Do("ZADD", "k1", 0, 0)
	c.Send("MULTI")
	c.Send("HSET", "k1", 0, 0)
	c.Send("ZINCRBY", "k1", 0, 0)
	v, err = c.Do("EXEC")
	if err != nil {
		t.Fatalf("EXEC returned error %v", err)
	}

	vs, err := redis.Values(v, nil)
	if err != nil {
		t.Fatalf("Values(v) returned error %v", err)
	}

	if len(vs) != 2 {
		t.Fatalf("len(vs) == %d, want 2", len(vs))
	}

	if _, ok := vs[0].(error); !ok {
		t.Fatalf("first result is type %T, expected error", vs[0])
	}

	if _, ok := vs[1].([]byte); !ok {
		t.Fatalf("second result is type %T, expected []byte", vs[2])
	}

	// Execute commands that fail after EXEC is called. The second command
	// returns an error.

	c.Do("ZADD", "k2", 0, 0)
	c.Send("MULTI")
	c.Send("ZINCRBY", "k2", 0, 0)
	c.Send("HSET", "k2", 0, 0)
	v, err = c.Do("EXEC")
	if err != nil {
		t.Fatalf("EXEC returned error %v", err)
	}

	vs, err = redis.Values(v, nil)
	if err != nil {
		t.Fatalf("Values(v) returned error %v", err)
	}

	if len(vs) != 2 {
		t.Fatalf("len(vs) == %d, want 2", len(vs))
	}

	if _, ok := vs[0].([]byte); !ok {
		t.Fatalf("first result is type %T, expected []byte", vs[0])
	}

	if _, ok := vs[1].(error); !ok {
		t.Fatalf("second result is type %T, expected error", vs[2])
	}
}
Exemplo n.º 13
0
		ve(redis.Ints(nil, nil)),
		ve([]int(nil), redis.ErrNil),
	},
	{
		"strings([v1, v2])",
		ve(redis.Strings([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
		ve([]string{"v1", "v2"}, nil),
	},
	{
		"strings(nil)",
		ve(redis.Strings(nil, nil)),
		ve([]string(nil), redis.ErrNil),
	},
	{
		"values([v1, v2])",
		ve(redis.Values([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
		ve([]interface{}{[]byte("v1"), []byte("v2")}, nil),
	},
	{
		"values(nil)",
		ve(redis.Values(nil, nil)),
		ve([]interface{}(nil), redis.ErrNil),
	},
	{
		"float64(1.0)",
		ve(redis.Float64([]byte("1.0"), nil)),
		ve(float64(1.0), nil),
	},
	{
		"float64(nil)",
		ve(redis.Float64(nil, nil)),