Exemplo n.º 1
0
func fetchChampion(cluster *riak.Cluster) {
	cmd, err := riak.NewFetchValueCommandBuilder().
		WithBucketType("sports").
		WithBucket("nba").
		WithKey("champion").
		Build()

	if err != nil {
		fmt.Println(err.Error())
		return
	}

	if err := cluster.Execute(cmd); err != nil {
		fmt.Println(err.Error())
		return
	}

	fvc := cmd.(*riak.FetchValueCommand)
	rsp := fvc.Response
	var obj *riak.Object
	if len(rsp.Values) > 0 {
		obj = rsp.Values[0]
	} else {
		obj = &riak.Object{
			ContentType:     "text/plain",
			Charset:         "utf-8",
			ContentEncoding: "utf-8",
			Value:           nil,
		}
	}

	obj.Value = []byte("Harlem Globetrotters")

	cmd, err = riak.NewStoreValueCommandBuilder().
		WithBucketType("sports").
		WithBucket("nba").
		WithContent(obj).
		Build()

	if err != nil {
		fmt.Println(err.Error())
		return
	}

	if err := cluster.Execute(cmd); err != nil {
		fmt.Println(err.Error())
		return
	}
}
func (dao *LogIndexRiakDAO) AppendToLogIndex(userId string, entry ent.IndexEntry) error {
	value := riak.Object{}
	value.ContentType = "application/json"
	value.Charset = "utf-8"
	value.ContentEncoding = "utf-8"
	index := ent.TimelineIndex{entry}
	if responce := dao.fetch(userId); responce != nil && !responce.IsNotFound {
		value.VClock = responce.VClock
		if err := json.Unmarshal(responce.Values[0].Value, &index); err != nil {
			return err
		}
	}
	index = ent.SortEntries(append(index, entry))
	encoded, _ := json.Marshal(index)
	value.Value = encoded
	return dao.store(userId, value)
}