Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import riak "github.com/tpjg/goriakpbc"
import "fmt"
import "strconv"
import "sync"
func setupBucket(cli *riak.Client, bucketName string) error {
bucket, err := cli.NewBucket(bucketName)
if err != nil {
return err
}
err = bucket.SetAllowMult(true)
if err != nil {
return err
}
return nil
}
const do_keys = 10000
func main() {
// Connect + setup bucket
con := riak.NewClientPool("localhost:10017", 100)
err := con.Connect()
if err != nil {
panic(err)
}
bucket, err := con.NewBucket("test_bucket_no_one_has2")
if err != nil {
panic(err)
}
err = bucket.SetAllowMult(true)
if err != nil {
panic(err)
}
// Ok, first insert 10000 items.
wg := sync.WaitGroup{}
wg.Add(do_keys)
for i := 0; i < do_keys; i++ {
go func(i int) {
defer wg.Done()
opts := make(map[string]uint32)
opts["pr"] = 3
opts["r"] = 3
item, err := bucket.Get(strconv.Itoa(i), opts)
if item == nil {
panic(err)
}
item.Data = []byte("ASDF")
m := make(map[string]uint32)
m["w"] = 3
m["pw"] = 3
m["dw"] = 3
x := make([]map[string]uint32, 10)
x[0] = m
item.Options = x
err = item.Store()
if err != nil {
panic(err)
}
}(i)
}
wg.Wait()
fmt.Println("Done insert")
// Verify items exist
wg.Add(do_keys)
for i := 0; i < do_keys; i++ {
go func(i int) {
defer wg.Done()
opts := make(map[string]uint32)
opts["pr"] = 3
opts["r"] = 3
_, err := bucket.Get(strconv.Itoa(i), opts)
if err != nil {
fmt.Printf("Failed to fetch item %v err %s\n", i, err)
}
}(i)
}
wg.Wait()
fmt.Println("Done fetch")
// And Delete
keys, err := bucket.ListKeys()
if err != nil {
panic(err)
}
wg.Add(len(keys))
for _, key := range keys {
go func(key string) {
defer wg.Done()
obj, err := bucket.Get(string(key))
if obj == nil {
panic(err)
}
m := make(map[string]uint32)
m["pw"] = 3
m["pr"] = 3
m["r"] = 3
m["w"] = 3
x := make([]map[string]uint32, 10)
x[0] = m
obj.Options = x
err = obj.Destroy()
if err != nil {
panic(err)
}
}(string(key))
}
wg.Wait()
fmt.Println("Done Delete")
fmt.Println("DONE")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.