Browse code

pkg/truncindex: lint and add comments

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)

unclejack authored on 2014/10/07 04:00:58
Showing 2 changed files
... ...
@@ -92,7 +92,7 @@ func httpError(w http.ResponseWriter, err error) {
92 92
 	// FIXME: this is brittle and should not be necessary.
93 93
 	// If we need to differentiate between different possible error types, we should
94 94
 	// create appropriate error types with clearly defined meaning.
95
-	if strings.Contains(err.Error(), "No such") {
95
+	if strings.Contains(err.Error(), "no such") {
96 96
 		statusCode = http.StatusNotFound
97 97
 	} else if strings.Contains(err.Error(), "Bad parameter") {
98 98
 		statusCode = http.StatusBadRequest
... ...
@@ -10,7 +10,9 @@ import (
10 10
 )
11 11
 
12 12
 var (
13
-	ErrNoID = errors.New("prefix can't be empty")
13
+	// ErrNoID is thrown when attempting to use empty prefixes
14
+	ErrNoID        = errors.New("prefix can't be empty")
15
+	errDuplicateID = errors.New("multiple IDs were found")
14 16
 )
15 17
 
16 18
 func init() {
... ...
@@ -27,56 +29,62 @@ type TruncIndex struct {
27 27
 	ids  map[string]struct{}
28 28
 }
29 29
 
30
+// NewTruncIndex creates a new TruncIndex and initializes with a list of IDs
30 31
 func NewTruncIndex(ids []string) (idx *TruncIndex) {
31 32
 	idx = &TruncIndex{
32 33
 		ids:  make(map[string]struct{}),
33 34
 		trie: patricia.NewTrie(),
34 35
 	}
35 36
 	for _, id := range ids {
36
-		idx.addId(id)
37
+		idx.addID(id)
37 38
 	}
38 39
 	return
39 40
 }
40 41
 
41
-func (idx *TruncIndex) addId(id string) error {
42
+func (idx *TruncIndex) addID(id string) error {
42 43
 	if strings.Contains(id, " ") {
43
-		return fmt.Errorf("Illegal character: ' '")
44
+		return fmt.Errorf("illegal character: ' '")
44 45
 	}
45 46
 	if id == "" {
46 47
 		return ErrNoID
47 48
 	}
48 49
 	if _, exists := idx.ids[id]; exists {
49
-		return fmt.Errorf("Id already exists: '%s'", id)
50
+		return fmt.Errorf("id already exists: '%s'", id)
50 51
 	}
51 52
 	idx.ids[id] = struct{}{}
52 53
 	if inserted := idx.trie.Insert(patricia.Prefix(id), struct{}{}); !inserted {
53
-		return fmt.Errorf("Failed to insert id: %s", id)
54
+		return fmt.Errorf("failed to insert id: %s", id)
54 55
 	}
55 56
 	return nil
56 57
 }
57 58
 
59
+// Add adds a new ID to the TruncIndex
58 60
 func (idx *TruncIndex) Add(id string) error {
59 61
 	idx.Lock()
60 62
 	defer idx.Unlock()
61
-	if err := idx.addId(id); err != nil {
63
+	if err := idx.addID(id); err != nil {
62 64
 		return err
63 65
 	}
64 66
 	return nil
65 67
 }
66 68
 
69
+// Delete removes an ID from the TruncIndex. If there are multiple IDs
70
+// with the given prefix, an error is thrown.
67 71
 func (idx *TruncIndex) Delete(id string) error {
68 72
 	idx.Lock()
69 73
 	defer idx.Unlock()
70 74
 	if _, exists := idx.ids[id]; !exists || id == "" {
71
-		return fmt.Errorf("No such id: '%s'", id)
75
+		return fmt.Errorf("no such id: '%s'", id)
72 76
 	}
73 77
 	delete(idx.ids, id)
74 78
 	if deleted := idx.trie.Delete(patricia.Prefix(id)); !deleted {
75
-		return fmt.Errorf("No such id: '%s'", id)
79
+		return fmt.Errorf("no such id: '%s'", id)
76 80
 	}
77 81
 	return nil
78 82
 }
79 83
 
84
+// Get retrieves an ID from the TruncIndex. If there are multiple IDs
85
+// with the given prefix, an error is thrown.
80 86
 func (idx *TruncIndex) Get(s string) (string, error) {
81 87
 	idx.RLock()
82 88
 	defer idx.RUnlock()
... ...
@@ -90,17 +98,17 @@ func (idx *TruncIndex) Get(s string) (string, error) {
90 90
 		if id != "" {
91 91
 			// we haven't found the ID if there are two or more IDs
92 92
 			id = ""
93
-			return fmt.Errorf("we've found two entries")
93
+			return errDuplicateID
94 94
 		}
95 95
 		id = string(prefix)
96 96
 		return nil
97 97
 	}
98 98
 
99 99
 	if err := idx.trie.VisitSubtree(patricia.Prefix(s), subTreeVisitFunc); err != nil {
100
-		return "", fmt.Errorf("No such id: %s", s)
100
+		return "", fmt.Errorf("no such id: %s", s)
101 101
 	}
102 102
 	if id != "" {
103 103
 		return id, nil
104 104
 	}
105
-	return "", fmt.Errorf("No such id: %s", s)
105
+	return "", fmt.Errorf("no such id: %s", s)
106 106
 }