Skip to content

Commit

Permalink
faster regex match
Browse files Browse the repository at this point in the history
  • Loading branch information
absolutelightning committed Oct 1, 2024
1 parent 87481cc commit 136612b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
9 changes: 3 additions & 6 deletions datastructures/radix/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ type Iterator struct {
key []byte
seekLowerBound bool
patternMatch bool
pattern string
pattern *regexp.Regexp
}

func (i *Iterator) PatternMatch(regex string) {
func (i *Iterator) PatternMatch(regex *regexp.Regexp) {
i.patternMatch = true
i.pattern = regex
}
Expand Down Expand Up @@ -76,10 +76,7 @@ func (i *Iterator) Next() ([]byte, interface{}, bool) {
if i.patternMatch {

for i.leafNode != nil {
matched := true
if len(i.pattern) > 0 {
matched, _ = regexp.MatchString(i.pattern, string(i.leafNode.key))
}
matched := i.pattern.MatchString(string(i.leafNode.key))
if i.leafNode != nil && matched {
res := i.leafNode
i.leafNode = i.leafNode.GetNextLeaf()
Expand Down
7 changes: 5 additions & 2 deletions store/tred_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package store
import (
"fmt"
"hash/fnv"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -324,7 +325,8 @@ func (rs *TredsStore) DeletePrefix(prefix string) (int, error) {

func (rs *TredsStore) Keys(regex string) (string, error) {
iterator := rs.tree.Root().Iterator()
iterator.PatternMatch(regex)
rx := regexp.MustCompile(regex)
iterator.PatternMatch(rx)

var result strings.Builder

Expand All @@ -344,7 +346,8 @@ func (rs *TredsStore) Keys(regex string) (string, error) {

func (rs *TredsStore) KVS(regex string) (string, error) {
iterator := rs.tree.Root().Iterator()
iterator.PatternMatch(regex)
rx := regexp.MustCompile(regex)
iterator.PatternMatch(rx)

var result strings.Builder

Expand Down

0 comments on commit 136612b

Please sign in to comment.