2014-06-24 13:19:15 -04:00
|
|
|
package truncindex
|
|
|
|
|
|
|
|
import (
|
2014-06-24 18:24:02 -04:00
|
|
|
"errors"
|
2014-06-24 13:19:15 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2014-06-24 18:24:02 -04:00
|
|
|
|
|
|
|
"github.com/tchap/go-patricia/patricia"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-12-16 18:06:35 -05:00
|
|
|
ErrEmptyPrefix = errors.New("Prefix can't be empty")
|
|
|
|
ErrAmbiguousPrefix = errors.New("Multiple IDs found with provided prefix")
|
2014-06-24 13:19:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// TruncIndex allows the retrieval of string identifiers by any of their unique prefixes.
|
|
|
|
// This is used to retrieve image and container IDs by more convenient shorthand prefixes.
|
|
|
|
type TruncIndex struct {
|
|
|
|
sync.RWMutex
|
2014-06-24 18:24:02 -04:00
|
|
|
trie *patricia.Trie
|
|
|
|
ids map[string]struct{}
|
2014-06-24 13:19:15 -04:00
|
|
|
}
|
|
|
|
|
2014-10-06 15:00:58 -04:00
|
|
|
// NewTruncIndex creates a new TruncIndex and initializes with a list of IDs
|
2014-06-24 13:19:15 -04:00
|
|
|
func NewTruncIndex(ids []string) (idx *TruncIndex) {
|
|
|
|
idx = &TruncIndex{
|
2015-05-03 01:25:57 -04:00
|
|
|
ids: make(map[string]struct{}),
|
|
|
|
|
|
|
|
// Change patricia max prefix per node length,
|
|
|
|
// because our len(ID) always 64
|
|
|
|
trie: patricia.NewTrie(patricia.MaxPrefixPerNode(64)),
|
2014-06-24 13:19:15 -04:00
|
|
|
}
|
|
|
|
for _, id := range ids {
|
2014-10-06 15:00:58 -04:00
|
|
|
idx.addID(id)
|
2014-06-24 13:19:15 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-06 15:00:58 -04:00
|
|
|
func (idx *TruncIndex) addID(id string) error {
|
2014-06-24 13:19:15 -04:00
|
|
|
if strings.Contains(id, " ") {
|
2014-10-06 15:00:58 -04:00
|
|
|
return fmt.Errorf("illegal character: ' '")
|
2014-06-24 13:19:15 -04:00
|
|
|
}
|
2014-06-24 18:24:02 -04:00
|
|
|
if id == "" {
|
2014-12-16 18:06:35 -05:00
|
|
|
return ErrEmptyPrefix
|
2014-06-24 18:24:02 -04:00
|
|
|
}
|
2014-06-24 13:19:15 -04:00
|
|
|
if _, exists := idx.ids[id]; exists {
|
2014-10-06 15:00:58 -04:00
|
|
|
return fmt.Errorf("id already exists: '%s'", id)
|
2014-06-24 18:24:02 -04:00
|
|
|
}
|
|
|
|
idx.ids[id] = struct{}{}
|
|
|
|
if inserted := idx.trie.Insert(patricia.Prefix(id), struct{}{}); !inserted {
|
2014-10-06 15:00:58 -04:00
|
|
|
return fmt.Errorf("failed to insert id: %s", id)
|
2014-06-24 13:19:15 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-06 15:00:58 -04:00
|
|
|
// Add adds a new ID to the TruncIndex
|
2014-06-24 13:19:15 -04:00
|
|
|
func (idx *TruncIndex) Add(id string) error {
|
|
|
|
idx.Lock()
|
|
|
|
defer idx.Unlock()
|
2014-10-06 15:00:58 -04:00
|
|
|
if err := idx.addID(id); err != nil {
|
2014-06-24 13:19:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-06 15:00:58 -04:00
|
|
|
// Delete removes an ID from the TruncIndex. If there are multiple IDs
|
|
|
|
// with the given prefix, an error is thrown.
|
2014-06-24 13:19:15 -04:00
|
|
|
func (idx *TruncIndex) Delete(id string) error {
|
|
|
|
idx.Lock()
|
|
|
|
defer idx.Unlock()
|
2014-06-24 18:24:02 -04:00
|
|
|
if _, exists := idx.ids[id]; !exists || id == "" {
|
2014-10-06 15:00:58 -04:00
|
|
|
return fmt.Errorf("no such id: '%s'", id)
|
2014-06-24 13:19:15 -04:00
|
|
|
}
|
|
|
|
delete(idx.ids, id)
|
2014-06-24 18:24:02 -04:00
|
|
|
if deleted := idx.trie.Delete(patricia.Prefix(id)); !deleted {
|
2014-10-06 15:00:58 -04:00
|
|
|
return fmt.Errorf("no such id: '%s'", id)
|
2014-06-24 13:19:15 -04:00
|
|
|
}
|
2014-06-24 18:24:02 -04:00
|
|
|
return nil
|
2014-06-24 13:19:15 -04:00
|
|
|
}
|
|
|
|
|
2014-10-06 15:00:58 -04:00
|
|
|
// Get retrieves an ID from the TruncIndex. If there are multiple IDs
|
|
|
|
// with the given prefix, an error is thrown.
|
2014-06-24 13:19:15 -04:00
|
|
|
func (idx *TruncIndex) Get(s string) (string, error) {
|
2014-12-16 18:06:35 -05:00
|
|
|
if s == "" {
|
|
|
|
return "", ErrEmptyPrefix
|
|
|
|
}
|
2014-06-24 18:24:02 -04:00
|
|
|
var (
|
|
|
|
id string
|
|
|
|
)
|
|
|
|
subTreeVisitFunc := func(prefix patricia.Prefix, item patricia.Item) error {
|
|
|
|
if id != "" {
|
|
|
|
// we haven't found the ID if there are two or more IDs
|
|
|
|
id = ""
|
2014-12-16 18:06:35 -05:00
|
|
|
return ErrAmbiguousPrefix
|
2014-06-24 18:24:02 -04:00
|
|
|
}
|
|
|
|
id = string(prefix)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
idx.RLock()
|
|
|
|
defer idx.RUnlock()
|
2014-06-24 18:24:02 -04:00
|
|
|
if err := idx.trie.VisitSubtree(patricia.Prefix(s), subTreeVisitFunc); err != nil {
|
2014-12-16 18:06:35 -05:00
|
|
|
return "", err
|
2014-06-24 18:24:02 -04:00
|
|
|
}
|
|
|
|
if id != "" {
|
|
|
|
return id, nil
|
2014-06-24 13:19:15 -04:00
|
|
|
}
|
2014-10-06 15:00:58 -04:00
|
|
|
return "", fmt.Errorf("no such id: %s", s)
|
2014-06-24 13:19:15 -04:00
|
|
|
}
|