summaryrefslogtreecommitdiffstats
path: root/src/cache.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2017-07-15 19:35:27 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2017-07-16 23:34:32 +0900
commit8dbdd557302a282ff01dc1a89c4e5c28676bf72e (patch)
tree21f348eed7f850cdb7eeca587080de4bc6c97730 /src/cache.go
parent6725151a994da0493aa2925c7fc141c1618bc3f9 (diff)
Refactor cache lookup
- Remove multiple mutex locks in partial cache lookup - Simplify return values
Diffstat (limited to 'src/cache.go')
-rw-r--r--src/cache.go36
1 files changed, 32 insertions, 4 deletions
diff --git a/src/cache.go b/src/cache.go
index baf88dd8..272a78be 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
}