2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
2015-04-17 13:23:52 +00:00
|
|
|
// queryCache associates strings to lists of items
|
2017-07-17 18:10:49 +00:00
|
|
|
type queryCache map[string][]Result
|
2015-01-11 18:01:24 +00:00
|
|
|
|
|
|
|
// ChunkCache associates Chunk and query string to lists of items
|
2015-01-01 19:49:30 +00:00
|
|
|
type ChunkCache struct {
|
|
|
|
mutex sync.Mutex
|
2015-04-17 13:23:52 +00:00
|
|
|
cache map[*Chunk]*queryCache
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// NewChunkCache returns a new ChunkCache
|
2015-01-01 19:49:30 +00:00
|
|
|
func NewChunkCache() ChunkCache {
|
2015-04-17 13:23:52 +00:00
|
|
|
return ChunkCache{sync.Mutex{}, make(map[*Chunk]*queryCache)}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Add adds the list to the cache
|
2017-07-17 18:10:49 +00:00
|
|
|
func (cc *ChunkCache) Add(chunk *Chunk, key string, list []Result) {
|
2015-04-17 13:23:52 +00:00
|
|
|
if len(key) == 0 || !chunk.IsFull() || len(list) > queryCacheMax {
|
2015-01-01 19:49:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cc.mutex.Lock()
|
|
|
|
defer cc.mutex.Unlock()
|
|
|
|
|
|
|
|
qc, ok := cc.cache[chunk]
|
|
|
|
if !ok {
|
2015-04-17 13:23:52 +00:00
|
|
|
cc.cache[chunk] = &queryCache{}
|
2015-01-01 19:49:30 +00:00
|
|
|
qc = cc.cache[chunk]
|
|
|
|
}
|
|
|
|
(*qc)[key] = list
|
|
|
|
}
|
|
|
|
|
2017-07-16 14:31:19 +00:00
|
|
|
// Lookup is called to lookup ChunkCache
|
2017-07-17 18:10:49 +00:00
|
|
|
func (cc *ChunkCache) Lookup(chunk *Chunk, key string) []Result {
|
2015-01-01 19:49:30 +00:00
|
|
|
if len(key) == 0 || !chunk.IsFull() {
|
2017-07-15 10:35:27 +00:00
|
|
|
return nil
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cc.mutex.Lock()
|
|
|
|
defer cc.mutex.Unlock()
|
|
|
|
|
|
|
|
qc, ok := cc.cache[chunk]
|
|
|
|
if ok {
|
|
|
|
list, ok := (*qc)[key]
|
|
|
|
if ok {
|
2017-07-15 10:35:27 +00:00
|
|
|
return list
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-15 10:35:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-17 18:10:49 +00:00
|
|
|
func (cc *ChunkCache) Search(chunk *Chunk, key string) []Result {
|
2017-07-15 10:35:27 +00:00
|
|
|
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
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|