2015-03-15 21:26:48 +00:00
|
|
|
package logical
|
|
|
|
|
|
|
|
import (
|
2017-07-26 21:59:14 +00:00
|
|
|
"strings"
|
2015-03-15 21:26:48 +00:00
|
|
|
"sync"
|
2016-06-06 16:03:08 +00:00
|
|
|
|
2017-07-26 21:59:14 +00:00
|
|
|
radix "github.com/armon/go-radix"
|
2015-03-15 21:26:48 +00:00
|
|
|
)
|
|
|
|
|
2017-07-26 21:59:14 +00:00
|
|
|
// InmemStorage implements Storage and stores all data in memory. It is
|
|
|
|
// basically a straight copy of physical.Inmem, but it prevents backends from
|
|
|
|
// having to load all of physical's dependencies (which are legion) just to
|
|
|
|
// have some testing storage.
|
2015-03-15 21:26:48 +00:00
|
|
|
type InmemStorage struct {
|
2017-07-26 21:59:14 +00:00
|
|
|
sync.RWMutex
|
|
|
|
root *radix.Tree
|
2015-03-15 21:26:48 +00:00
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
2017-07-26 21:59:14 +00:00
|
|
|
func (s *InmemStorage) Get(key string) (*StorageEntry, error) {
|
2015-03-15 21:26:48 +00:00
|
|
|
s.once.Do(s.init)
|
|
|
|
|
2017-07-26 21:59:14 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
2015-03-15 21:26:48 +00:00
|
|
|
|
2017-07-26 21:59:14 +00:00
|
|
|
if raw, ok := s.root.Get(key); ok {
|
|
|
|
se := raw.(*StorageEntry)
|
|
|
|
return &StorageEntry{
|
|
|
|
Key: se.Key,
|
|
|
|
Value: se.Value,
|
|
|
|
}, nil
|
2016-06-06 16:03:08 +00:00
|
|
|
}
|
2017-07-26 21:59:14 +00:00
|
|
|
|
|
|
|
return nil, nil
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InmemStorage) Put(entry *StorageEntry) error {
|
|
|
|
s.once.Do(s.init)
|
2017-07-26 21:59:14 +00:00
|
|
|
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
s.root.Insert(entry.Key, &StorageEntry{
|
2016-06-06 16:03:08 +00:00
|
|
|
Key: entry.Key,
|
|
|
|
Value: entry.Value,
|
2017-07-26 21:59:14 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InmemStorage) Delete(key string) error {
|
|
|
|
s.once.Do(s.init)
|
|
|
|
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
s.root.Delete(key)
|
|
|
|
return nil
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 21:59:14 +00:00
|
|
|
func (s *InmemStorage) List(prefix string) ([]string, error) {
|
2015-03-15 21:26:48 +00:00
|
|
|
s.once.Do(s.init)
|
2017-07-26 21:59:14 +00:00
|
|
|
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
var out []string
|
|
|
|
seen := make(map[string]interface{})
|
|
|
|
walkFn := func(s string, v interface{}) bool {
|
|
|
|
trimmed := strings.TrimPrefix(s, prefix)
|
|
|
|
sep := strings.Index(trimmed, "/")
|
|
|
|
if sep == -1 {
|
|
|
|
out = append(out, trimmed)
|
|
|
|
} else {
|
|
|
|
trimmed = trimmed[:sep+1]
|
|
|
|
if _, ok := seen[trimmed]; !ok {
|
|
|
|
out = append(out, trimmed)
|
|
|
|
seen[trimmed] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
s.root.WalkPrefix(prefix, walkFn)
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InmemStorage) init() {
|
2017-07-26 21:59:14 +00:00
|
|
|
s.root = radix.New()
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|