Skip to content

Commit

Permalink
Return results Multi and allow ordering of ops
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel committed Dec 19, 2014
1 parent 1fff4bb commit 6b99eab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
47 changes: 28 additions & 19 deletions zk/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,31 +769,40 @@ func (c *Conn) Sync(path string) (string, error) {
return res.Path, err
}

type MultiOps struct {
Create []CreateRequest
Delete []DeleteRequest
SetData []SetDataRequest
Check []CheckVersionRequest
type MultiResponse struct {
Stat *Stat
String string
}

func (c *Conn) Multi(ops MultiOps) error {
// Multi executes multiple ZooKeeper operations or none of them. The provided
// ops must be one of *CreateRequest, *DeleteRequest, *SetDataRequest, or
// *CheckVersionRequest.
func (c *Conn) Multi(ops ...interface{}) ([]MultiResponse, error) {
req := &multiRequest{
Ops: make([]multiRequestOp, 0, len(ops.Create)+len(ops.Delete)+len(ops.SetData)+len(ops.Check)),
Ops: make([]multiRequestOp, 0, len(ops)),
DoneHeader: multiHeader{Type: -1, Done: true, Err: -1},
}
for _, r := range ops.Create {
req.Ops = append(req.Ops, multiRequestOp{multiHeader{opCreate, false, -1}, r})
}
for _, r := range ops.SetData {
req.Ops = append(req.Ops, multiRequestOp{multiHeader{opSetData, false, -1}, r})
}
for _, r := range ops.Delete {
req.Ops = append(req.Ops, multiRequestOp{multiHeader{opDelete, false, -1}, r})
}
for _, r := range ops.Check {
req.Ops = append(req.Ops, multiRequestOp{multiHeader{opCheck, false, -1}, r})
for _, op := range ops {
var opCode int32
switch op.(type) {
case *CreateRequest:
opCode = opCreate
case *SetDataRequest:
opCode = opSetData
case *DeleteRequest:
opCode = opDelete
case *CheckVersionRequest:
opCode = opCheck
default:
return nil, fmt.Errorf("uknown operation type %T", op)
}
req.Ops = append(req.Ops, multiRequestOp{multiHeader{opCode, false, -1}, op})
}
res := &multiResponse{}
_, err := c.request(opMulti, req, res, nil)
return err
mr := make([]MultiResponse, len(res.Ops))
for i, op := range res.Ops {
mr[i] = MultiResponse{Stat: op.Stat, String: op.String}
}
return mr, err
}
1 change: 0 additions & 1 deletion zk/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func trace(conn1, conn2 net.Conn, client bool) {
found := false
opcode, found = requests[xid]
if !found {
println("WEFWEFEW")
opcode = 0
}
delete(requests, xid)
Expand Down
21 changes: 9 additions & 12 deletions zk/zk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,16 @@ func TestMulti(t *testing.T) {
if err := zk.Delete(path, -1); err != nil && err != ErrNoNode {
t.Fatalf("Delete returned error: %+v", err)
}
ops := MultiOps{
Create: []CreateRequest{
{Path: path, Data: []byte{1, 2, 3, 4}, Acl: WorldACL(PermAll)},
},
SetData: []SetDataRequest{
{Path: path, Data: []byte{1, 2, 3, 4}, Version: -1},
},
// Delete: []DeleteRequest{
// {Path: path, Version: -1},
// },
}
if err := zk.Multi(ops); err != nil {
ops := []interface{}{
&CreateRequest{Path: path, Data: []byte{1, 2, 3, 4}, Acl: WorldACL(PermAll)},
&SetDataRequest{Path: path, Data: []byte{1, 2, 3, 4}, Version: -1},
}
if res, err := zk.Multi(ops...); err != nil {
t.Fatalf("Multi returned error: %+v", err)
} else if len(res) != 2 {
t.Fatalf("Expected 2 responses got %d", len(res))
} else {
t.Logf("%+v", res)
}
if data, stat, err := zk.Get(path); err != nil {
t.Fatalf("Get returned error: %+v", err)
Expand Down

0 comments on commit 6b99eab

Please sign in to comment.