Ejemplo n.º 1
0
func storeRufus(cluster *riak.Cluster) {
	obj := &riak.Object{
		ContentType:     "text/plain",
		Charset:         "utf-8",
		ContentEncoding: "utf-8",
		Value:           []byte("WOOF!"),
	}

	cmd, err := riak.NewStoreValueCommandBuilder().
		WithBucketType("animals").
		WithBucket("dogs").
		WithKey("rufus").
		WithContent(obj).
		Build()

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

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

	svc := cmd.(*riak.StoreValueCommand)
	rsp := svc.Response
	fmt.Println(rsp.VClock)
}
Ejemplo n.º 2
0
func storeQuote(cluster *riak.Cluster) {
	obj := &riak.Object{
		ContentType:     "text/plain",
		Charset:         "utf-8",
		ContentEncoding: "utf-8",
		Value:           []byte("I have nothing to declare but my genius"),
	}

	cmd, err := riak.NewStoreValueCommandBuilder().
		WithBucketType("quotes").
		WithBucket("oscar_wilde").
		WithKey("genius").
		WithContent(obj).
		Build()

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

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

	svc := cmd.(*riak.StoreValueCommand)
	rsp := svc.Response
	fmt.Println(rsp.VClock)
}
Ejemplo n.º 3
0
func storeCar(cluster *riak.Cluster) {
	obj := &riak.Object{
		ContentType:     "text/plain",
		Charset:         "utf-8",
		ContentEncoding: "utf-8",
		Value:           []byte("vroom"),
	}

	cmd, err := riak.NewStoreValueCommandBuilder().
		WithBucketType("cars").
		WithBucket("dodge").
		WithKey("viper").
		WithW(3).
		WithContent(obj).
		WithReturnBody(true).
		Build()

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

	if err := cluster.Execute(cmd); err != nil {
		fmt.Println(err.Error())
		return
	}
}
Ejemplo n.º 4
0
func insertingObjects(cluster *riak.Cluster) error {
	obj := &riak.Object{
		ContentType:     "text/plain",
		Charset:         "utf-8",
		ContentEncoding: "utf-8",
		BucketType:      "indexes",
		Bucket:          "users",
		Key:             "john_smith",
		Value:           []byte("…user data…"),
	}

	obj.AddToIndex("twitter_bin", "jsmith123")
	obj.AddToIndex("email_bin", "*****@*****.**")

	cmd, err := riak.NewStoreValueCommandBuilder().
		WithContent(obj).
		Build()
	if err != nil {
		return err
	}

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

	return nil
}
Ejemplo n.º 5
0
func storeStray(cluster *riak.Cluster) {
	// There is not a default content type for an object, so it must be defined. For JSON content
	// types, please use 'application/json'
	obj := &riak.Object{
		ContentType:     "text/plain",
		Charset:         "utf-8",
		ContentEncoding: "utf-8",
		Value:           []byte("Found in alley."),
	}

	cmd, err := riak.NewStoreValueCommandBuilder().
		WithBucketType("animals").
		WithBucket("dogs").
		WithContent(obj).
		Build()

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

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

	svc := cmd.(*riak.StoreValueCommand)
	rsp := svc.Response
	fmt.Println(rsp.GeneratedKey)
}
Ejemplo n.º 6
0
func resolveUsingResolver(cluster *riak.Cluster) error {
	// Note: a more sophisticated resolver would
	// look into the objects to pick one, or perhaps
	// present the list to a user to choose
	cr := &FirstSiblingResolver{}

	cmd, err := riak.NewFetchValueCommandBuilder().
		WithBucketType("siblings").
		WithBucket("nickelodeon").
		WithKey("best_character").
		WithConflictResolver(cr).
		Build()
	if err != nil {
		return err
	}

	err = cluster.Execute(cmd)
	if err != nil {
		return err
	}

	fcmd := cmd.(*riak.FetchValueCommand)

	// Test that the resolver just returned one riak.Object
	vlen := len(fcmd.Response.Values)
	if vlen != 1 {
		return fmt.Errorf("expected 1 object, got %v", vlen)
	}

	obj := fcmd.Response.Values[0]
	return storeObject(cluster, obj)
}
Ejemplo n.º 7
0
func incorrectDataType(cluster *riak.Cluster) error {
	obj := &riak.Object{
		BucketType:      "indexes",
		Bucket:          "people",
		Key:             "larry",
		ContentType:     "text/plain",
		Charset:         "utf-8",
		ContentEncoding: "utf-8",
		Value:           []byte("My name is Larry"),
	}
	obj.AddToIndex("field2_int", "bar")

	cmd, err := riak.NewStoreValueCommandBuilder().
		WithContent(obj).
		Build()
	if err != nil {
		return err
	}

	if err := cluster.Execute(cmd); err != nil {
		fmt.Println("[DevUsing2i] index data type error:", err)
	} else {
		return errors.New("[DevUsing2i] expected an error!")
	}

	return nil
}
Ejemplo n.º 8
0
func storeCoach(cluster *riak.Cluster) {
	obj := &riak.Object{
		ContentType:     "text/plain",
		Charset:         "utf-8",
		ContentEncoding: "utf-8",
		Value:           []byte("Pete Carroll"),
	}

	cmd, err := riak.NewStoreValueCommandBuilder().
		WithBucketType("siblings").
		WithBucket("coaches").
		WithKey("seahawks").
		WithContent(obj).
		Build()

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

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

	fmt.Println("Stored Pete Carroll")
}
Ejemplo n.º 9
0
func doPaginatedQuery(cluster *riak.Cluster, continuation []byte) error {
	builder := riak.NewSecondaryIndexQueryCommandBuilder().
		WithBucketType("indexes").
		WithBucket("tweets").
		WithIndexName("hashtags_bin").
		WithRange("ri", "ru").
		WithMaxResults(5)

	if continuation != nil && len(continuation) > 0 {
		builder.WithContinuation(continuation)
	}

	cmd, err := builder.Build()
	if err != nil {
		return err
	}

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

	printIndexQueryResults(cmd)

	sciq := cmd.(*riak.SecondaryIndexQueryCommand)
	if sciq.Response == nil {
		return errors.New("[DevUsing2i] expected response but did not get one")
	}

	rc := sciq.Response.Continuation
	if rc != nil && len(rc) > 0 {
		return doPaginatedQuery(cluster, sciq.Response.Continuation)
	}

	return nil
}
Ejemplo n.º 10
0
func storeAndUpdateSport(cluster *riak.Cluster) {
	obj := &riak.Object{
		ContentType:     "text/plain",
		Charset:         "utf-8",
		ContentEncoding: "utf-8",
		Value:           []byte("Washington Generals"),
	}

	cmd, err := riak.NewStoreValueCommandBuilder().
		WithBucketType("sports").
		WithBucket("nba").
		WithKey("champion").
		WithContent(obj).
		WithReturnBody(true).
		Build()

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

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

	svc := cmd.(*riak.StoreValueCommand)
	rsp := svc.Response
	obj = rsp.Values[0]
	obj.Value = []byte("Harlem Globetrotters")

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

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

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

	svc = cmd.(*riak.StoreValueCommand)
	rsp = svc.Response
	obj = rsp.Values[0]
	fmt.Printf("champion: %v\n", string(obj.Value))
}
Ejemplo n.º 11
0
func storeBucketProperties(cluster *riak.Cluster) error {
	cmd, err := riak.NewStoreBucketPropsCommandBuilder().
		WithBucketType("animals").
		WithBucket("cats").
		WithSearchIndex("famous").
		Build()
	if err != nil {
		return err
	}

	return cluster.Execute(cmd)
}
Ejemplo n.º 12
0
func InitIndexBucket(cluster *riak.Cluster, bucket string) {
	cmd, err := riak.NewStoreBucketPropsCommandBuilder().
		WithBucket(bucket).
		WithAllowMult(true).
		Build()
	if err != nil {
		logError("initIndexBucket", "Error creating command for setting up bucket for \"allow_mult\"", err.Error())
	}
	if err = cluster.Execute(cmd); err != nil {
		logError("initIndexBucket", "Error setting up bucket for \"allow_mult\"", err.Error())
	}
}
Ejemplo n.º 13
0
func storeIndex(cluster *riak.Cluster) error {
	cmd, err := riak.NewStoreIndexCommandBuilder().
		WithIndexName("famous").
		WithSchemaName("_yz_default").
		WithTimeout(time.Second * 30).
		Build()
	if err != nil {
		return err
	}

	return cluster.Execute(cmd)
}
Ejemplo n.º 14
0
func storeObject(cluster *riak.Cluster, obj *riak.Object) error {
	cmd, err := riak.NewStoreValueCommandBuilder().
		WithBucketType("siblings").
		WithBucket("nickelodeon").
		WithKey("best_character").
		WithContent(obj).
		Build()
	if err != nil {
		return err
	}

	return cluster.Execute(cmd)
}
Ejemplo n.º 15
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
	}
}
Ejemplo n.º 16
0
func doAndSearchRequest(cluster *riak.Cluster) error {
	cmd, err := riak.NewSearchCommandBuilder().
		WithIndexName("famous").
		WithQuery("leader_b:true AND age_i:[30 TO *]").
		Build()
	if err != nil {
		return err
	}

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

	return printDocs(cmd, "AND Search Documents:")
}
Ejemplo n.º 17
0
func queryingIndexes(cluster *riak.Cluster) error {
	cmd, err := riak.NewSecondaryIndexQueryCommandBuilder().
		WithBucketType("indexes").
		WithBucket("users").
		WithIndexName("twitter_bin").
		WithIndexKey("jsmith123").
		Build()
	if err != nil {
		return err
	}

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

	printIndexQueryResults(cmd)
	return nil
}
Ejemplo n.º 18
0
func queryingRangeWithTerms(cluster *riak.Cluster) error {
	cmd, err := riak.NewSecondaryIndexQueryCommandBuilder().
		WithBucketType("indexes").
		WithBucket("tweets").
		WithIndexName("hashtags_bin").
		WithRange("rock", "rocl").
		Build()
	if err != nil {
		return err
	}

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

	printIndexQueryResults(cmd)
	return nil
}
Ejemplo n.º 19
0
func storeUserThenDelete(cluster *riak.Cluster) {
	obj := &riak.Object{
		ContentType:     "application/json",
		Charset:         "utf-8",
		ContentEncoding: "utf-8",
		Value:           []byte("{'user':'******'}"),
	}

	cmd, err := riak.NewStoreValueCommandBuilder().
		WithBucketType("users").
		WithBucket("random_user_keys").
		WithContent(obj).
		Build()

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

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

	svc := cmd.(*riak.StoreValueCommand)
	rsp := svc.Response
	fmt.Printf("Generated key: %v\n", rsp.GeneratedKey)

	cmd, err = riak.NewDeleteValueCommandBuilder().
		WithBucketType("users").
		WithBucket("random_user_keys").
		WithKey(rsp.GeneratedKey).
		Build()

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

	if err := cluster.Execute(cmd); err != nil {
		fmt.Println(err.Error())
		return
	}
}
Ejemplo n.º 20
0
func fetchBucketProps(cluster *riak.Cluster) {
	cmd, err := riak.NewFetchBucketPropsCommandBuilder().
		WithBucketType("n_val_of_5").
		WithBucket("any_bucket_name").
		Build()

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

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

	fbc := cmd.(*riak.FetchBucketPropsCommand)
	fmt.Println("bucket props:", fbc.Response)
}
Ejemplo n.º 21
0
func invalidFieldNames(cluster *riak.Cluster) error {
	cmd, err := riak.NewSecondaryIndexQueryCommandBuilder().
		WithBucketType("indexes").
		WithBucket("users").
		WithIndexName("field2_foo").
		WithIndexKey("jsmith123").
		Build()
	if err != nil {
		return err
	}

	if err := cluster.Execute(cmd); err != nil {
		fmt.Println("[DevUsing2i] field name error:", err)
	} else {
		return errors.New("[DevUsing2i] expected an error!")
	}

	return nil
}
Ejemplo n.º 22
0
func doSearchRequest(cluster *riak.Cluster) error {
	cmd, err := riak.NewSearchCommandBuilder().
		WithIndexName("famous").
		WithQuery("name_s:Lion*").
		Build()
	if err != nil {
		return err
	}

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

	if err := printDocs(cmd, "Search Request Documents:"); err != nil {
		return err
	}

	sc := cmd.(*riak.SearchCommand)
	doc := sc.Response.Docs[0] // NB: SearchDoc struct type

	cmd, err = riak.NewFetchValueCommandBuilder().
		WithBucketType(doc.BucketType).
		WithBucket(doc.Bucket).
		WithKey(doc.Key).
		Build()
	if err != nil {
		return err
	}

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

	fc := cmd.(*riak.FetchValueCommand)
	if json, jerr := json.MarshalIndent(fc.Response, "", "  "); jerr != nil {
		return jerr
	} else {
		fmt.Println(string(json))
	}

	return nil
}
Ejemplo n.º 23
0
func resolveChoosingFirst(cluster *riak.Cluster) error {
	cmd, err := riak.NewFetchValueCommandBuilder().
		WithBucketType("siblings").
		WithBucket("nickelodeon").
		WithKey("best_character").
		Build()
	if err != nil {
		return err
	}

	err = cluster.Execute(cmd)
	if err != nil {
		return err
	}

	fcmd := cmd.(*riak.FetchValueCommand)
	obj := fcmd.Response.Values[0]

	return storeObject(cluster, obj)
}
Ejemplo n.º 24
0
func readSiblings(cluster *riak.Cluster) error {
	cmd, err := riak.NewFetchValueCommandBuilder().
		WithBucketType("siblings").
		WithBucket("nickelodeon").
		WithKey("best_character").
		Build()
	if err != nil {
		return err
	}

	err = cluster.Execute(cmd)
	if err != nil {
		return err
	}

	fcmd := cmd.(*riak.FetchValueCommand)
	fmt.Printf("[DevUsingConflictRes] nickelodeon/best_character has '%v' siblings\n", len(fcmd.Response.Values))

	return nil
}
Ejemplo n.º 25
0
func doPaginatedSearchRequest(cluster *riak.Cluster) error {
	rowsPerPage := uint32(2)
	page := uint32(2)
	start := rowsPerPage * (page - uint32(1))

	cmd, err := riak.NewSearchCommandBuilder().
		WithIndexName("famous").
		WithQuery("*:*").
		WithStart(start).
		WithNumRows(rowsPerPage).
		Build()
	if err != nil {
		return err
	}

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

	return printDocs(cmd, "Paginated Search Documents:")
}
Ejemplo n.º 26
0
func fetchRufus(cluster *riak.Cluster) {
	cmd, err := riak.NewFetchValueCommandBuilder().
		WithBucketType("animals").
		WithBucket("dogs").
		WithKey("rufus").
		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
	fmt.Println(rsp.IsNotFound)
}
Ejemplo n.º 27
0
func updateCoach(cluster *riak.Cluster, team, newCoach string) error {
	var cmd riak.Command
	var err error

	cmd, err = riak.NewFetchValueCommandBuilder().
		WithBucketType("siblings").
		WithBucket("coaches").
		WithKey(team).
		Build()

	if err != nil {
		return err
	}

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

	fvc := cmd.(*riak.FetchValueCommand)
	obj := fvc.Response.Values[0]
	obj.Value = []byte(newCoach)

	cmd, err = riak.NewStoreValueCommandBuilder().
		WithBucketType("siblings").
		WithBucket("coaches").
		WithKey(team).
		WithContent(obj).
		Build()

	if err != nil {
		return err
	}

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

	return nil
}
Ejemplo n.º 28
0
func resolveViaOverwrite(cluster *riak.Cluster) error {
	cmd, err := riak.NewFetchValueCommandBuilder().
		WithBucketType("siblings").
		WithBucket("nickelodeon").
		WithKey("best_character").
		Build()
	if err != nil {
		return err
	}

	err = cluster.Execute(cmd)
	if err != nil {
		return err
	}

	fcmd := cmd.(*riak.FetchValueCommand)
	obj := fcmd.Response.Values[0]
	// This overwrites the value and provides the canonical one
	obj.Value = []byte("Stimpy")

	return storeObject(cluster, obj)
}
Ejemplo n.º 29
0
func deleteIndex(cluster *riak.Cluster) error {
	cmd, err := riak.NewStoreBucketPropsCommandBuilder().
		WithBucketType("animals").
		WithBucket("cats").
		WithSearchIndex("_dont_index_").
		Build()
	if err != nil {
		return err
	}

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

	cmd, err = riak.NewDeleteIndexCommandBuilder().
		WithIndexName("famous").
		Build()
	if err != nil {
		return err
	}

	return cluster.Execute(cmd)
}