Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix Scans for Storage Keys #35

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,15 @@ func (a *Auth) CheckPermission(scope, userID string) error {
func (a *Auth) Users() ([]string, error) {
a.logger.Debug("Retrieving all user IDs from storage")

keys, err := a.store.Keys()
keys, err := a.store.KeysWithPrefix(permissionKeyPrefix)
if err != nil {
return nil, errors.Wrap(err, "failed to load permissions")
}

var userIDs []string
for _, key := range keys {
if strings.HasPrefix(key, permissionKeyPrefix) {
userID := strings.TrimPrefix(key, permissionKeyPrefix)
userIDs = append(userIDs, userID)
}
userID := strings.TrimPrefix(key, permissionKeyPrefix)
userIDs = append(userIDs, userID)
}

return userIDs, nil
Expand Down
34 changes: 34 additions & 0 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package joe
import (
"encoding/json"
"sort"
"strings"
"sync"

"github.com/pkg/errors"
Expand Down Expand Up @@ -34,6 +35,14 @@ type Memory interface {
Close() error
}

// PrefixAwareMemory is an optional interface for Memory to implement if the
// backing storage has a higher performance way to search keys matching a given
// prefix. If the Memory does not implement this interface, we will iterate
// over the keys given by Keys().
type PrefixAwareMemory interface {
KeysWithPrefix(prefix string) ([]string, error)
}

// A MemoryEncoder is used to encode and decode any values that are stored in
// the Memory. The default implementation that is used by the Storage uses a
// JSON encoding.
Expand Down Expand Up @@ -83,6 +92,31 @@ func (s *Storage) Keys() ([]string, error) {
return keys, err
}

// KeysWithPrefix returns all keys matching a given prefix.
func (s *Storage) KeysWithPrefix(prefix string) ([]string, error) {
if m, ok := s.memory.(PrefixAwareMemory); ok {
s.mu.RLock()
keys, err := m.KeysWithPrefix(prefix)
s.mu.RUnlock()

sort.Strings(keys)
return keys, err
}

keys, err := s.memory.Keys()
if err != nil {
return nil, err
}
var results []string
for _, k := range keys {
if strings.HasPrefix(k, prefix) {
results = append(results, k)
}
}
sort.Strings(results)
return results, nil
}

// Set encodes the given data and stores it in the Memory that is managed by the
// Storage.
func (s *Storage) Set(key string, value interface{}) error {
Expand Down
62 changes: 62 additions & 0 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package joe
import (
"bytes"
"encoding/gob"
"go.uber.org/zap"
dwmunster marked this conversation as resolved.
Show resolved Hide resolved
"sort"
"strings"
"testing"

"github.com/pkg/errors"
Expand Down Expand Up @@ -121,3 +124,62 @@ func (e gobEncoder) Decode(data []byte, target interface{}) error {
enc := gob.NewDecoder(bytes.NewBuffer(data))
return enc.Decode(target)
}

type prefixAwareMemory struct {
*inMemory
prefixUsed bool
}

func (m *prefixAwareMemory) KeysWithPrefix(prefix string) ([]string, error) {
m.prefixUsed = true
keys, err := m.Keys()
if err != nil {
return nil, err
}
var results []string
for _, k := range keys {
if strings.HasPrefix(k, prefix) {
results = append(results, k)
}
}
sort.Strings(results)
return results, nil
}

func newPrefixAwareStorage(logger *zap.Logger) *Storage {
return &Storage{
logger: logger,
memory: &prefixAwareMemory{newInMemory(), false},
encoder: new(jsonEncoder),
}
}

var _ PrefixAwareMemory = &prefixAwareMemory{nil, false}

func TestStorage_PrefixAware(t *testing.T) {
logger := zaptest.NewLogger(t)
store := newPrefixAwareStorage(logger)

testEntries := []string{
"test.k3",
"test.k1",
"non-matching",
"test.k2",
}

for _, k := range testEntries {
err := store.Set(k, nil)
require.NoError(t, err)
}

expectedKeys := []string{
"test.k1",
"test.k2",
"test.k3",
}
actualKeys, err := store.KeysWithPrefix("test.")
require.NoError(t, err)

assert.Equal(t, expectedKeys, actualKeys)
assert.True(t, store.memory.(*prefixAwareMemory).prefixUsed)
}