De-nests the KV output structure (removes DirEnt member).

This commit is contained in:
James Phillips 2016-05-11 13:48:03 -07:00
parent 960b9d6fb6
commit 4882a9fe43
6 changed files with 114 additions and 152 deletions

View File

@ -286,7 +286,7 @@ type TxnOps []*TxnOp
// TxnResult is the internal format we receive from Consul. // TxnResult is the internal format we receive from Consul.
type TxnResult struct { type TxnResult struct {
KV *struct{ DirEnt *KVPair } KV *KVPair
} }
// TxnResults is a list of TxnResult objects. // TxnResults is a list of TxnResult objects.
@ -373,11 +373,7 @@ func (k *KV) Txn(txn KVTxnOps, q *WriteOptions) (bool, *KVTxnResponse, *WriteMet
Errors: txnResp.Errors, Errors: txnResp.Errors,
} }
for _, result := range txnResp.Results { for _, result := range txnResp.Results {
var entry *KVPair kvResp.Results = append(kvResp.Results, result.KV)
if result.KV != nil {
entry = result.KV.DirEnt
}
kvResp.Results = append(kvResp.Results, entry)
} }
return resp.StatusCode == http.StatusOK, &kvResp, wm, nil return resp.StatusCode == http.StatusOK, &kvResp, wm, nil
} }

View File

@ -126,36 +126,32 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
if len(txnResp.Results) != 2 { if len(txnResp.Results) != 2 {
t.Fatalf("bad: %v", txnResp) t.Fatalf("bad: %v", txnResp)
} }
index = txnResp.Results[0].KV.DirEnt.ModifyIndex index = txnResp.Results[0].KV.ModifyIndex
expected := structs.TxnResponse{ expected := structs.TxnResponse{
Results: structs.TxnResults{ Results: structs.TxnResults{
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "key",
Key: "key", Value: nil,
Value: nil, Flags: 23,
Flags: 23, Session: id,
Session: id, LockIndex: 1,
LockIndex: 1, RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: index,
CreateIndex: index, ModifyIndex: index,
ModifyIndex: index,
},
}, },
}, },
}, },
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "key",
Key: "key", Value: []byte("hello world"),
Value: []byte("hello world"), Flags: 23,
Flags: 23, Session: id,
Session: id, LockIndex: 1,
LockIndex: 1, RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: index,
CreateIndex: index, ModifyIndex: index,
ModifyIndex: index,
},
}, },
}, },
}, },
@ -208,32 +204,28 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
if len(txnResp.Results) != 2 { if len(txnResp.Results) != 2 {
t.Fatalf("bad: %v", txnResp) t.Fatalf("bad: %v", txnResp)
} }
modIndex := txnResp.Results[0].KV.DirEnt.ModifyIndex modIndex := txnResp.Results[0].KV.ModifyIndex
expected := structs.TxnResponse{ expected := structs.TxnResponse{
Results: structs.TxnResults{ Results: structs.TxnResults{
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "key",
Key: "key", Value: nil,
Value: nil, Session: id,
Session: id, RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: index,
CreateIndex: index, ModifyIndex: modIndex,
ModifyIndex: modIndex,
},
}, },
}, },
}, },
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "key",
Key: "key", Value: []byte("goodbye world"),
Value: []byte("goodbye world"), Session: id,
Session: id, RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: index,
CreateIndex: index, ModifyIndex: modIndex,
ModifyIndex: modIndex,
},
}, },
}, },
}, },

View File

@ -7,7 +7,7 @@ import (
"github.com/hashicorp/go-memdb" "github.com/hashicorp/go-memdb"
) )
func (s *StateStore) txnKVS(tx *memdb.Txn, idx uint64, op *structs.TxnKVOp) (*structs.TxnKVResult, error) { func (s *StateStore) txnKVS(tx *memdb.Txn, idx uint64, op *structs.TxnKVOp) (structs.TxnKVResult, error) {
var entry *structs.DirEntry var entry *structs.DirEntry
var err error var err error
@ -74,12 +74,12 @@ func (s *StateStore) txnKVS(tx *memdb.Txn, idx uint64, op *structs.TxnKVOp) (*st
// the state store). // the state store).
if entry != nil { if entry != nil {
if op.Verb == structs.KVSGet { if op.Verb == structs.KVSGet {
return &structs.TxnKVResult{entry}, nil return entry, nil
} }
clone := entry.Clone() clone := entry.Clone()
clone.Value = nil clone.Value = nil
return &structs.TxnKVResult{clone}, nil return clone, nil
} }
return nil, nil return nil, nil

View File

@ -177,13 +177,11 @@ func TestStateStore_Txn_KVS(t *testing.T) {
// Make sure the response looks as expected. // Make sure the response looks as expected.
expected := structs.TxnResults{ expected := structs.TxnResults{
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "foo/new",
Key: "foo/new", RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: 8,
CreateIndex: 8, ModifyIndex: 8,
ModifyIndex: 8,
},
}, },
}, },
}, },
@ -191,112 +189,94 @@ func TestStateStore_Txn_KVS(t *testing.T) {
&structs.TxnResult{}, // delete tree &structs.TxnResult{}, // delete tree
&structs.TxnResult{}, // delete CAS &structs.TxnResult{}, // delete CAS
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "foo/update",
Key: "foo/update", Value: []byte("stale"),
Value: []byte("stale"), RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: 5,
CreateIndex: 5, ModifyIndex: 5,
ModifyIndex: 5,
},
}, },
}, },
}, },
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{
Key: "foo/update", Key: "foo/update",
RaftIndex: structs.RaftIndex{ RaftIndex: structs.RaftIndex{
CreateIndex: 5, CreateIndex: 5,
ModifyIndex: 5, ModifyIndex: 5,
},
}, },
}, },
}, },
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "foo/update",
Key: "foo/update", RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: 5,
CreateIndex: 5, ModifyIndex: 8,
ModifyIndex: 8,
},
}, },
}, },
}, },
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "foo/update",
Key: "foo/update", Value: []byte("new"),
Value: []byte("new"), RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: 5,
CreateIndex: 5, ModifyIndex: 8,
ModifyIndex: 8,
},
}, },
}, },
}, },
&structs.TxnResult{}, // get on not/there &structs.TxnResult{}, // get on not/there
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "foo/update",
Key: "foo/update", RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: 5,
CreateIndex: 5, ModifyIndex: 8,
ModifyIndex: 8,
},
}, },
}, },
}, },
&structs.TxnResult{}, // get on foo/lock before it's created &structs.TxnResult{}, // get on foo/lock before it's created
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "foo/lock",
Key: "foo/lock", Session: session,
Session: session, LockIndex: 1,
LockIndex: 1, RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: 8,
CreateIndex: 8, ModifyIndex: 8,
ModifyIndex: 8,
},
}, },
}, },
}, },
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "foo/lock",
Key: "foo/lock", Session: session,
Session: session, LockIndex: 1,
LockIndex: 1, RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: 8,
CreateIndex: 8, ModifyIndex: 8,
ModifyIndex: 8,
},
}, },
}, },
}, },
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "foo/lock",
Key: "foo/lock", LockIndex: 1,
LockIndex: 1, RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: 8,
CreateIndex: 8, ModifyIndex: 8,
ModifyIndex: 8,
},
}, },
}, },
}, },
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "foo/lock",
Key: "foo/lock", LockIndex: 1,
LockIndex: 1, RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: 8,
CreateIndex: 8, ModifyIndex: 8,
ModifyIndex: 8,
},
}, },
}, },
}, },

View File

@ -13,9 +13,7 @@ type TxnKVOp struct {
// TxnKVResult is used to define the result of a single operation on the KVS // TxnKVResult is used to define the result of a single operation on the KVS
// inside a transaction. // inside a transaction.
type TxnKVResult struct { type TxnKVResult *DirEntry
DirEnt *DirEntry
}
// TxnOp is used to define a single operation inside a transaction. Only one // TxnOp is used to define a single operation inside a transaction. Only one
// of the types should be filled out per entry. // of the types should be filled out per entry.
@ -56,7 +54,7 @@ type TxnErrors []*TxnError
// TxnResult is used to define the result of a given operation inside a // TxnResult is used to define the result of a given operation inside a
// transaction. Only one of the types should be filled out per entry. // transaction. Only one of the types should be filled out per entry.
type TxnResult struct { type TxnResult struct {
KV *TxnKVResult KV TxnKVResult
} }
// TxnResults is a list of TxnResult entries. // TxnResults is a list of TxnResult entries.

View File

@ -71,28 +71,24 @@ func TestTxn_Apply(t *testing.T) {
expected := structs.TxnResponse{ expected := structs.TxnResponse{
Results: structs.TxnResults{ Results: structs.TxnResults{
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "test",
Key: "test", Flags: 42,
Flags: 42, Value: nil,
Value: nil, RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: d.CreateIndex,
CreateIndex: d.CreateIndex, ModifyIndex: d.ModifyIndex,
ModifyIndex: d.ModifyIndex,
},
}, },
}, },
}, },
&structs.TxnResult{ &structs.TxnResult{
KV: &structs.TxnKVResult{ KV: &structs.DirEntry{
DirEnt: &structs.DirEntry{ Key: "test",
Key: "test", Flags: 42,
Flags: 42, Value: []byte("test"),
Value: []byte("test"), RaftIndex: structs.RaftIndex{
RaftIndex: structs.RaftIndex{ CreateIndex: d.CreateIndex,
CreateIndex: d.CreateIndex, ModifyIndex: d.ModifyIndex,
ModifyIndex: d.ModifyIndex,
},
}, },
}, },
}, },
@ -315,7 +311,7 @@ func TestTxn_Apply_LockDelay(t *testing.T) {
} }
if len(out.Results) != 1 || if len(out.Results) != 1 ||
len(out.Errors) != 0 || len(out.Errors) != 0 ||
out.Results[0].KV.DirEnt.LockIndex != 2 { out.Results[0].KV.LockIndex != 2 {
t.Fatalf("bad: %v", out) t.Fatalf("bad: %v", out)
} }
} }