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