Skip to content

Commit

Permalink
Merge branch 'marshalling_support'
Browse files Browse the repository at this point in the history
Conflicts:
	CHANGELOG.md
  • Loading branch information
khaf committed Feb 17, 2015
2 parents a6511de + 5c1cb88 commit 6bceed4
Show file tree
Hide file tree
Showing 28 changed files with 1,515 additions and 471 deletions.
70 changes: 70 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
# Change history

## Feb 17 2015 : v1.4.0

This is a major release, and makes using the client much easier to develop applications.

* **New Features**

* Added Marshalling Support for Put and Get operations. Refer to [Marshalling Test](client_object_test.go) to see how to take advantage.
Same functionality for other APIs will follow soon.
Example:
```go
type SomeStruct struct {
A int `as:"a"` // alias the field to myself
Self *SomeStruct `as:"-"` // will not persist the field
}

type OtherStruct struct {
i interface{}
OtherObject *OtherStruct
}

obj := &OtherStruct {
i: 15,
OtherObject: OtherStruct {A: 18},
}

key, _ := as.NewKey("ns", "set", value)
err := client.PutObject(nil, key, obj)
// handle error here

rObj := &OtherStruct{}
err = client.GetObject(nil, key, rObj)
```

* Added `Recordset.Results()`. Consumers of a recordset do not have to implement a select anymore. Instead of:
```go
recordset, err := client.ScanAll(...)
L:
for {
select {
case r := <-recordset.Record:
if r == nil {
break L
}
// process record here
case e := <-recordset.Errors:
// handle error here
}
}
```

one should only range on `recordset.Results()`:

```go
recordset, err := client.ScanAll(...)
for res := range recordset.Results() {
if res.Err != nil {
// handle error here
} else {
// process record here
fmt.Println(res.Record.Bins)
}
}
```

Use of the old pattern is discouraged and deprecated, and direct access to recordset.Records and recordset.Errors will be removed in a future release.

* **Improvements**

* Custom Types are now allowed as bin values.

## Jan 26 2015 : v1.3.1

* **Improvements**
Expand Down
84 changes: 71 additions & 13 deletions aerospike_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package aerospike_test

import (
"bytes"
"runtime"

. "github.com/aerospike/aerospike-client-go"

"testing"
Expand All @@ -23,18 +26,39 @@ import (
var r *Record
var err error

func benchGet(times int, client *Client, key *Key) {
type OBJECT struct {
Price int
DBName string
Blob []byte
}

func benchGet(times int, client *Client, key *Key, obj interface{}) {
for i := 0; i < times; i++ {
if r, err = client.Get(nil, key); err != nil {
panic(err)
if obj == nil {
if r, err = client.Get(nil, key); err != nil {
panic(err)
}
} else {
if err = client.GetObject(nil, key, obj); err != nil {
panic(err)
}
}
}
}

func benchPut(times int, client *Client, key *Key, bins []*Bin, wp *WritePolicy) {
func benchPut(times int, client *Client, key *Key, wp *WritePolicy, obj interface{}) {
for i := 0; i < times; i++ {
if err = client.PutBins(wp, key, bins...); err != nil {
panic(err)
if obj == nil {
dbName := NewBin("dbname", "CouchDB")
price := NewBin("price", 0)
keywords := NewBin("keywords", []string{"concurrent", "fast"})
if err = client.PutBins(wp, key, dbName, price, keywords); err != nil {
panic(err)
}
} else {
if err = client.PutObject(wp, key, obj); err != nil {
panic(err)
}
}
}
}
Expand All @@ -46,25 +70,59 @@ func Benchmark_Get(b *testing.B) {
}

key, _ := NewKey("test", "databases", "Aerospike")
obj := &OBJECT{198, "Jack Shaftoe and Company", []byte(bytes.Repeat([]byte{32}, 1000))}
client.PutObject(nil, key, obj)

b.N = 100000
runtime.GC()
b.ResetTimer()
benchGet(b.N, client, key)
benchGet(b.N, client, key, nil)
}

func Benchmark_GetObject(b *testing.B) {
client, err := NewClientWithPolicy(clientPolicy, *host, *port)
if err != nil {
b.Fail()
}

key, _ := NewKey("test", "databases", "Aerospike")

obj := &OBJECT{198, "Jack Shaftoe and Company", []byte(bytes.Repeat([]byte{32}, 1000))}
client.PutObject(nil, key, obj)

b.N = 100000
runtime.GC()
b.ResetTimer()
benchGet(b.N, client, key, obj)
}

func Benchmark_Put(b *testing.B) {
client, err := NewClient("localhost", 3000)
client, err := NewClient(*host, *port)
if err != nil {
b.Fail()
}

key, _ := NewKey("test", "databases", "Aerospike")
writepolicy := NewWritePolicy(0, 0)

dbName := NewBin("dbname", "CouchDB")
price := NewBin("price", 0)
// keywords := NewBin("keywords", []string{"concurrent", "fast"})
// speeds := NewBin("keywords", []int{18, 251})
b.N = 100000
runtime.GC()
b.ResetTimer()
benchPut(b.N, client, key, writepolicy, nil)
}

func Benchmark_PutObject(b *testing.B) {
client, err := NewClient(*host, *port)
if err != nil {
b.Fail()
}

obj := &OBJECT{198, "Jack Shaftoe and Company", []byte(bytes.Repeat([]byte{32}, 1000))}
key, _ := NewKey("test", "databases", "Aerospike")
writepolicy := NewWritePolicy(0, 0)

b.N = 100000
runtime.GC()
b.ResetTimer()
benchPut(b.N, client, key, []*Bin{dbName, price}, writepolicy)
benchPut(b.N, client, key, writepolicy, obj)
}
10 changes: 1 addition & 9 deletions batch_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"fmt"

. "github.com/aerospike/aerospike-client-go/types"
. "github.com/aerospike/aerospike-client-go/types/atomic"
// . "github.com/aerospike/aerospike-client-go/types/atomic"
Buffer "github.com/aerospike/aerospike-client-go/utils/buffer"
)

Expand All @@ -28,22 +28,18 @@ const (

type multiCommand interface {
Stop()
IsValid() bool
}

type baseMultiCommand struct {
*baseCommand

recordset *Recordset

valid *AtomicBool
}

func newMultiCommand(node *Node, recordset *Recordset) *baseMultiCommand {
return &baseMultiCommand{
baseCommand: &baseCommand{node: node},
recordset: recordset,
valid: NewAtomicBool(true),
}
}

Expand Down Expand Up @@ -132,7 +128,3 @@ func (cmd *baseMultiCommand) readBytes(length int) error {
cmd.dataOffset += length
return nil
}

func (cmd *baseMultiCommand) IsValid() bool {
return cmd.valid.Get()
}
4 changes: 0 additions & 4 deletions batch_command_exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ func (cmd *batchCommandExists) parseRecordResults(ifc command, receiveSize int)
cmd.dataOffset = 0

for cmd.dataOffset < receiveSize {
if !cmd.IsValid() {
return false, NewAerospikeError(QUERY_TERMINATED)
}

if err := cmd.readBytes(int(_MSG_REMAINING_HEADER_SIZE)); err != nil {
return false, err
}
Expand Down
4 changes: 0 additions & 4 deletions batch_command_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ func (cmd *batchCommandGet) parseRecord(key *Key, opCount int, generation int, e
var bins map[string]interface{}

for i := 0; i < opCount; i++ {
if !cmd.IsValid() {
return nil, NewAerospikeError(QUERY_TERMINATED)
}

if err := cmd.readBytes(8); err != nil {
return nil, err
}
Expand Down
41 changes: 33 additions & 8 deletions bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package aerospike

import (
. "github.com/aerospike/aerospike-client-go/types"
)

// BinMap is used to define a map of bin names to values.
type BinMap map[string]interface{}

Expand All @@ -35,15 +39,36 @@ func NewBin(name string, value interface{}) *Bin {
}
}

func binMapToBins(bins BinMap) []*Bin {
binList := make([]*Bin, 0, len(bins))
for k, v := range bins {
binList = append(binList, NewBin(k, v))
}
return binList
}

// String implements Stringer interface.
func (bn *Bin) String() string {
return bn.Name + ":" + bn.Value.String()
}

func binMapToBins(bins []*Bin, binMap BinMap) []*Bin {
i := 0
for k, v := range binMap {
bins[i].Name = k
bins[i].Value = NewValue(v)
i++
}

return bins
}

// pool Bins so that we won't have to allocate them everytime
var binPool = NewPool(512)

func init() {
binPool.New = func(params ...interface{}) interface{} {
size := params[0].(int)
bins := make([]*Bin, size, size)
for i := range bins {
bins[i] = &Bin{}
}
return bins
}

binPool.IsUsable = func(obj interface{}, params ...interface{}) bool {
return len(obj.([]*Bin)) >= params[0].(int)
}
}
Loading

0 comments on commit 6bceed4

Please sign in to comment.