pkg/truncindex: lint and add comments

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
This commit is contained in:
unclejack 2014-10-06 22:00:58 +03:00
parent 6f66d8a30f
commit d250fdea61

View file

@ -10,7 +10,9 @@ import (
) )
var ( var (
// ErrNoID is thrown when attempting to use empty prefixes
ErrNoID = errors.New("prefix can't be empty") ErrNoID = errors.New("prefix can't be empty")
errDuplicateID = errors.New("multiple IDs were found")
) )
func init() { func init() {
@ -27,56 +29,62 @@ type TruncIndex struct {
ids map[string]struct{} ids map[string]struct{}
} }
// NewTruncIndex creates a new TruncIndex and initializes with a list of IDs
func NewTruncIndex(ids []string) (idx *TruncIndex) { func NewTruncIndex(ids []string) (idx *TruncIndex) {
idx = &TruncIndex{ idx = &TruncIndex{
ids: make(map[string]struct{}), ids: make(map[string]struct{}),
trie: patricia.NewTrie(), trie: patricia.NewTrie(),
} }
for _, id := range ids { for _, id := range ids {
idx.addId(id) idx.addID(id)
} }
return return
} }
func (idx *TruncIndex) addId(id string) error { func (idx *TruncIndex) addID(id string) error {
if strings.Contains(id, " ") { if strings.Contains(id, " ") {
return fmt.Errorf("Illegal character: ' '") return fmt.Errorf("illegal character: ' '")
} }
if id == "" { if id == "" {
return ErrNoID return ErrNoID
} }
if _, exists := idx.ids[id]; exists { if _, exists := idx.ids[id]; exists {
return fmt.Errorf("Id already exists: '%s'", id) return fmt.Errorf("id already exists: '%s'", id)
} }
idx.ids[id] = struct{}{} idx.ids[id] = struct{}{}
if inserted := idx.trie.Insert(patricia.Prefix(id), struct{}{}); !inserted { if inserted := idx.trie.Insert(patricia.Prefix(id), struct{}{}); !inserted {
return fmt.Errorf("Failed to insert id: %s", id) return fmt.Errorf("failed to insert id: %s", id)
} }
return nil return nil
} }
// Add adds a new ID to the TruncIndex
func (idx *TruncIndex) Add(id string) error { func (idx *TruncIndex) Add(id string) error {
idx.Lock() idx.Lock()
defer idx.Unlock() defer idx.Unlock()
if err := idx.addId(id); err != nil { if err := idx.addID(id); err != nil {
return err return err
} }
return nil return nil
} }
// Delete removes an ID from the TruncIndex. If there are multiple IDs
// with the given prefix, an error is thrown.
func (idx *TruncIndex) Delete(id string) error { func (idx *TruncIndex) Delete(id string) error {
idx.Lock() idx.Lock()
defer idx.Unlock() defer idx.Unlock()
if _, exists := idx.ids[id]; !exists || id == "" { if _, exists := idx.ids[id]; !exists || id == "" {
return fmt.Errorf("No such id: '%s'", id) return fmt.Errorf("no such id: '%s'", id)
} }
delete(idx.ids, id) delete(idx.ids, id)
if deleted := idx.trie.Delete(patricia.Prefix(id)); !deleted { if deleted := idx.trie.Delete(patricia.Prefix(id)); !deleted {
return fmt.Errorf("No such id: '%s'", id) return fmt.Errorf("no such id: '%s'", id)
} }
return nil return nil
} }
// Get retrieves an ID from the TruncIndex. If there are multiple IDs
// with the given prefix, an error is thrown.
func (idx *TruncIndex) Get(s string) (string, error) { func (idx *TruncIndex) Get(s string) (string, error) {
idx.RLock() idx.RLock()
defer idx.RUnlock() defer idx.RUnlock()
@ -90,17 +98,17 @@ func (idx *TruncIndex) Get(s string) (string, error) {
if id != "" { if id != "" {
// we haven't found the ID if there are two or more IDs // we haven't found the ID if there are two or more IDs
id = "" id = ""
return fmt.Errorf("we've found two entries") return errDuplicateID
} }
id = string(prefix) id = string(prefix)
return nil return nil
} }
if err := idx.trie.VisitSubtree(patricia.Prefix(s), subTreeVisitFunc); err != nil { if err := idx.trie.VisitSubtree(patricia.Prefix(s), subTreeVisitFunc); err != nil {
return "", fmt.Errorf("No such id: %s", s) return "", fmt.Errorf("no such id: %s", s)
} }
if id != "" { if id != "" {
return id, nil return id, nil
} }
return "", fmt.Errorf("No such id: %s", s) return "", fmt.Errorf("no such id: %s", s)
} }