Developing lightweight computation at the DSG edge

Commit 9621aabf authored by Peter Zeller's avatar Peter Zeller
Browse files

some functions for Go interface

parent 1cd2c860
package antidoteclient
import (
"gopkg.in/fatih/pool.v2"
"net"
"fmt"
"gopkg.in/fatih/pool.v2"
"math/rand"
"net"
"time"
)
......@@ -20,16 +20,16 @@ type Host struct {
Port int
}
func NewClient(hosts []Host) (client *Client, err error) {
func NewClient(hosts ...Host) (client *Client, err error) {
pools := make([]pool.Pool, len(hosts))
for i, h := range hosts {
p, err := pool.NewChannelPool(INITIAL_POOL_SIZE, MAX_POOL_SIZE, func () (net.Conn, error) { return net.Dial("tcp", fmt.Sprint("{}:{}", h.Name, h.Port)) })
p, err := pool.NewChannelPool(INITIAL_POOL_SIZE, MAX_POOL_SIZE, func() (net.Conn, error) { return net.Dial("tcp", fmt.Sprintf("%s:%d", h.Name, h.Port)) })
if err != nil {
return
return nil, err
}
pools[i] = p
}
client = &Client {
client = &Client{
pools: pools,
}
return
......@@ -48,13 +48,13 @@ func (client *Client) getConnection() (c *Connection, err error) {
p := client.pools[i]
con, err := p.Get()
if err != nil {
return
return nil, err
}
c = &Connection{
Conn: con,
pool: p,
}
return
return c, nil
}
err = fmt.Errorf("All connections dead")
return
......@@ -95,3 +95,36 @@ func (client *Client) StartTransaction() (tx *InteractiveTransaction, err error)
}
return
}
func (tx *InteractiveTransaction) update(updates ...*ApbUpdateOp) (op *ApbOperationResp, err error) {
apbUpdate := &ApbUpdateObjects{
Updates: updates,
TransactionDescriptor: tx.txID,
}
err = apbUpdate.encode(tx.con)
if err != nil {
return
}
return decodeOperationResp(tx.con)
}
func (tx *InteractiveTransaction) read(objects ...*ApbBoundObject) (resp *ApbReadObjectsResp, err error) {
apbUpdate := &ApbReadObjects{
TransactionDescriptor: tx.txID,
Boundobjects: objects,
}
err = apbUpdate.encode(tx.con)
if err != nil {
return
}
return decodeReadObjectsResp(tx.con)
}
func (tx *InteractiveTransaction) commit() (op *ApbCommitResp, err error) {
msg := &ApbCommitTransaction{}
err = msg.encode(tx.con)
if err != nil {
return
}
return decodeCommitResp(tx.con)
}
package antidoteclient
//import "fmt"
import (
"testing"
"fmt"
)
func TestSimple(t *testing.T) {
client, err := NewClient(Host{"127.0.0.1", 8087})
if err != nil {
t.Fatal(err)
}
tx, err := client.StartTransaction()
if err != nil {
t.Fatal(err)
}
crdtType := CRDTType_COUNTER
key := &ApbBoundObject{
Bucket: []byte("bucket"),
Key: []byte("key"),
Type: &crdtType}
one := int64(1)
tx.update(&ApbUpdateOp{
Boundobject: key,
Operation: &ApbUpdateOperation{Counterop: &ApbCounterUpdate{Inc: &one}},
})
resp, err := tx.read(key)
if err != nil {
t.Fatal(err)
}
fmt.Print(resp.Objects[0])
_, err = tx.commit()
if err != nil {
t.Fatal(err)
}
}
......@@ -2,9 +2,9 @@ package antidoteclient
import (
"encoding/binary"
"io"
"github.com/golang/protobuf/proto"
"fmt"
"github.com/golang/protobuf/proto"
"io"
)
func readMsgRaw(reader io.Reader) (data []byte, err error) {
......@@ -15,7 +15,7 @@ func readMsgRaw(reader io.Reader) (data []byte, err error) {
for count != 4 {
n, err := reader.Read(sizeB[count:])
if err != nil {
return
return nil, err
}
count += uint32(n)
}
......@@ -27,7 +27,7 @@ func readMsgRaw(reader io.Reader) (data []byte, err error) {
n, err := reader.Read(data[count:])
if err != nil {
data = nil
return
return nil, err
}
count += uint32(n)
}
......@@ -196,7 +196,6 @@ func decodeStaticReadObjectsResp(reader io.Reader) (op *ApbStaticReadObjectsResp
return
}
func decodeError(data []byte) (err error) {
resp := &ApbErrorResp{}
err = proto.Unmarshal(data, resp)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment