indexmatcher_mph.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package strmatcher
  2. // A MphIndexMatcher is divided into three parts:
  3. // 1. `full` and `domain` patterns are matched by Rabin-Karp algorithm and minimal perfect hash table;
  4. // 2. `substr` patterns are matched by ac automaton;
  5. // 3. `regex` patterns are matched with the regex library.
  6. type MphIndexMatcher struct {
  7. count uint32
  8. mph *MphMatcherGroup
  9. ac *ACAutomatonMatcherGroup
  10. regex SimpleMatcherGroup
  11. }
  12. func NewMphIndexMatcher() *MphIndexMatcher {
  13. return &MphIndexMatcher{
  14. mph: nil,
  15. ac: nil,
  16. regex: SimpleMatcherGroup{},
  17. }
  18. }
  19. // Add implements IndexMatcher.Add.
  20. func (g *MphIndexMatcher) Add(matcher Matcher) uint32 {
  21. g.count++
  22. index := g.count
  23. switch matcher := matcher.(type) {
  24. case FullMatcher:
  25. if g.mph == nil {
  26. g.mph = NewMphMatcherGroup()
  27. }
  28. g.mph.AddFullMatcher(matcher, index)
  29. case DomainMatcher:
  30. if g.mph == nil {
  31. g.mph = NewMphMatcherGroup()
  32. }
  33. g.mph.AddDomainMatcher(matcher, index)
  34. case SubstrMatcher:
  35. if g.ac == nil {
  36. g.ac = NewACAutomatonMatcherGroup()
  37. }
  38. g.ac.AddSubstrMatcher(matcher, index)
  39. case *RegexMatcher:
  40. g.regex.AddMatcher(matcher, index)
  41. }
  42. return index
  43. }
  44. // Build implements IndexMatcher.Build.
  45. func (g *MphIndexMatcher) Build() error {
  46. if g.mph != nil {
  47. g.mph.Build()
  48. }
  49. if g.ac != nil {
  50. g.ac.Build()
  51. }
  52. return nil
  53. }
  54. // Match implements IndexMatcher.Match.
  55. func (*MphIndexMatcher) Match(string) []uint32 {
  56. return nil
  57. }
  58. // MatchAny implements IndexMatcher.MatchAny.
  59. func (g *MphIndexMatcher) MatchAny(input string) bool {
  60. if g.mph != nil && g.mph.MatchAny(input) {
  61. return true
  62. }
  63. if g.ac != nil && g.ac.MatchAny(input) {
  64. return true
  65. }
  66. return g.regex.MatchAny(input)
  67. }
  68. // Size implements IndexMatcher.Size.
  69. func (g *MphIndexMatcher) Size() uint32 {
  70. return g.count
  71. }