Beispiel #1
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()
}
Beispiel #2
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))
	}
}