-
Notifications
You must be signed in to change notification settings - Fork 277
/
index.go
120 lines (99 loc) 路 3.48 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package hardware
import (
"fmt"
"reflect"
)
// FieldIndexer indexes collection of objects for a single type against one of its fields.
// FieldIndexer is not thread safe.
type FieldIndexer struct {
expectedType reflect.Type
indexes map[string]*fieldIndex
}
// NewFieldIndexer creates a new FieldIndexer instance. object is the object to be indexed and will
// be checked during Insert() calls. NewFieldIndexer will panic if object is nil.
func NewFieldIndexer(object interface{}) *FieldIndexer {
objectType := reflect.TypeOf(object)
if objectType == nil {
panic("object cannot be nil")
}
return &FieldIndexer{
expectedType: objectType,
indexes: make(map[string]*fieldIndex),
}
}
// KeyExtractorFunc returns a key from object that can be used to look up the object.
type KeyExtractorFunc func(object interface{}) string
// IndexField registers a new index with i. field is the index name and should represent a path
// to the field such as `.Spec.ID`. fn is used to extract the lookup key on Insert() from the object
// to be inserted.
func (i *FieldIndexer) IndexField(field string, fn KeyExtractorFunc) {
i.indexes[field] = &fieldIndex{
index: make(map[string][]interface{}),
keyExtractorFunc: fn,
}
}
// Insert inserts v into i on all indexed fields registered with IndexField. If v is not of the
// expected type defined by NewFieldIndexer() ErrIncorrectType is returned. Multiple objects
// with the same index value may be inserted.
func (i *FieldIndexer) Insert(v interface{}) error {
objectType := reflect.TypeOf(v)
if objectType != i.expectedType {
return ErrIncorrectType{Expected: i.expectedType, Received: objectType}
}
for _, idx := range i.indexes {
idx.Insert(v)
}
return nil
}
// Lookup uses the index associated with field to find and return all objects associated with key.
// If field has no associated index created by IndexField ErrUnknownIndex is returned.
func (i *FieldIndexer) Lookup(field string, key string) ([]interface{}, error) {
idx, ok := i.indexes[field]
if !ok {
return nil, ErrUnknownIndex{Field: field}
}
return idx.Lookup(key), nil
}
// Remove removes v from all indexes if present. If v is not present Remove is a no-op. If v is of
// an incorrect type ErrUnknownType is returned.
func (i *FieldIndexer) Remove(v interface{}) error {
objectType := reflect.TypeOf(v)
if objectType != i.expectedType {
return ErrIncorrectType{Expected: i.expectedType, Received: objectType}
}
for _, idx := range i.indexes {
idx.Remove(v)
}
return nil
}
// fieldIndex represents a single index on a particular object. When inserting into the fieldIndex
// the key is extracted from the object using the KeyExtractorFunc.
type fieldIndex struct {
index map[string][]interface{}
keyExtractorFunc KeyExtractorFunc
}
func (i *fieldIndex) Insert(v interface{}) {
key := i.keyExtractorFunc(v)
i.index[key] = append(i.index[key], v)
}
func (i *fieldIndex) Lookup(key string) []interface{} {
return i.index[key]
}
func (i *fieldIndex) Remove(v interface{}) {
key := i.keyExtractorFunc(v)
delete(i.index, key)
}
// ErrIncorrectType indicates an incorrect type was used with a FieldIndexer.
type ErrIncorrectType struct {
Expected reflect.Type
Received reflect.Type
}
func (e ErrIncorrectType) Error() string {
return fmt.Sprintf("expected type '%s', received object of type '%v'", e.Expected, e.Received)
}
type ErrUnknownIndex struct {
Field string
}
func (e ErrUnknownIndex) Error() string {
return fmt.Sprintf("unknown index: %v", e.Field)
}