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 15package registry 16 17import ( 18 "fmt" 19 20 "github.com/blevesearch/bleve/analysis" 21) 22 23func RegisterAnalyzer(name string, constructor AnalyzerConstructor) { 24 _, exists := analyzers[name] 25 if exists { 26 panic(fmt.Errorf("attempted to register duplicate analyzer named '%s'", name)) 27 } 28 analyzers[name] = constructor 29} 30 31type AnalyzerConstructor func(config map[string]interface{}, cache *Cache) (*analysis.Analyzer, error) 32type AnalyzerRegistry map[string]AnalyzerConstructor 33 34type AnalyzerCache struct { 35 *ConcurrentCache 36} 37 38func NewAnalyzerCache() *AnalyzerCache { 39 return &AnalyzerCache{ 40 NewConcurrentCache(), 41 } 42} 43 44func AnalyzerBuild(name string, config map[string]interface{}, cache *Cache) (interface{}, error) { 45 cons, registered := analyzers[name] 46 if !registered { 47 return nil, fmt.Errorf("no analyzer with name or type '%s' registered", name) 48 } 49 analyzer, err := cons(config, cache) 50 if err != nil { 51 return nil, fmt.Errorf("error building analyzer: %v", err) 52 } 53 return analyzer, nil 54} 55 56func (c *AnalyzerCache) AnalyzerNamed(name string, cache *Cache) (*analysis.Analyzer, error) { 57 item, err := c.ItemNamed(name, cache, AnalyzerBuild) 58 if err != nil { 59 return nil, err 60 } 61 return item.(*analysis.Analyzer), nil 62} 63 64func (c *AnalyzerCache) DefineAnalyzer(name string, typ string, config map[string]interface{}, cache *Cache) (*analysis.Analyzer, error) { 65 item, err := c.DefineItem(name, typ, config, cache, AnalyzerBuild) 66 if err != nil { 67 if err == ErrAlreadyDefined { 68 return nil, fmt.Errorf("analyzer named '%s' already defined", name) 69 } 70 return nil, err 71 } 72 return item.(*analysis.Analyzer), nil 73} 74 75func AnalyzerTypesAndInstances() ([]string, []string) { 76 emptyConfig := map[string]interface{}{} 77 emptyCache := NewCache() 78 var types []string 79 var instances []string 80 for name, cons := range analyzers { 81 _, err := cons(emptyConfig, emptyCache) 82 if err == nil { 83 instances = append(instances, name) 84 } else { 85 types = append(types, name) 86 } 87 } 88 return types, instances 89} 90