2016-05-11 08:35:27 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
2018-10-18 12:45:37 +00:00
|
|
|
"errors"
|
2016-05-11 08:35:27 +00:00
|
|
|
"fmt"
|
2017-04-19 23:00:11 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2018-10-18 12:45:37 +00:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2016-05-11 08:35:27 +00:00
|
|
|
)
|
|
|
|
|
2016-05-11 17:58:27 +00:00
|
|
|
// TxnKVOp is used to define a single operation on the KVS inside a
|
2016-05-11 08:35:27 +00:00
|
|
|
// transaction
|
2016-05-11 17:58:27 +00:00
|
|
|
type TxnKVOp struct {
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb api.KVOp
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt DirEntry
|
|
|
|
}
|
|
|
|
|
2016-05-11 17:58:27 +00:00
|
|
|
// TxnKVResult is used to define the result of a single operation on the KVS
|
2016-05-11 08:35:27 +00:00
|
|
|
// inside a transaction.
|
2016-05-11 20:48:03 +00:00
|
|
|
type TxnKVResult *DirEntry
|
2016-05-11 08:35:27 +00:00
|
|
|
|
2018-10-18 12:45:37 +00:00
|
|
|
// TxnKVOp is used to define a single operation on an Intention inside a
|
|
|
|
// transaction.
|
|
|
|
type TxnIntentionOp IntentionRequest
|
|
|
|
|
2016-05-11 08:35:27 +00:00
|
|
|
// TxnOp is used to define a single operation inside a transaction. Only one
|
|
|
|
// of the types should be filled out per entry.
|
|
|
|
type TxnOp struct {
|
2018-10-18 12:45:37 +00:00
|
|
|
KV *TxnKVOp
|
|
|
|
Intention *TxnIntentionOp
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TxnOps is a list of operations within a transaction.
|
|
|
|
type TxnOps []*TxnOp
|
|
|
|
|
|
|
|
// TxnRequest is used to apply multiple operations to the state store in a
|
|
|
|
// single transaction
|
|
|
|
type TxnRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Ops TxnOps
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *TxnRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:38:25 +00:00
|
|
|
// TxnReadRequest is used as a fast path for read-only transactions that don't
|
|
|
|
// modify the state store.
|
|
|
|
type TxnReadRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Ops TxnOps
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *TxnReadRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2016-05-11 08:35:27 +00:00
|
|
|
// TxnError is used to return information about an error for a specific
|
|
|
|
// operation.
|
|
|
|
type TxnError struct {
|
|
|
|
OpIndex int
|
|
|
|
What string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the string representation of an atomic error.
|
|
|
|
func (e TxnError) Error() string {
|
|
|
|
return fmt.Sprintf("op %d: %s", e.OpIndex, e.What)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxnErrors is a list of TxnError entries.
|
|
|
|
type TxnErrors []*TxnError
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
type TxnResult struct {
|
2016-05-11 20:48:03 +00:00
|
|
|
KV TxnKVResult
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TxnResults is a list of TxnResult entries.
|
|
|
|
type TxnResults []*TxnResult
|
|
|
|
|
|
|
|
// TxnResponse is the structure returned by a TxnRequest.
|
|
|
|
type TxnResponse struct {
|
|
|
|
Results TxnResults
|
|
|
|
Errors TxnErrors
|
|
|
|
}
|
2016-05-13 00:38:25 +00:00
|
|
|
|
2018-10-18 12:45:37 +00:00
|
|
|
// Error returns an aggregate of all errors in this TxnResponse.
|
|
|
|
func (r TxnResponse) Error() error {
|
|
|
|
var errs error
|
|
|
|
for _, err := range r.Errors {
|
|
|
|
errs = multierror.Append(errs, errors.New(err.Error()))
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:38:25 +00:00
|
|
|
// TxnReadResponse is the structure returned by a TxnReadRequest.
|
|
|
|
type TxnReadResponse struct {
|
|
|
|
TxnResponse
|
|
|
|
QueryMeta
|
|
|
|
}
|