Developing lightweight computation at the DSG edge
Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
LightKone
antidote-go-client
Commits
9621aabf
Commit
9621aabf
authored
Mar 20, 2018
by
Peter Zeller
Browse files
some functions for Go interface
parent
1cd2c860
Changes
3
Show whitespace changes
Inline
Side-by-side
antidoteclient.go
View file @
9621aabf
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
.
Sprint
f
(
"
%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
)
}
antidoteclient_test.go
0 → 100644
View file @
9621aabf
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
)
}
}
coder.go
View file @
9621aabf
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment