index.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cache
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/util/sets"
  18. )
  19. // Indexer is a storage interface that lets you list objects using multiple indexing functions
  20. type Indexer interface {
  21. Store
  22. // Retrieve list of objects that match on the named indexing function
  23. Index(indexName string, obj interface{}) ([]interface{}, error)
  24. // IndexKeys returns the set of keys that match on the named indexing function.
  25. IndexKeys(indexName, indexKey string) ([]string, error)
  26. // ListIndexFuncValues returns the list of generated values of an Index func
  27. ListIndexFuncValues(indexName string) []string
  28. // ByIndex lists object that match on the named indexing function with the exact key
  29. ByIndex(indexName, indexKey string) ([]interface{}, error)
  30. // GetIndexer return the indexers
  31. GetIndexers() Indexers
  32. // AddIndexers adds more indexers to this store. If you call this after you already have data
  33. // in the store, the results are undefined.
  34. AddIndexers(newIndexers Indexers) error
  35. }
  36. // IndexFunc knows how to provide an indexed value for an object.
  37. type IndexFunc func(obj interface{}) ([]string, error)
  38. // IndexFuncToKeyFuncAdapter adapts an indexFunc to a keyFunc. This is only useful if your index function returns
  39. // unique values for every object. This is conversion can create errors when more than one key is found. You
  40. // should prefer to make proper key and index functions.
  41. func IndexFuncToKeyFuncAdapter(indexFunc IndexFunc) KeyFunc {
  42. return func(obj interface{}) (string, error) {
  43. indexKeys, err := indexFunc(obj)
  44. if err != nil {
  45. return "", err
  46. }
  47. if len(indexKeys) > 1 {
  48. return "", fmt.Errorf("too many keys: %v", indexKeys)
  49. }
  50. if len(indexKeys) == 0 {
  51. return "", fmt.Errorf("unexpected empty indexKeys")
  52. }
  53. return indexKeys[0], nil
  54. }
  55. }
  56. // Index maps the indexed value to a set of keys in the store that match on that value
  57. type Index map[string]sets.String
  58. // Indexers maps a name to a IndexFunc
  59. type Indexers map[string]IndexFunc
  60. // Indices maps a name to an Index
  61. type Indices map[string]Index