2015-10-05 19:50:45 -04:00
|
|
|
// Package truncindex provides a general 'index tree', used by Docker
|
2015-07-25 04:35:07 -04:00
|
|
|
// in order to be able to reference containers by only a few unambiguous
|
|
|
|
// characters of their id.
|
2018-02-05 16:05:59 -05:00
|
|
|
package truncindex // import "github.com/docker/docker/pkg/truncindex"
|
2014-06-24 13:19:15 -04:00
|
|
|
|
|
|
|
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 (
|
2015-07-25 04:35:07 -04:00
|
|
|
// ErrEmptyPrefix is an error returned if the prefix was empty.
|
|
|
|
ErrEmptyPrefix = errors.New("Prefix can't be empty")
|
2015-07-30 10:51:19 -04:00
|
|
|
|
|
|
|
// ErrIllegalChar is returned when a space is in the ID
|
|
|
|
ErrIllegalChar = errors.New("illegal character: ' '")
|
2015-11-03 20:19:18 -05:00
|
|
|
|
|
|
|
// ErrNotExist is returned when ID or its prefix not found in index.
|
|
|
|
ErrNotExist = errors.New("ID does not exist")
|
2014-06-24 13:19:15 -04:00
|
|
|
)
|
|
|
|
|
2016-03-17 10:53:12 -04:00
|
|
|
// ErrAmbiguousPrefix is returned if the prefix was ambiguous
|
|
|
|
// (multiple ids for the prefix).
|
|
|
|
type ErrAmbiguousPrefix struct {
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrAmbiguousPrefix) Error() string {
|
|
|
|
return fmt.Sprintf("Multiple IDs found with provided prefix: %s", e.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
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -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, " ") {
|
2015-07-30 10:51:19 -04:00
|
|
|
return ErrIllegalChar
|
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
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -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()
|
2016-12-13 15:10:11 -05:00
|
|
|
return idx.addID(id)
|
2014-06-24 13:19:15 -04:00
|
|
|
}
|
|
|
|
|
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 = ""
|
2018-09-24 00:17:31 -04:00
|
|
|
return ErrAmbiguousPrefix{prefix: s}
|
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
|
|
|
}
|
2015-11-03 20:19:18 -05:00
|
|
|
return "", ErrNotExist
|
2014-06-24 13:19:15 -04:00
|
|
|
}
|
2015-06-19 11:01:39 -04:00
|
|
|
|
2017-01-06 09:14:10 -05:00
|
|
|
// Iterate iterates over all stored IDs and passes each of them to the given
|
|
|
|
// handler. Take care that the handler method does not call any public
|
|
|
|
// method on truncindex as the internal locking is not reentrant/recursive
|
|
|
|
// and will result in deadlock.
|
2015-06-19 11:01:39 -04:00
|
|
|
func (idx *TruncIndex) Iterate(handler func(id string)) {
|
2017-01-06 09:14:10 -05:00
|
|
|
idx.Lock()
|
|
|
|
defer idx.Unlock()
|
2015-06-19 11:01:39 -04:00
|
|
|
idx.trie.Visit(func(prefix patricia.Prefix, item patricia.Item) error {
|
|
|
|
handler(string(prefix))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|