Ejemplo n.º 1
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.º 2
0
func (dao *LogIndexRiakDAO) fetch(userId string) *riak.FetchValueResponse {
	cmd, _ := riak.NewFetchValueCommandBuilder().
		WithBucket(dao.IndexBucket).
		WithNotFoundOk(true).
		WithKey(userId).
		WithConflictResolver(dao.Resolver).
		Build()
	if err := dao.Cluster.Execute(cmd); err != nil {
		logErr("Fetching timeline for "+userId, err)
		return nil
	}
	fvc := cmd.(*riak.FetchValueCommand)
	return fvc.Response
}
Ejemplo n.º 3
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 *LogRecordRiakDao) GetLogRecord(logId string) (string, error) {
	cmd, _ := riak.NewFetchValueCommandBuilder().
		WithBucket(dao.LogBucket).
		WithNotFoundOk(true).
		WithKey(logId).
		Build()
	if err := dao.Cluster.Execute(cmd); err != nil {
		logErr("Fetching log record for "+logId, err)
		return "", err
	}
	fvc := cmd.(*riak.FetchValueCommand)
	if fvc.Response == nil || fvc.Response.IsNotFound || len(fvc.Response.Values) == 0 {
		return "", nil
	}
	return string(fvc.Response.Values[0].Value), nil
}
Ejemplo n.º 5
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.º 6
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.º 7
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.º 8
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.º 9
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.º 10
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.º 11
0
func main() {
	var err error

	// un-comment-out to enable debug logging
	// riak.EnableDebugLogging = true

	o := &riak.NewClientOptions{
		RemoteAddresses: []string{util.GetRiakAddress()},
	}

	var c *riak.Client
	c, err = riak.NewClient(o)
	if err != nil {
		util.ErrExit(err)
	}

	defer func() {
		if err := c.Stop(); err != nil {
			util.ErrExit(err)
		}
	}()

	val1 := uint32(1)
	val1buf := make([]byte, 4)
	binary.LittleEndian.PutUint32(val1buf, val1)

	val2 := "two"

	val3 := struct{ MyValue int }{3} // NB: ensure that members are exported (i.e. capitalized)
	var val3json []byte
	val3json, err = json.Marshal(val3)
	if err != nil {
		util.ErrExit(err)
	}

	bucket := "test"

	util.Log.Println("Creating Objects In Riak...")

	objs := []*riak.Object{
		{
			Bucket:      bucket,
			Key:         "one",
			ContentType: "application/octet-stream",
			Value:       val1buf,
		},
		{
			Bucket:      bucket,
			Key:         "two",
			ContentType: "text/plain",
			Value:       []byte(val2),
		},
		{
			Bucket:      bucket,
			Key:         "three",
			ContentType: "application/json",
			Value:       val3json,
		},
	}

	var cmd riak.Command
	wg := &sync.WaitGroup{}

	for _, o := range objs {
		cmd, err = riak.NewStoreValueCommandBuilder().
			WithContent(o).
			Build()
		if err != nil {
			util.ErrLog.Println(err)
			continue
		}
		a := &riak.Async{
			Command: cmd,
			Wait:    wg,
		}
		if err := c.ExecuteAsync(a); err != nil {
			util.ErrLog.Println(err)
		}
	}

	wg.Wait()

	util.Log.Println("Reading Objects From Riak...")

	d := make(chan riak.Command, len(objs))

	for _, o := range objs {
		cmd, err = riak.NewFetchValueCommandBuilder().
			WithBucket(bucket).
			WithKey(o.Key).
			Build()
		if err != nil {
			util.ErrLog.Println(err)
			continue
		}
		a := &riak.Async{
			Command: cmd,
			Wait:    wg,
			Done:    d,
		}
		if err := c.ExecuteAsync(a); err != nil {
			util.ErrLog.Println(err)
		}
	}

	wg.Wait()
	close(d)

	var obj3 *riak.Object

	for done := range d {
		f := done.(*riak.FetchValueCommand)
		/* un-comment to dump fetched object as JSON
		if json, jerr := json.MarshalIndent(f.Response, "", "  "); err != nil {
			util.ErrLog.Println(jerr)
		} else {
			util.Log.Println("fetched value: ", string(json))
		}
		*/
		obj := f.Response.Values[0]
		switch obj.Key {
		case "one":
			if actual, expected := binary.LittleEndian.Uint32(obj.Value), val1; actual != expected {
				util.ErrLog.Printf("key: %s, actual %v, expected %v", obj.Key, actual, expected)
			}
		case "two":
			if actual, expected := string(obj.Value), val2; actual != expected {
				util.ErrLog.Printf("key: %s, actual %v, expected %v", obj.Key, actual, expected)
			}
		case "three":
			obj3 = obj
			val3.MyValue = 0
			if jerr := json.Unmarshal(obj.Value, &val3); jerr != nil {
				util.ErrLog.Println(jerr)
			} else {
				if actual, expected := val3.MyValue, int(3); actual != expected {
					util.ErrLog.Printf("key: %s, actual %v, expected %v", obj.Key, actual, expected)
				}
			}
		default:
			util.ErrLog.Printf("unrecognized key: %s", obj.Key)
		}
	}

	util.Log.Println("Updating Object Three In Riak...")

	val3.MyValue = 42
	obj3.Value, err = json.Marshal(val3)
	if err != nil {
		util.ErrExit(err)
	}

	cmd, err = riak.NewStoreValueCommandBuilder().
		WithContent(obj3).
		WithReturnBody(true).
		Build()
	if err != nil {
		util.ErrLog.Println(err)
	} else {
		if err := c.Execute(cmd); err != nil {
			util.ErrLog.Println(err)
		}
	}

	svcmd := cmd.(*riak.StoreValueCommand)
	svrsp := svcmd.Response
	obj3 = svrsp.Values[0]
	val3.MyValue = 0
	if jerr := json.Unmarshal(obj3.Value, &val3); jerr != nil {
		util.ErrLog.Println(jerr)
	} else {
		if actual, expected := val3.MyValue, int(42); actual != expected {
			util.ErrLog.Printf("key: %s, actual %v, expected %v", obj3.Key, actual, expected)
		}
	}
	util.Log.Println("updated object key: ", obj3.Key)
	util.Log.Println("updated object value: ", val3.MyValue)

	book := &Book{
		ISBN:        "1111979723",
		Title:       "Moby Dick",
		Author:      "Herman Melville",
		Body:        "Call me Ishmael. Some years ago...",
		CopiesOwned: 3,
	}

	var jbook []byte
	jbook, err = json.Marshal(book)
	if err != nil {
		util.ErrExit(err)
	}

	bookObj := &riak.Object{
		Bucket:      "books",
		Key:         book.ISBN,
		ContentType: "application/json",
		Value:       jbook,
	}

	cmd, err = riak.NewStoreValueCommandBuilder().
		WithContent(bookObj).
		WithReturnBody(false).
		Build()
	if err != nil {
		util.ErrLog.Println(err)
	} else {
		if err := c.Execute(cmd); err != nil {
			util.ErrLog.Println(err)
		}
	}

	cmd, err = riak.NewFetchValueCommandBuilder().
		WithBucket("books").
		WithKey(book.ISBN).
		Build()
	if err != nil {
		util.ErrExit(err)
	}
	if err := c.Execute(cmd); err != nil {
		util.ErrLog.Println(err)
	}

	fcmd := cmd.(*riak.FetchValueCommand)
	bookObj = fcmd.Response.Values[0]
	util.Log.Println("serialized object: ", string(bookObj.Value))

	util.Log.Println("Deleting Objects From Riak...")

	objs = append(objs, bookObj)
	for _, o := range objs {
		cmd, err = riak.NewDeleteValueCommandBuilder().
			WithBucket(o.Bucket).
			WithKey(o.Key).
			Build()
		if err != nil {
			util.ErrLog.Println(err)
			continue
		}
		a := &riak.Async{
			Command: cmd,
			Wait:    wg,
		}
		if err := c.ExecuteAsync(a); err != nil {
			util.ErrLog.Println(err)
		}
	}

	wg.Wait()
}
Ejemplo n.º 12
0
func main() {
	var err error
	var customerId string

	util.Log.Println("Creating Data")

	var cd time.Time
	cd, err = time.Parse(timeFmt, "2013-10-01 14:30:26")
	if err != nil {
		util.ErrExit(err)
	}

	customer := &Customer{
		Name:        "John Smith",
		Address:     "123 Main Street",
		City:        "Columbus",
		State:       "Ohio",
		Zip:         "43210",
		Phone:       "+1-614-555-5555",
		CreatedDate: cd,
	}

	util.Log.Printf("customer: %v", customer)

	util.Log.Println("Starting Client")

	// un-comment-out to enable debug logging
	// riak.EnableDebugLogging = true

	o := &riak.NewClientOptions{
		RemoteAddresses: []string{util.GetRiakAddress()},
	}

	var c *riak.Client
	c, err = riak.NewClient(o)
	if err != nil {
		util.ErrExit(err)
	}

	defer func() {
		if err := c.Stop(); err != nil {
			util.ErrExit(err)
		}
	}()

	util.Log.Println("Storing Customer")

	var cmd riak.Command
	var customerJson []byte

	customerJson, err = json.Marshal(customer)
	if err != nil {
		util.ErrExit(err)
	}

	obj := &riak.Object{
		Bucket:      customersBucket,
		ContentType: "application/json",
		Value:       customerJson,
	}

	cmd, err = riak.NewStoreValueCommandBuilder().
		WithContent(obj).
		WithReturnBody(true).
		Build()
	if err != nil {
		util.ErrExit(err)
	}
	if eerr := c.Execute(cmd); eerr != nil {
		util.ErrExit(eerr)
	}

	svc := cmd.(*riak.StoreValueCommand)
	customerId = svc.Response.GeneratedKey
	if customerId == "" {
		util.ErrExit(errors.New("expected generated customer Id"))
	} else {
		util.Log.Println("Customer ID:", customerId)
	}

	util.Log.Println("Storing Data")

	var orders []*Order
	orders, err = createOrders(customerId)
	if err != nil {
		util.ErrExit(err)
	}

	var orderSummary *OrderSummary
	var orderSummaryJson []byte
	orderSummary = createOrderSummary(customerId, orders)

	ccmds := 1 + len(orders)
	cmds := make([]riak.Command, ccmds)

	// command to store OrderSummary
	orderSummaryJson, err = json.Marshal(orderSummary)
	if err != nil {
		util.ErrExit(err)
	}
	obj = &riak.Object{
		Bucket:      orderSummariesBucket,
		Key:         customerId,
		ContentType: "application/json",
		Value:       orderSummaryJson,
	}
	cmds[0], err = riak.NewStoreValueCommandBuilder().
		WithContent(obj).
		Build()
	if err != nil {
		util.ErrExit(err)
	}

	for i, order := range orders {
		// command to store Order
		var orderJson []byte
		orderJson, err = json.Marshal(order)
		if err != nil {
			util.ErrExit(err)
		}
		obj = &riak.Object{
			Bucket:      ordersBucket,
			Key:         order.Id,
			ContentType: "application/json",
			Value:       orderJson,
		}
		cmds[i+1], err = riak.NewStoreValueCommandBuilder().
			WithContent(obj).
			Build()
		if err != nil {
			util.ErrExit(err)
		}
	}

	errored := false
	wg := &sync.WaitGroup{}
	for _, cmd := range cmds {
		a := &riak.Async{
			Command: cmd,
			Wait:    wg,
		}
		if eerr := c.ExecuteAsync(a); eerr != nil {
			errored = true
			util.ErrLog.Println(eerr)
		}
	}
	wg.Wait()
	if errored {
		util.ErrExit(errors.New("error, exiting!"))
	}

	util.Log.Println("Fetching related data by shared key")

	cmds = cmds[:0]

	// fetch customer
	cmd, err = riak.NewFetchValueCommandBuilder().
		WithBucket(customersBucket).
		WithKey(customerId).
		Build()
	if err != nil {
		util.ErrExit(err)
	}
	cmds = append(cmds, cmd)

	// fetch OrderSummary
	cmd, err = riak.NewFetchValueCommandBuilder().
		WithBucket(orderSummariesBucket).
		WithKey(customerId).
		Build()
	if err != nil {
		util.ErrExit(err)
	}
	cmds = append(cmds, cmd)

	doneChan := make(chan riak.Command)
	errored = false
	for _, cmd := range cmds {
		a := &riak.Async{
			Command: cmd,
			Done:    doneChan,
		}
		if eerr := c.ExecuteAsync(a); eerr != nil {
			errored = true
			util.ErrLog.Println(eerr)
		}
	}
	if errored {
		util.ErrExit(errors.New("error, exiting!"))
	}

	for i := 0; i < len(cmds); i++ {
		select {
		case d := <-doneChan:
			if fv, ok := d.(*riak.FetchValueCommand); ok {
				obj := fv.Response.Values[0]
				switch obj.Bucket {
				case customersBucket:
					util.Log.Printf("Customer     1: %v", string(obj.Value))
				case orderSummariesBucket:
					util.Log.Printf("OrderSummary 1: %v", string(obj.Value))
				}
			} else {
				util.ErrExit(fmt.Errorf("unknown response command type: %v", reflect.TypeOf(d)))
			}
		case <-time.After(5 * time.Second):
			util.ErrExit(errors.New("fetch operations took too long"))
		}
	}

	util.Log.Println("Adding Index Data")

	// fetch orders to add index data
	cmds = cmds[:0]

	for _, order := range orders {
		cmd, err = riak.NewFetchValueCommandBuilder().
			WithBucket(ordersBucket).
			WithKey(order.Id).
			Build()
		if err != nil {
			util.ErrExit(err)
		}
		cmds = append(cmds, cmd)
	}

	errored = false
	for _, cmd := range cmds {
		a := &riak.Async{
			Command: cmd,
			Done:    doneChan,
		}
		if eerr := c.ExecuteAsync(a); eerr != nil {
			errored = true
			util.ErrLog.Println(eerr)
		}
	}
	if errored {
		util.ErrExit(errors.New("error, exiting!"))
	}

	errored = false
	for i := 0; i < len(cmds); i++ {
		select {
		case d := <-doneChan:
			if fv, ok := d.(*riak.FetchValueCommand); ok {
				obj := fv.Response.Values[0]
				switch obj.Key {
				case "1":
					obj.AddToIntIndex("SalespersonId_int", 9000)
					obj.AddToIndex("OrderDate_bin", "2013-10-01")
				case "2":
					obj.AddToIntIndex("SalespersonId_int", 9001)
					obj.AddToIndex("OrderDate_bin", "2013-10-15")
				case "3":
					obj.AddToIntIndex("SalespersonId_int", 9000)
					obj.AddToIndex("OrderDate_bin", "2013-11-03")
				}
				scmd, serr := riak.NewStoreValueCommandBuilder().
					WithContent(obj).
					Build()
				if serr != nil {
					util.ErrExit(serr)
				}
				a := &riak.Async{
					Command: scmd,
					Wait:    wg,
				}
				if eerr := c.ExecuteAsync(a); eerr != nil {
					errored = true
					util.ErrLog.Println(eerr)
				}
			} else {
				util.ErrExit(fmt.Errorf("unknown response command type: %v", reflect.TypeOf(d)))
			}
		case <-time.After(5 * time.Second):
			util.ErrExit(errors.New("fetch operations took too long"))
		}
	}

	if errored {
		util.ErrExit(errors.New("error, exiting!"))
	}

	wg.Wait()
	close(doneChan)

	util.Log.Println("Index Queries")

	cmd, err = riak.NewSecondaryIndexQueryCommandBuilder().
		WithBucket(ordersBucket).
		WithIndexName("SalespersonId_int").
		WithIndexKey("9000").
		Build()
	if err != nil {
		util.ErrExit(err)
	}

	if eerr := c.Execute(cmd); eerr != nil {
		util.ErrExit(eerr)
	}

	qcmd := cmd.(*riak.SecondaryIndexQueryCommand)
	for _, rslt := range qcmd.Response.Results {
		util.Log.Println("Jane's Orders, key: ", string(rslt.ObjectKey))
	}

	cmd, err = riak.NewSecondaryIndexQueryCommandBuilder().
		WithBucket(ordersBucket).
		WithIndexName("OrderDate_bin").
		WithRange("2013-10-01", "2013-10-31").
		Build()
	if err != nil {
		util.ErrExit(err)
	}

	if eerr := c.Execute(cmd); eerr != nil {
		util.ErrExit(eerr)
	}

	qcmd = cmd.(*riak.SecondaryIndexQueryCommand)
	for _, rslt := range qcmd.Response.Results {
		util.Log.Println("October's Orders, key: ", string(rslt.ObjectKey))
	}
}