2021-02-11 00:00:13 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-08-17 18:29:39 +00:00
|
|
|
"encoding/binary"
|
2021-02-11 00:00:13 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-11-24 14:10:38 +00:00
|
|
|
"strings"
|
2021-09-13 18:37:16 +00:00
|
|
|
"time"
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2021-09-13 18:37:16 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2021-02-11 00:00:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// indexerSingle implements both memdb.Indexer and memdb.SingleIndexer. It may
|
|
|
|
// be used in a memdb.IndexSchema to specify functions that generate the index
|
|
|
|
// value for memdb.Txn operations.
|
2022-06-23 15:07:19 +00:00
|
|
|
//
|
|
|
|
// R represents the type used to generate the read index.
|
|
|
|
// W represents the type used to generate the write index.
|
|
|
|
type indexerSingle[R, W any] struct {
|
2021-02-11 00:00:13 +00:00
|
|
|
// readIndex is used by memdb for Txn.Get, Txn.First, and other operations
|
|
|
|
// that read data.
|
2022-06-23 15:07:19 +00:00
|
|
|
readIndex[R]
|
2021-02-11 00:00:13 +00:00
|
|
|
// writeIndex is used by memdb for Txn.Insert, Txn.Delete, for operations
|
|
|
|
// that write data to the index.
|
2022-06-23 15:07:19 +00:00
|
|
|
writeIndex[W]
|
2021-02-11 00:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// indexerMulti implements both memdb.Indexer and memdb.MultiIndexer. It may
|
|
|
|
// be used in a memdb.IndexSchema to specify functions that generate the index
|
|
|
|
// value for memdb.Txn operations.
|
2022-06-23 15:07:19 +00:00
|
|
|
//
|
|
|
|
// R represents the type used to generate the read index.
|
|
|
|
// W represents the type used to generate the write index.
|
|
|
|
type indexerMulti[R, W any] struct {
|
2021-02-11 00:00:13 +00:00
|
|
|
// readIndex is used by memdb for Txn.Get, Txn.First, and other operations
|
|
|
|
// that read data.
|
2022-06-23 15:07:19 +00:00
|
|
|
readIndex[R]
|
2021-02-11 00:00:13 +00:00
|
|
|
// writeIndexMulti is used by memdb for Txn.Insert, Txn.Delete, for operations
|
|
|
|
// that write data to the index.
|
2022-06-23 15:07:19 +00:00
|
|
|
writeIndexMulti[W]
|
2021-02-11 00:00:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 18:37:17 +00:00
|
|
|
// indexerSingleWithPrefix is a indexerSingle which also supports prefix queries.
|
2022-06-23 15:07:19 +00:00
|
|
|
//
|
|
|
|
// R represents the type used to generate the read index.
|
|
|
|
// W represents the type used to generate the write index.
|
|
|
|
// P represents the type used to generate the prefix index.
|
|
|
|
type indexerSingleWithPrefix[R, W, P any] struct {
|
|
|
|
readIndex[R]
|
|
|
|
writeIndex[W]
|
|
|
|
prefixIndex[P]
|
2021-03-10 18:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 00:00:13 +00:00
|
|
|
// readIndex implements memdb.Indexer. It exists so that a function can be used
|
|
|
|
// to provide the interface.
|
|
|
|
//
|
|
|
|
// Unlike memdb.Indexer, a readIndex function accepts only a single argument. To
|
|
|
|
// generate an index from multiple values, use a struct type with multiple fields.
|
2022-06-23 15:07:19 +00:00
|
|
|
type readIndex[R any] func(arg R) ([]byte, error)
|
2021-02-11 00:00:13 +00:00
|
|
|
|
2022-06-23 15:07:19 +00:00
|
|
|
func (f readIndex[R]) FromArgs(args ...interface{}) ([]byte, error) {
|
2021-02-11 00:00:13 +00:00
|
|
|
if len(args) != 1 {
|
|
|
|
return nil, fmt.Errorf("index supports only a single arg")
|
|
|
|
}
|
2022-06-23 15:07:19 +00:00
|
|
|
arg, ok := args[0].(R)
|
|
|
|
if !ok {
|
|
|
|
var typ R
|
|
|
|
return nil, fmt.Errorf("unexpected type %T, does not implement %T", args[0], typ)
|
|
|
|
}
|
|
|
|
return f(arg)
|
2021-02-11 00:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var errMissingValueForIndex = fmt.Errorf("object is missing a value for this index")
|
|
|
|
|
2021-02-12 21:52:09 +00:00
|
|
|
// writeIndex implements memdb.SingleIndexer. It exists so that a function
|
2021-02-11 00:00:13 +00:00
|
|
|
// can be used to provide this interface.
|
|
|
|
//
|
|
|
|
// Instead of a bool return value, writeIndex expects errMissingValueForIndex to
|
|
|
|
// indicate that an index could not be build for the object. It will translate
|
|
|
|
// this error into a false value to satisfy the memdb.SingleIndexer interface.
|
2022-06-23 15:07:19 +00:00
|
|
|
type writeIndex[W any] func(raw W) ([]byte, error)
|
2021-02-11 00:00:13 +00:00
|
|
|
|
2022-06-23 15:07:19 +00:00
|
|
|
func (f writeIndex[W]) FromObject(raw interface{}) (bool, []byte, error) {
|
|
|
|
obj, ok := raw.(W)
|
|
|
|
if !ok {
|
|
|
|
var typ W
|
|
|
|
return false, nil, fmt.Errorf("unexpected type %T, does not implement %T", raw, typ)
|
|
|
|
}
|
|
|
|
v, err := f(obj)
|
2021-02-11 00:00:13 +00:00
|
|
|
if errors.Is(err, errMissingValueForIndex) {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
return err == nil, v, err
|
|
|
|
}
|
|
|
|
|
2021-02-12 21:52:09 +00:00
|
|
|
// writeIndexMulti implements memdb.MultiIndexer. It exists so that a function
|
2021-02-11 00:00:13 +00:00
|
|
|
// can be used to provide this interface.
|
|
|
|
//
|
|
|
|
// Instead of a bool return value, writeIndexMulti expects errMissingValueForIndex to
|
|
|
|
// indicate that an index could not be build for the object. It will translate
|
|
|
|
// this error into a false value to satisfy the memdb.MultiIndexer interface.
|
2022-06-23 15:07:19 +00:00
|
|
|
type writeIndexMulti[W any] func(raw W) ([][]byte, error)
|
2021-02-11 00:00:13 +00:00
|
|
|
|
2022-06-23 15:07:19 +00:00
|
|
|
func (f writeIndexMulti[W]) FromObject(raw interface{}) (bool, [][]byte, error) {
|
|
|
|
obj, ok := raw.(W)
|
|
|
|
if !ok {
|
|
|
|
var typ W
|
|
|
|
return false, nil, fmt.Errorf("unexpected type %T, does not implement %T", raw, typ)
|
|
|
|
}
|
|
|
|
v, err := f(obj)
|
2021-02-11 00:00:13 +00:00
|
|
|
if errors.Is(err, errMissingValueForIndex) {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
return err == nil, v, err
|
|
|
|
}
|
|
|
|
|
2021-03-10 18:37:17 +00:00
|
|
|
// prefixIndex implements memdb.PrefixIndexer. It exists so that a function
|
|
|
|
// can be used to provide this interface.
|
2022-06-23 15:07:19 +00:00
|
|
|
type prefixIndex[P any] func(args P) ([]byte, error)
|
2021-03-10 18:37:17 +00:00
|
|
|
|
2022-06-23 15:07:19 +00:00
|
|
|
func (f prefixIndex[P]) PrefixFromArgs(args ...interface{}) ([]byte, error) {
|
2021-03-10 18:37:17 +00:00
|
|
|
if len(args) != 1 {
|
|
|
|
return nil, fmt.Errorf("index supports only a single arg")
|
|
|
|
}
|
2022-06-23 15:07:19 +00:00
|
|
|
arg, ok := args[0].(P)
|
|
|
|
if !ok {
|
|
|
|
var typ P
|
|
|
|
return nil, fmt.Errorf("unexpected type %T, does not implement %T", args[0], typ)
|
|
|
|
}
|
|
|
|
return f(arg)
|
2021-03-10 18:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 00:00:13 +00:00
|
|
|
const null = "\x00"
|
|
|
|
|
|
|
|
// indexBuilder is a buffer used to construct memdb index values.
|
|
|
|
type indexBuilder bytes.Buffer
|
|
|
|
|
2021-03-29 17:01:31 +00:00
|
|
|
func newIndexBuilder(cap int) *indexBuilder {
|
|
|
|
buff := make([]byte, 0, cap)
|
|
|
|
b := bytes.NewBuffer(buff)
|
|
|
|
return (*indexBuilder)(b)
|
|
|
|
}
|
|
|
|
|
2021-02-11 00:00:13 +00:00
|
|
|
// String appends the string and a null terminator to the buffer.
|
|
|
|
func (b *indexBuilder) String(v string) {
|
|
|
|
(*bytes.Buffer)(b).WriteString(v)
|
|
|
|
(*bytes.Buffer)(b).WriteString(null)
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
func (b *indexBuilder) Int64(v int64) {
|
|
|
|
const size = binary.MaxVarintLen64
|
|
|
|
|
|
|
|
// Get the value and encode it
|
|
|
|
buf := make([]byte, size)
|
|
|
|
binary.PutVarint(buf, v)
|
|
|
|
b.Raw(buf)
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:35:16 +00:00
|
|
|
// Raw appends the bytes without a null terminator to the buffer. Raw should
|
|
|
|
// only be used when v has a fixed length, or when building the last segment of
|
|
|
|
// a prefix index.
|
|
|
|
func (b *indexBuilder) Raw(v []byte) {
|
|
|
|
(*bytes.Buffer)(b).Write(v)
|
|
|
|
}
|
|
|
|
|
2021-02-11 00:00:13 +00:00
|
|
|
func (b *indexBuilder) Bytes() []byte {
|
|
|
|
return (*bytes.Buffer)(b).Bytes()
|
|
|
|
}
|
2021-09-13 15:53:00 +00:00
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
// singleValueID is an interface that may be implemented by any type that should
|
|
|
|
// be indexed by a single ID and a structs.EnterpriseMeta to scope the ID.
|
|
|
|
type singleValueID interface {
|
|
|
|
IDValue() string
|
|
|
|
PartitionOrDefault() string
|
|
|
|
NamespaceOrDefault() string
|
|
|
|
}
|
|
|
|
|
2021-11-24 14:10:38 +00:00
|
|
|
type multiValueID interface {
|
|
|
|
IDValue() []string
|
|
|
|
PartitionOrDefault() string
|
|
|
|
NamespaceOrDefault() string
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
var _ singleValueID = (*structs.DirEntry)(nil)
|
|
|
|
var _ singleValueID = (*Tombstone)(nil)
|
|
|
|
var _ singleValueID = (*Query)(nil)
|
2021-11-08 21:20:50 +00:00
|
|
|
var _ singleValueID = (*structs.Session)(nil)
|
2021-11-08 14:35:56 +00:00
|
|
|
|
2021-11-24 14:10:38 +00:00
|
|
|
// indexFromIDValue creates an index key from any struct that implements singleValueID
|
2022-06-23 15:07:19 +00:00
|
|
|
func indexFromIDValueLowerCase(e singleValueID) ([]byte, error) {
|
2021-11-24 14:10:38 +00:00
|
|
|
v := strings.ToLower(e.IDValue())
|
|
|
|
if v == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(v)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// indexFromIDValue creates an index key from any struct that implements singleValueID
|
2022-06-23 15:07:19 +00:00
|
|
|
func indexFromMultiValueID(e multiValueID) ([]byte, error) {
|
2021-11-24 14:10:38 +00:00
|
|
|
var b indexBuilder
|
|
|
|
for _, v := range e.IDValue() {
|
|
|
|
if v == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
b.String(strings.ToLower(v))
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:53:00 +00:00
|
|
|
func (b *indexBuilder) Bool(v bool) {
|
|
|
|
b.Raw([]byte{intFromBool(v)})
|
|
|
|
}
|
2021-09-13 18:37:16 +00:00
|
|
|
|
|
|
|
type TimeQuery struct {
|
|
|
|
Value time.Time
|
2022-04-05 21:10:06 +00:00
|
|
|
acl.EnterpriseMeta
|
2021-09-13 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NamespaceOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q TimeQuery) NamespaceOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.NamespaceOrDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartitionOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q TimeQuery) PartitionOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.PartitionOrDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *indexBuilder) Time(t time.Time) {
|
|
|
|
val := t.Unix()
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(buf, uint64(val))
|
|
|
|
(*bytes.Buffer)(b).Write(buf)
|
|
|
|
}
|