analyzer.go 2.5 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 RegisterAnalyzer(name string, constructor AnalyzerConstructor) {
  20. _, exists := analyzers[name]
  21. if exists {
  22. panic(fmt.Errorf("attempted to register duplicate analyzer named '%s'", name))
  23. }
  24. analyzers[name] = constructor
  25. }
  26. type AnalyzerConstructor func(config map[string]interface{}, cache *Cache) (*analysis.Analyzer, error)
  27. type AnalyzerRegistry map[string]AnalyzerConstructor
  28. type AnalyzerCache struct {
  29. *ConcurrentCache
  30. }
  31. func NewAnalyzerCache() *AnalyzerCache {
  32. return &AnalyzerCache{
  33. NewConcurrentCache(),
  34. }
  35. }
  36. func AnalyzerBuild(name string, config map[string]interface{}, cache *Cache) (interface{}, error) {
  37. cons, registered := analyzers[name]
  38. if !registered {
  39. return nil, fmt.Errorf("no analyzer with name or type '%s' registered", name)
  40. }
  41. analyzer, err := cons(config, cache)
  42. if err != nil {
  43. return nil, fmt.Errorf("error building analyzer: %v", err)
  44. }
  45. return analyzer, nil
  46. }
  47. func (c *AnalyzerCache) AnalyzerNamed(name string, cache *Cache) (*analysis.Analyzer, error) {
  48. item, err := c.ItemNamed(name, cache, AnalyzerBuild)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return item.(*analysis.Analyzer), nil
  53. }
  54. func (c *AnalyzerCache) DefineAnalyzer(name string, typ string, config map[string]interface{}, cache *Cache) (*analysis.Analyzer, error) {
  55. item, err := c.DefineItem(name, typ, config, cache, AnalyzerBuild)
  56. if err != nil {
  57. if err == ErrAlreadyDefined {
  58. return nil, fmt.Errorf("analyzer named '%s' already defined", name)
  59. }
  60. return nil, err
  61. }
  62. return item.(*analysis.Analyzer), nil
  63. }
  64. func AnalyzerTypesAndInstances() ([]string, []string) {
  65. emptyConfig := map[string]interface{}{}
  66. emptyCache := NewCache()
  67. var types []string
  68. var instances []string
  69. for name, cons := range analyzers {
  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. }