2014-06-24 17:19:15 +00:00
|
|
|
package truncindex
|
|
|
|
|
|
|
|
import (
|
2014-06-24 22:24:02 +00:00
|
|
|
"errors"
|
2014-06-24 17:19:15 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2014-06-24 22:24:02 +00:00
|
|
|
|
|
|
|
"github.com/tchap/go-patricia/patricia"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNoID = errors.New("prefix can't be empty")
|
2014-06-24 17:19:15 +00:00
|
|
|
)
|
|
|
|
|
2014-06-28 08:27:44 +00:00
|
|
|
func init() {
|
|
|
|
// Change patricia max prefix per node length,
|
|
|
|
// because our len(ID) always 64
|
|
|
|
patricia.MaxPrefixPerNode = 64
|
|
|
|
}
|
|
|
|
|
2014-06-24 17:19:15 +00: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 22:24:02 +00:00
|
|
|
trie *patricia.Trie
|
|
|
|
ids map[string]struct{}
|
2014-06-24 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTruncIndex(ids []string) (idx *TruncIndex) {
|
|
|
|
idx = &TruncIndex{
|
2014-06-24 22:24:02 +00:00
|
|
|
ids: make(map[string]struct{}),
|
|
|
|
trie: patricia.NewTrie(),
|
2014-06-24 17:19:15 +00:00
|
|
|
}
|
|
|
|
for _, id := range ids {
|
2014-06-24 22:24:02 +00:00
|
|
|
idx.addId(id)
|
2014-06-24 17:19:15 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (idx *TruncIndex) addId(id string) error {
|
|
|
|
if strings.Contains(id, " ") {
|
|
|
|
return fmt.Errorf("Illegal character: ' '")
|
|
|
|
}
|
2014-06-24 22:24:02 +00:00
|
|
|
if id == "" {
|
|
|
|
return ErrNoID
|
|
|
|
}
|
2014-06-24 17:19:15 +00:00
|
|
|
if _, exists := idx.ids[id]; exists {
|
2014-06-24 22:24:02 +00:00
|
|
|
return fmt.Errorf("Id already exists: '%s'", id)
|
|
|
|
}
|
|
|
|
idx.ids[id] = struct{}{}
|
|
|
|
if inserted := idx.trie.Insert(patricia.Prefix(id), struct{}{}); !inserted {
|
|
|
|
return fmt.Errorf("Failed to insert id: %s", id)
|
2014-06-24 17:19:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (idx *TruncIndex) Add(id string) error {
|
|
|
|
idx.Lock()
|
|
|
|
defer idx.Unlock()
|
|
|
|
if err := idx.addId(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (idx *TruncIndex) Delete(id string) error {
|
|
|
|
idx.Lock()
|
|
|
|
defer idx.Unlock()
|
2014-06-24 22:24:02 +00:00
|
|
|
if _, exists := idx.ids[id]; !exists || id == "" {
|
|
|
|
return fmt.Errorf("No such id: '%s'", id)
|
2014-06-24 17:19:15 +00:00
|
|
|
}
|
|
|
|
delete(idx.ids, id)
|
2014-06-24 22:24:02 +00:00
|
|
|
if deleted := idx.trie.Delete(patricia.Prefix(id)); !deleted {
|
|
|
|
return fmt.Errorf("No such id: '%s'", id)
|
2014-06-24 17:19:15 +00:00
|
|
|
}
|
2014-06-24 22:24:02 +00:00
|
|
|
return nil
|
2014-06-24 17:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (idx *TruncIndex) Get(s string) (string, error) {
|
|
|
|
idx.RLock()
|
|
|
|
defer idx.RUnlock()
|
2014-06-24 22:24:02 +00:00
|
|
|
var (
|
|
|
|
id string
|
|
|
|
)
|
|
|
|
if s == "" {
|
|
|
|
return "", ErrNoID
|
|
|
|
}
|
|
|
|
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 = ""
|
|
|
|
return fmt.Errorf("we've found two entries")
|
|
|
|
}
|
|
|
|
id = string(prefix)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := idx.trie.VisitSubtree(patricia.Prefix(s), subTreeVisitFunc); err != nil {
|
|
|
|
return "", fmt.Errorf("No such id: %s", s)
|
|
|
|
}
|
|
|
|
if id != "" {
|
|
|
|
return id, nil
|
2014-06-24 17:19:15 +00:00
|
|
|
}
|
2014-06-24 22:24:02 +00:00
|
|
|
return "", fmt.Errorf("No such id: %s", s)
|
2014-06-24 17:19:15 +00:00
|
|
|
}
|