mirror of https://github.com/hashicorp/consul
Removes null results for deletes, and preps for more than one result from an operation.
parent
a37bf9de56
commit
fbfb90a694
|
@ -8,7 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// txnKVS handles all KV-related operations.
|
// 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 entry *structs.DirEntry
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -78,12 +78,14 @@ func (s *StateStore) txnKVS(tx *memdb.Txn, idx uint64, op *structs.TxnKVOp) (str
|
||||||
// the state store).
|
// the state store).
|
||||||
if entry != nil {
|
if entry != nil {
|
||||||
if op.Verb == structs.KVSGet {
|
if op.Verb == structs.KVSGet {
|
||||||
return entry, nil
|
result := structs.TxnResult{KV: entry}
|
||||||
|
return structs.TxnResults{&result}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
clone := entry.Clone()
|
clone := entry.Clone()
|
||||||
clone.Value = nil
|
clone.Value = nil
|
||||||
return clone, nil
|
result := structs.TxnResult{KV: clone}
|
||||||
|
return structs.TxnResults{&result}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, 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))
|
results := make(structs.TxnResults, 0, len(ops))
|
||||||
errors := make(structs.TxnErrors, 0, len(ops))
|
errors := make(structs.TxnErrors, 0, len(ops))
|
||||||
for i, op := range ops {
|
for i, op := range ops {
|
||||||
var result structs.TxnResult
|
var ret structs.TxnResults
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Dispatch based on the type of operation.
|
// Dispatch based on the type of operation.
|
||||||
if op.KV != nil {
|
if op.KV != nil {
|
||||||
result.KV, err = s.txnKVS(tx, idx, op.KV)
|
ret, err = s.txnKVS(tx, idx, op.KV)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("no operation specified")
|
err = fmt.Errorf("no operation specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accumulate the results.
|
// Accumulate the results.
|
||||||
results = append(results, &result)
|
results = append(results, ret...)
|
||||||
|
|
||||||
// Capture any error along with the index of the operation that
|
// Capture any error along with the index of the operation that
|
||||||
// failed.
|
// failed.
|
||||||
|
|
|
@ -154,9 +154,6 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
if len(errors) > 0 {
|
if len(errors) > 0 {
|
||||||
t.Fatalf("err: %v", errors)
|
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.
|
// Make sure the response looks as expected.
|
||||||
expected := structs.TxnResults{
|
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{
|
&structs.TxnResult{
|
||||||
KV: &structs.DirEntry{
|
KV: &structs.DirEntry{
|
||||||
Key: "foo/update",
|
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>`
|
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
|
endpoint, `Value` will be Base64-encoded if it is present. Also, no result entries will be
|
||||||
keys, a `null` result entry will be present, keeping the list of `Results` 1:1 with the
|
added for verbs that delete keys.
|
||||||
list of operations given in the transaction.
|
|
||||||
|
|
||||||
`Errors` has entries describing which operations failed if the transaction was rolled
|
`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
|
back. The `OpIndex` gives the index of the failed operation in the transaction, and
|
||||||
|
|
Loading…
Reference in New Issue