diff --git a/src/cache.go b/src/cache.go index baf88dd..272a78b 100644 --- a/src/cache.go +++ b/src/cache.go @@ -34,9 +34,9 @@ func (cc *ChunkCache) Add(chunk *Chunk, key string, list []*Result) { } // Find is called to lookup ChunkCache -func (cc *ChunkCache) Find(chunk *Chunk, key string) ([]*Result, bool) { +func (cc *ChunkCache) Find(chunk *Chunk, key string) []*Result { if len(key) == 0 || !chunk.IsFull() { - return nil, false + return nil } cc.mutex.Lock() @@ -46,8 +46,36 @@ func (cc *ChunkCache) Find(chunk *Chunk, key string) ([]*Result, bool) { if ok { list, ok := (*qc)[key] if ok { - return list, true + return list } } - return nil, false + return nil +} + +func (cc *ChunkCache) Search(chunk *Chunk, key string) []*Result { + if len(key) == 0 || !chunk.IsFull() { + return nil + } + + cc.mutex.Lock() + defer cc.mutex.Unlock() + + qc, ok := cc.cache[chunk] + if !ok { + return nil + } + + for idx := 1; idx < len(key); idx++ { + // [---------| ] | [ |---------] + // [--------| ] | [ |--------] + // [-------| ] | [ |-------] + prefix := key[:len(key)-idx] + suffix := key[idx:] + for _, substr := range [2]string{prefix, suffix} { + if cached, found := (*qc)[substr]; found { + return cached + } + } + } + return nil } diff --git a/src/pattern.go b/src/pattern.go index 05b03b9..f1caeba 100644 --- a/src/pattern.go +++ b/src/pattern.go @@ -247,27 +247,13 @@ func (p *Pattern) Match(chunk *Chunk, slab *util.Slab) []*Result { // ChunkCache: Exact match cacheKey := p.CacheKey() if p.cacheable { - if cached, found := _cache.Find(chunk, cacheKey); found { + if cached := _cache.Find(chunk, cacheKey); cached != nil { return cached } } // Prefix/suffix cache - var space []*Result -Loop: - for idx := 1; idx < len(cacheKey); idx++ { - // [---------| ] | [ |---------] - // [--------| ] | [ |--------] - // [-------| ] | [ |-------] - prefix := cacheKey[:len(cacheKey)-idx] - suffix := cacheKey[idx:] - for _, substr := range [2]*string{&prefix, &suffix} { - if cached, found := _cache.Find(chunk, *substr); found { - space = cached - break Loop - } - } - } + space := _cache.Search(chunk, cacheKey) matches := p.matchChunk(chunk, space, slab)