From fbfb90a694f821936f8a84680b3da68c1bfb8a8c Mon Sep 17 00:00:00 2001 From: James Phillips Date: Fri, 13 May 2016 01:47:55 -0700 Subject: [PATCH] Removes null results for deletes, and preps for more than one result from an operation. --- consul/state/txn.go | 14 ++++++++------ consul/state/txn_test.go | 6 ------ website/source/docs/agent/http/kv.html.markdown | 7 +++---- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/consul/state/txn.go b/consul/state/txn.go index 43ad8b8c03..896b73f7f0 100644 --- a/consul/state/txn.go +++ b/consul/state/txn.go @@ -8,7 +8,7 @@ import ( ) // txnKVS handles all KV-related operations. -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.TxnResults, error) { var entry *structs.DirEntry var err error @@ -78,12 +78,14 @@ func (s *StateStore) txnKVS(tx *memdb.Txn, idx uint64, op *structs.TxnKVOp) (str // the state store). if entry != nil { if op.Verb == structs.KVSGet { - return entry, nil + result := structs.TxnResult{KV: entry} + return structs.TxnResults{&result}, nil } clone := entry.Clone() clone.Value = nil - return clone, nil + result := structs.TxnResult{KV: clone} + return structs.TxnResults{&result}, nil } return nil, nil @@ -94,18 +96,18 @@ func (s *StateStore) txnDispatch(tx *memdb.Txn, idx uint64, ops structs.TxnOps) results := make(structs.TxnResults, 0, len(ops)) errors := make(structs.TxnErrors, 0, len(ops)) for i, op := range ops { - var result structs.TxnResult + var ret structs.TxnResults var err error // Dispatch based on the type of operation. if op.KV != nil { - result.KV, err = s.txnKVS(tx, idx, op.KV) + ret, err = s.txnKVS(tx, idx, op.KV) } else { err = fmt.Errorf("no operation specified") } // Accumulate the results. - results = append(results, &result) + results = append(results, ret...) // Capture any error along with the index of the operation that // failed. diff --git a/consul/state/txn_test.go b/consul/state/txn_test.go index c8c14bda7b..dda793211b 100644 --- a/consul/state/txn_test.go +++ b/consul/state/txn_test.go @@ -154,9 +154,6 @@ func TestStateStore_Txn_KVS(t *testing.T) { if len(errors) > 0 { t.Fatalf("err: %v", errors) } - if len(results) != len(ops) { - t.Fatalf("bad len: %d != %d", len(results), len(ops)) - } // Make sure the response looks as expected. expected := structs.TxnResults{ @@ -169,9 +166,6 @@ func TestStateStore_Txn_KVS(t *testing.T) { }, }, }, - &structs.TxnResult{}, // delete - &structs.TxnResult{}, // delete tree - &structs.TxnResult{}, // delete CAS &structs.TxnResult{ KV: &structs.DirEntry{ Key: "foo/update", diff --git a/website/source/docs/agent/http/kv.html.markdown b/website/source/docs/agent/http/kv.html.markdown index 09fa796a77..9b41134972 100644 --- a/website/source/docs/agent/http/kv.html.markdown +++ b/website/source/docs/agent/http/kv.html.markdown @@ -352,11 +352,10 @@ back. If either of these status codes are returned, the response will look like } ``` -`Results` has an entry for each operation if the transaction was successful. To save +`Results` has entries for some operations if the transaction was successful. To save space, the `Value` will be `null` for any `Verb` other than "get". Like the `/v1/kv/` -endpoint, `Value` will be Base64-encoded if it is present. For verbs that delete -keys, a `null` result entry will be present, keeping the list of `Results` 1:1 with the -list of operations given in the transaction. +endpoint, `Value` will be Base64-encoded if it is present. Also, no result entries will be +added for verbs that delete keys. `Errors` has entries describing which operations failed if the transaction was rolled back. The `OpIndex` gives the index of the failed operation in the transaction, and