state: remove duplication of Query indexer

pull/9947/head
Daniel Nephin 2021-03-29 14:14:20 -04:00
parent 08ee12ab34
commit 341265ec69
3 changed files with 16 additions and 28 deletions

View File

@ -39,7 +39,7 @@ func nodesTableSchema() *memdb.TableSchema {
AllowMissing: false,
Unique: true,
Indexer: indexerSingle{
readIndex: indexFromNodeQuery,
readIndex: indexFromQuery,
writeIndex: indexFromNode,
},
},
@ -77,18 +77,6 @@ func indexFromNode(raw interface{}) ([]byte, error) {
return b.Bytes(), nil
}
// TODO: remove once all uses of indexFromQuery are ported
func indexFromNodeQuery(arg interface{}) ([]byte, error) {
q, ok := arg.(Query)
if !ok {
return nil, fmt.Errorf("unexpected type %T for Query index", arg)
}
var b indexBuilder
b.String(strings.ToLower(q.Value))
return b.Bytes(), nil
}
// servicesTableSchema returns a new table schema used to store information
// about services.
func servicesTableSchema() *memdb.TableSchema {
@ -110,7 +98,7 @@ func servicesTableSchema() *memdb.TableSchema {
AllowMissing: false,
Unique: false,
Indexer: indexerSingle{
readIndex: indexFromNodeQuery,
readIndex: indexFromQuery,
writeIndex: indexFromNodeIdentity,
},
},
@ -225,7 +213,7 @@ func checksTableSchema() *memdb.TableSchema {
AllowMissing: true,
Unique: false,
Indexer: indexerSingle{
readIndex: indexFromNodeQuery,
readIndex: indexFromQuery,
writeIndex: indexFromNodeIdentity,
},
},

View File

@ -21,6 +21,19 @@ func (q Query) NamespaceOrDefault() string {
return q.EnterpriseMeta.NamespaceOrDefault()
}
// indexFromQuery builds an index key where Query.Value is lowercase, and is
// a required value.
func indexFromQuery(arg interface{}) ([]byte, error) {
q, ok := arg.(Query)
if !ok {
return nil, fmt.Errorf("unexpected type %T for Query index", arg)
}
var b indexBuilder
b.String(strings.ToLower(q.Value))
return b.Bytes(), nil
}
// uuidStringToBytes is a modified version of memdb.UUIDFieldIndex.parseString
func uuidStringToBytes(uuid string) ([]byte, error) {
l := len(uuid)

View File

@ -9,19 +9,6 @@ import (
"github.com/hashicorp/consul/agent/structs"
)
// indexFromQuery builds an index key where Query.Value is lowercase, and is
// a required value.
func indexFromQuery(arg interface{}) ([]byte, error) {
q, ok := arg.(Query)
if !ok {
return nil, fmt.Errorf("unexpected type %T for Query index", arg)
}
var b indexBuilder
b.String(strings.ToLower(q.Value))
return b.Bytes(), nil
}
func prefixIndexFromQuery(arg interface{}) ([]byte, error) {
var b indexBuilder
switch v := arg.(type) {