Build cache key for a pattern only once

This commit is contained in:
Junegunn Choi 2017-08-10 23:18:52 +09:00
parent 6b18b144cf
commit e55e029ae8
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627

View File

@ -47,6 +47,7 @@ type Pattern struct {
text []rune
termSets []termSet
cacheable bool
cacheKey string
delimiter Delimiter
nth []Range
procFun map[termType]algo.Algo
@ -134,6 +135,7 @@ func BuildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case,
delimiter: delimiter,
procFun: make(map[termType]algo.Algo)}
ptr.cacheKey = ptr.buildCacheKey()
ptr.procFun[termFuzzy] = fuzzyAlgo
ptr.procFun[termEqual] = algo.EqualMatch
ptr.procFun[termExact] = algo.ExactMatchNaive
@ -238,8 +240,7 @@ func (p *Pattern) AsString() string {
return string(p.text)
}
// CacheKey is used to build string to be used as the key of result cache
func (p *Pattern) CacheKey() string {
func (p *Pattern) buildCacheKey() string {
if !p.extended {
return p.AsString()
}
@ -252,6 +253,11 @@ func (p *Pattern) CacheKey() string {
return strings.Join(cacheableTerms, "\t")
}
// CacheKey is used to build string to be used as the key of result cache
func (p *Pattern) CacheKey() string {
return p.cacheKey
}
// Match returns the list of matches Items in the given Chunk
func (p *Pattern) Match(chunk *Chunk, slab *util.Slab) []Result {
// ChunkCache: Exact match