registry.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2014 Couchbase, Inc.
  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 http
  15. import (
  16. "fmt"
  17. "sync"
  18. "github.com/blevesearch/bleve"
  19. )
  20. var indexNameMapping map[string]bleve.Index
  21. var indexNameMappingLock sync.RWMutex
  22. func RegisterIndexName(name string, idx bleve.Index) {
  23. indexNameMappingLock.Lock()
  24. defer indexNameMappingLock.Unlock()
  25. if indexNameMapping == nil {
  26. indexNameMapping = make(map[string]bleve.Index)
  27. }
  28. indexNameMapping[name] = idx
  29. }
  30. func UnregisterIndexByName(name string) bleve.Index {
  31. indexNameMappingLock.Lock()
  32. defer indexNameMappingLock.Unlock()
  33. if indexNameMapping == nil {
  34. return nil
  35. }
  36. rv := indexNameMapping[name]
  37. if rv != nil {
  38. delete(indexNameMapping, name)
  39. }
  40. return rv
  41. }
  42. func IndexByName(name string) bleve.Index {
  43. indexNameMappingLock.RLock()
  44. defer indexNameMappingLock.RUnlock()
  45. return indexNameMapping[name]
  46. }
  47. func IndexNames() []string {
  48. indexNameMappingLock.RLock()
  49. defer indexNameMappingLock.RUnlock()
  50. rv := make([]string, len(indexNameMapping))
  51. count := 0
  52. for k := range indexNameMapping {
  53. rv[count] = k
  54. count++
  55. }
  56. return rv
  57. }
  58. func UpdateAlias(alias string, add, remove []string) error {
  59. indexNameMappingLock.Lock()
  60. defer indexNameMappingLock.Unlock()
  61. index, exists := indexNameMapping[alias]
  62. if !exists {
  63. // new alias
  64. if len(remove) > 0 {
  65. return fmt.Errorf("cannot remove indexes from a new alias")
  66. }
  67. indexes := make([]bleve.Index, len(add))
  68. for i, addIndexName := range add {
  69. addIndex, indexExists := indexNameMapping[addIndexName]
  70. if !indexExists {
  71. return fmt.Errorf("index named '%s' does not exist", addIndexName)
  72. }
  73. indexes[i] = addIndex
  74. }
  75. indexAlias := bleve.NewIndexAlias(indexes...)
  76. indexNameMapping[alias] = indexAlias
  77. } else {
  78. // something with this name already exists
  79. indexAlias, isAlias := index.(bleve.IndexAlias)
  80. if !isAlias {
  81. return fmt.Errorf("'%s' is not an alias", alias)
  82. }
  83. // build list of add indexes
  84. addIndexes := make([]bleve.Index, len(add))
  85. for i, addIndexName := range add {
  86. addIndex, indexExists := indexNameMapping[addIndexName]
  87. if !indexExists {
  88. return fmt.Errorf("index named '%s' does not exist", addIndexName)
  89. }
  90. addIndexes[i] = addIndex
  91. }
  92. // build list of remove indexes
  93. removeIndexes := make([]bleve.Index, len(remove))
  94. for i, removeIndexName := range remove {
  95. removeIndex, indexExists := indexNameMapping[removeIndexName]
  96. if !indexExists {
  97. return fmt.Errorf("index named '%s' does not exist", removeIndexName)
  98. }
  99. removeIndexes[i] = removeIndex
  100. }
  101. indexAlias.Swap(addIndexes, removeIndexes)
  102. }
  103. return nil
  104. }