Skip to content

Commit

Permalink
Move external backend key logic to receiver methods of backend.Key (#…
Browse files Browse the repository at this point in the history
…45985)

There are a number of places that the backend.Key is assumed to
be a []byte and is passed to various functions from the `bytes`
package. This creates equivalent functionality on the backend.Key
type so that all assumptions about the backend key type can be
removed.
  • Loading branch information
rosstimothy authored Sep 3, 2024
1 parent c175620 commit c0241f3
Show file tree
Hide file tree
Showing 24 changed files with 673 additions and 155 deletions.
27 changes: 2 additions & 25 deletions lib/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
package backend

import (
"bytes"
"context"
"fmt"
"io"
"sort"
"strings"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -219,7 +217,7 @@ type Watch struct {
// String returns a user-friendly description
// of the watcher
func (w *Watch) String() string {
return fmt.Sprintf("Watcher(name=%v, prefixes=%v)", w.Name, string(bytes.Join(w.Prefixes, []byte(", "))))
return fmt.Sprintf("Watcher(name=%v, prefixes=%v)", w.Name, w.Prefixes)
}

// Watcher returns watcher
Expand Down Expand Up @@ -380,7 +378,7 @@ func (it Items) Swap(i, j int) {

// Less is part of sort.Interface.
func (it Items) Less(i, j int) bool {
return bytes.Compare(it[i].Key, it[j].Key) < 0
return it[i].Key.Compare(it[j].Key) < 0
}

// TTL returns TTL in duration units, rounds up to one second
Expand Down Expand Up @@ -431,27 +429,6 @@ func (p earliest) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}

// Separator is used as a separator between key parts
const Separator = '/'

// NewKey joins parts into path separated by Separator,
// makes sure path always starts with Separator ("/")
func NewKey(parts ...string) Key {
return internalKey("", parts...)
}

// ExactKey is like Key, except a Separator is appended to the result
// path of Key. This is to ensure range matching of a path will only
// math child paths and not other paths that have the resulting path
// as a prefix.
func ExactKey(parts ...string) Key {
return append(NewKey(parts...), Separator)
}

func internalKey(internalPrefix string, parts ...string) Key {
return Key(strings.Join(append([]string{internalPrefix}, parts...), string(Separator)))
}

// CreateRevision generates a new identifier to be used
// as a resource revision. Backend implementations that provide
// their own mechanism for versioning resources should be
Expand Down
2 changes: 1 addition & 1 deletion lib/backend/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ type BufferWatcher struct {
// String returns user-friendly representation
// of the buffer watcher
func (w *BufferWatcher) String() string {
return fmt.Sprintf("Watcher(name=%v, prefixes=%v, capacity=%v, size=%v)", w.Name, string(bytes.Join(w.Prefixes, []byte(", "))), w.capacity, len(w.eventsC))
return fmt.Sprintf("Watcher(name=%v, prefixes=%v, capacity=%v, size=%v)", w.Name, w.Prefixes, w.capacity, len(w.eventsC))
}

// Events returns events channel. This method performs internal work and should be re-called after each event
Expand Down
3 changes: 1 addition & 2 deletions lib/backend/dynamo/dynamodbbk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package dynamo

import (
"bytes"
"context"
"errors"
"net/http"
Expand Down Expand Up @@ -638,7 +637,7 @@ func (b *Backend) CompareAndSwap(ctx context.Context, expected backend.Item, rep
if len(replaceWith.Key) == 0 {
return nil, trace.BadParameter("missing parameter Key")
}
if !bytes.Equal(expected.Key, replaceWith.Key) {
if expected.Key.Compare(replaceWith.Key) != 0 {
return nil, trace.BadParameter("expected and replaceWith keys should match")
}

Expand Down
5 changes: 2 additions & 3 deletions lib/backend/etcdbk/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package etcdbk

import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
Expand Down Expand Up @@ -797,7 +796,7 @@ func (b *EtcdBackend) CompareAndSwap(ctx context.Context, expected backend.Item,
if len(replaceWith.Key) == 0 {
return nil, trace.BadParameter("missing parameter Key")
}
if !bytes.Equal(expected.Key, replaceWith.Key) {
if expected.Key.Compare(replaceWith.Key) != 0 {
return nil, trace.BadParameter("expected and replaceWith keys should match")
}
var opts []clientv3.OpOption
Expand Down Expand Up @@ -1108,7 +1107,7 @@ func fromType(eventType mvccpb.Event_EventType) types.OpType {
}

func (b *EtcdBackend) trimPrefix(in backend.Key) backend.Key {
return bytes.TrimPrefix(in, backend.Key(b.cfg.Key))
return in.TrimPrefix(backend.Key(b.cfg.Key))
}

func (b *EtcdBackend) prependPrefix(in backend.Key) string {
Expand Down
2 changes: 1 addition & 1 deletion lib/backend/firestore/firestorebk.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func (b *Backend) CompareAndSwap(ctx context.Context, expected backend.Item, rep
if len(replaceWith.Key) == 0 {
return nil, trace.BadParameter("missing parameter Key")
}
if !bytes.Equal(expected.Key, replaceWith.Key) {
if expected.Key.Compare(replaceWith.Key) != 0 {
return nil, trace.BadParameter("expected and replaceWith keys should match")
}

Expand Down
90 changes: 89 additions & 1 deletion lib/backend/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,93 @@

package backend

import (
"bytes"
"fmt"
"strings"
)

// Key is the unique identifier for an [Item].
type Key = []byte
type Key []byte

// Separator is used as a separator between key parts
const Separator = '/'

// NewKey joins parts into path separated by Separator,
// makes sure path always starts with Separator ("/")
func NewKey(parts ...string) Key {
return internalKey("", parts...)
}

// ExactKey is like Key, except a Separator is appended to the result
// path of Key. This is to ensure range matching of a path will only
// math child paths and not other paths that have the resulting path
// as a prefix.
func ExactKey(parts ...string) Key {
return append(NewKey(parts...), Separator)
}

func internalKey(internalPrefix string, parts ...string) Key {
return Key(strings.Join(append([]string{internalPrefix}, parts...), string(Separator)))
}

// String returns the textual representation of the key with
// each component concatenated together via the [Separator].
func (k Key) String() string {
return string(k)
}

// HasPrefix reports whether the key begins with prefix.
func (k Key) HasPrefix(prefix Key) bool {
return bytes.HasPrefix(k, prefix)
}

// TrimPrefix returns the key without the provided leading prefix string.
// If the key doesn't start with prefix, it is returned unchanged.
func (k Key) TrimPrefix(prefix Key) Key {
return bytes.TrimPrefix(k, prefix)
}

func (k Key) PrependPrefix(p Key) Key {
return append(p, k...)
}

// HasSuffix reports whether the key ends with suffix.
func (k Key) HasSuffix(suffix Key) bool {
return bytes.HasSuffix(k, suffix)
}

// TrimSuffix returns the key without the provided trailing suffix string.
// If the key doesn't end with suffix, it is returned unchanged.
func (k Key) TrimSuffix(suffix Key) Key {
return bytes.TrimSuffix(k, suffix)
}

func (k Key) Components() [][]byte {
if len(k) == 0 {
return nil
}

sep := []byte{Separator}
return bytes.Split(bytes.TrimPrefix(k, sep), sep)
}

func (k Key) Compare(o Key) int {
return bytes.Compare(k, o)
}

// Scan implement sql.Scanner, allowing a [Key] to
// be directly retrieved from sql backends without
// an intermediary object.
func (k *Key) Scan(scan any) error {
switch key := scan.(type) {
case []byte:
*k = bytes.Clone(key)
case string:
*k = []byte(strings.Clone(key))
default:
return fmt.Errorf("invalid Key type %T", scan)
}

return nil
}
Loading

0 comments on commit c0241f3

Please sign in to comment.