ByIndex returns a list of items that match an exact value on the index function
(indexName, indexKey string)
| 173 | |
| 174 | // ByIndex returns a list of items that match an exact value on the index function |
| 175 | func (c *threadSafeMap) ByIndex(indexName, indexKey string) ([]interface{}, error) { |
| 176 | c.lock.RLock() |
| 177 | defer c.lock.RUnlock() |
| 178 | |
| 179 | indexFunc := c.indexers[indexName] |
| 180 | if indexFunc == nil { |
| 181 | return nil, fmt.Errorf("Index with name %s does not exist", indexName) |
| 182 | } |
| 183 | |
| 184 | index := c.indices[indexName] |
| 185 | |
| 186 | set := index[indexKey] |
| 187 | list := make([]interface{}, 0, set.Len()) |
| 188 | for key := range set { |
| 189 | list = append(list, c.items[key]) |
| 190 | } |
| 191 | |
| 192 | return list, nil |
| 193 | } |
| 194 | |
| 195 | // IndexKeys returns a list of keys that match on the index function. |
| 196 | // IndexKeys is thread-safe so long as you treat all items as immutable. |