Removes null results for deletes, and preps for more than one result from an operation.
This commit is contained in:
parent
2649a6336e
commit
5fd99b13ef
|
@ -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.
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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/<key>`
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue