2017-06-29 14:58:59 +00:00
|
|
|
package physical
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2017-08-15 20:44:16 +00:00
|
|
|
"errors"
|
2017-06-29 14:58:59 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-08-15 20:44:16 +00:00
|
|
|
var (
|
|
|
|
ErrRelativePath = errors.New("relative paths not supported")
|
|
|
|
)
|
|
|
|
|
2017-06-29 14:58:59 +00:00
|
|
|
// View represents a prefixed view of a physical backend
|
|
|
|
type View struct {
|
|
|
|
backend Backend
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
2018-01-20 01:44:24 +00:00
|
|
|
// Verify View satisfies the correct interfaces
|
|
|
|
var _ Backend = (*View)(nil)
|
|
|
|
|
2017-06-29 14:58:59 +00:00
|
|
|
// NewView takes an underlying physical backend and returns
|
|
|
|
// a view of it that can only operate with the given prefix.
|
|
|
|
func NewView(backend Backend, prefix string) *View {
|
|
|
|
return &View{
|
|
|
|
backend: backend,
|
|
|
|
prefix: prefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the contents of the prefixed view
|
2018-01-19 06:44:44 +00:00
|
|
|
func (v *View) List(ctx context.Context, prefix string) ([]string, error) {
|
2017-06-29 14:58:59 +00:00
|
|
|
if err := v.sanityCheck(prefix); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
return v.backend.List(ctx, v.expandKey(prefix))
|
2017-06-29 14:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the key of the prefixed view
|
2018-01-19 06:44:44 +00:00
|
|
|
func (v *View) Get(ctx context.Context, key string) (*Entry, error) {
|
2017-06-29 14:58:59 +00:00
|
|
|
if err := v.sanityCheck(key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err := v.backend.Get(ctx, v.expandKey(key))
|
2017-06-29 14:58:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if entry != nil {
|
|
|
|
entry.Key = v.truncateKey(entry.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Entry{
|
|
|
|
Key: entry.Key,
|
|
|
|
Value: entry.Value,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the entry into the prefix view
|
2018-01-19 06:44:44 +00:00
|
|
|
func (v *View) Put(ctx context.Context, entry *Entry) error {
|
2017-06-29 14:58:59 +00:00
|
|
|
if err := v.sanityCheck(entry.Key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nested := &Entry{
|
|
|
|
Key: v.expandKey(entry.Key),
|
|
|
|
Value: entry.Value,
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
return v.backend.Put(ctx, nested)
|
2017-06-29 14:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the entry from the prefix view
|
2018-01-19 06:44:44 +00:00
|
|
|
func (v *View) Delete(ctx context.Context, key string) error {
|
2017-06-29 14:58:59 +00:00
|
|
|
if err := v.sanityCheck(key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
return v.backend.Delete(ctx, v.expandKey(key))
|
2017-06-29 14:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sanityCheck is used to perform a sanity check on a key
|
|
|
|
func (v *View) sanityCheck(key string) error {
|
|
|
|
if strings.Contains(key, "..") {
|
2017-08-15 20:44:16 +00:00
|
|
|
return ErrRelativePath
|
2017-06-29 14:58:59 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// expandKey is used to expand to the full key path with the prefix
|
|
|
|
func (v *View) expandKey(suffix string) string {
|
|
|
|
return v.prefix + suffix
|
|
|
|
}
|
|
|
|
|
|
|
|
// truncateKey is used to remove the prefix of the key
|
|
|
|
func (v *View) truncateKey(full string) string {
|
|
|
|
return strings.TrimPrefix(full, v.prefix)
|
|
|
|
}
|