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) }
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) }
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 } }
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 }
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) }
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) }
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 }
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") }
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 }
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)) }
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) }
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()) } }
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) }
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) }
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 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:") }
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 }
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 }
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 } }
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) }
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 }
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 }
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) }
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 }
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:") }
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) }
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 }
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) }
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) }