예제 #1
0
func storeObjects(cluster *riak.Cluster) error {
	o1 := &riak.Object{
		Key:   "liono",
		Value: []byte("{\"name_s\":\"Lion-o\",\"age_i\":30,\"leader_b\":true}"),
	}
	o2 := &riak.Object{
		Key:   "cheetara",
		Value: []byte("{\"name_s\":\"Cheetara\",\"age_i\":30,\"leader_b\":false}"),
	}
	o3 := &riak.Object{
		Key:   "snarf",
		Value: []byte("{\"name_s\":\"Snarf\",\"age_i\":43,\"leader_b\":false}"),
	}
	o4 := &riak.Object{
		Key:   "panthro",
		Value: []byte("{\"name_s\":\"Panthro\",\"age_i\":36,\"leader_b\":false}"),
	}

	objs := [...]*riak.Object{o1, o2, o3, o4}

	wg := &sync.WaitGroup{}
	for _, obj := range objs {
		obj.ContentType = "application/json"
		obj.Charset = "utf-8"
		obj.ContentEncoding = "utf-8"

		cmd, err := riak.NewStoreValueCommandBuilder().
			WithBucketType("animals").
			WithBucket("cats").
			WithContent(obj).
			Build()
		if err != nil {
			return err
		}

		args := &riak.Async{
			Command: cmd,
			Wait:    wg,
		}
		if err := cluster.ExecuteAsync(args); err != nil {
			return err
		}
	}

	wg.Wait()

	return nil
}
예제 #2
0
func queryingRange(cluster *riak.Cluster) error {
	c1, c1err := riak.NewSecondaryIndexQueryCommandBuilder().
		WithBucketType("indexes").
		WithBucket("people").
		WithIndexName("field1_bin").
		WithRange("val2", "val4").
		Build()
	if c1err != nil {
		return c1err
	}

	c2, c2err := riak.NewSecondaryIndexQueryCommandBuilder().
		WithBucketType("indexes").
		WithBucket("people").
		WithIndexName("field2_int").
		WithIntRange(1002, 1004).
		Build()
	if c2err != nil {
		return c2err
	}

	wg := &sync.WaitGroup{}
	cmds := [...]riak.Command{c1, c2}

	for _, cmd := range cmds {
		args := &riak.Async{
			Command: cmd,
			Wait:    wg,
		}
		if err := cluster.ExecuteAsync(args); err != nil {
			return err
		}
	}

	wg.Wait()

	for _, cmd := range cmds {
		printIndexQueryResults(cmd)
	}

	return nil
}
예제 #3
0
func indexingObjects(cluster *riak.Cluster) error {
	o1 := &riak.Object{
		Key:   "larry",
		Value: []byte("My name is Larry"),
	}
	o1.AddToIndex("field1_bin", "val1")
	o1.AddToIntIndex("field2_int", 1001)

	o2 := &riak.Object{
		Key:   "moe",
		Value: []byte("My name is Moe"),
	}
	o2.AddToIndex("Field1_bin", "val2")
	o2.AddToIntIndex("Field2_int", 1002)

	o3 := &riak.Object{
		Key:   "curly",
		Value: []byte("My name is Curly"),
	}
	o3.AddToIndex("FIELD1_BIN", "val3")
	o3.AddToIntIndex("FIELD2_INT", 1003)

	o4 := &riak.Object{
		Key:   "veronica",
		Value: []byte("My name is Veronica"),
	}
	o4.AddToIndex("FIELD1_bin", "val4")
	o4.AddToIndex("FIELD1_bin", "val4")
	o4.AddToIndex("FIELD1_bin", "val4a")
	o4.AddToIndex("FIELD1_bin", "val4b")
	o4.AddToIntIndex("FIELD2_int", 1004)
	o4.AddToIntIndex("FIELD2_int", 1005)
	o4.AddToIntIndex("FIELD2_int", 1006)
	o4.AddToIntIndex("FIELD2_int", 1004)
	o4.AddToIntIndex("FIELD2_int", 1004)
	o4.AddToIntIndex("FIELD2_int", 1007)

	objs := [...]*riak.Object{o1, o2, o3, o4}

	wg := &sync.WaitGroup{}
	for _, obj := range objs {
		obj.ContentType = "text/plain"
		obj.Charset = "utf-8"
		obj.ContentEncoding = "utf-8"

		cmd, err := riak.NewStoreValueCommandBuilder().
			WithBucketType("indexes").
			WithBucket("people").
			WithContent(obj).
			Build()
		if err != nil {
			return err
		}

		args := &riak.Async{
			Command: cmd,
			Wait:    wg,
		}
		if err := cluster.ExecuteAsync(args); err != nil {
			return err
		}
	}

	wg.Wait()

	return nil
}