char_filter.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 registry
  15. import (
  16. "fmt"
  17. "github.com/blevesearch/bleve/analysis"
  18. )
  19. func RegisterCharFilter(name string, constructor CharFilterConstructor) {
  20. _, exists := charFilters[name]
  21. if exists {
  22. panic(fmt.Errorf("attempted to register duplicate char filter named '%s'", name))
  23. }
  24. charFilters[name] = constructor
  25. }
  26. type CharFilterConstructor func(config map[string]interface{}, cache *Cache) (analysis.CharFilter, error)
  27. type CharFilterRegistry map[string]CharFilterConstructor
  28. type CharFilterCache struct {
  29. *ConcurrentCache
  30. }
  31. func NewCharFilterCache() *CharFilterCache {
  32. return &CharFilterCache{
  33. NewConcurrentCache(),
  34. }
  35. }
  36. func CharFilterBuild(name string, config map[string]interface{}, cache *Cache) (interface{}, error) {
  37. cons, registered := charFilters[name]
  38. if !registered {
  39. return nil, fmt.Errorf("no char filter with name or type '%s' registered", name)
  40. }
  41. charFilter, err := cons(config, cache)
  42. if err != nil {
  43. return nil, fmt.Errorf("error building char filter: %v", err)
  44. }
  45. return charFilter, nil
  46. }
  47. func (c *CharFilterCache) CharFilterNamed(name string, cache *Cache) (analysis.CharFilter, error) {
  48. item, err := c.ItemNamed(name, cache, CharFilterBuild)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return item.(analysis.CharFilter), nil
  53. }
  54. func (c *CharFilterCache) DefineCharFilter(name string, typ string, config map[string]interface{}, cache *Cache) (analysis.CharFilter, error) {
  55. item, err := c.DefineItem(name, typ, config, cache, CharFilterBuild)
  56. if err != nil {
  57. if err == ErrAlreadyDefined {
  58. return nil, fmt.Errorf("char filter named '%s' already defined", name)
  59. }
  60. return nil, err
  61. }
  62. return item.(analysis.CharFilter), nil
  63. }
  64. func CharFilterTypesAndInstances() ([]string, []string) {
  65. emptyConfig := map[string]interface{}{}
  66. emptyCache := NewCache()
  67. var types []string
  68. var instances []string
  69. for name, cons := range charFilters {
  70. _, err := cons(emptyConfig, emptyCache)
  71. if err == nil {
  72. instances = append(instances, name)
  73. } else {
  74. types = append(types, name)
  75. }
  76. }
  77. return types, instances
  78. }