Sha256: 5295f1d0b2a377353334aa941ba210745ea467c5f21ffc9db5aa8248a6fcfb10

Contents?: true

Size: 1.58 KB

Versions: 87

Compression:

Stored size: 1.58 KB

Contents

package fzf

import "sync"

// queryCache associates strings to lists of items
type queryCache map[string][]Result

// ChunkCache associates Chunk and query string to lists of items
type ChunkCache struct {
	mutex sync.Mutex
	cache map[*Chunk]*queryCache
}

// NewChunkCache returns a new ChunkCache
func NewChunkCache() ChunkCache {
	return ChunkCache{sync.Mutex{}, make(map[*Chunk]*queryCache)}
}

// Add adds the list to the cache
func (cc *ChunkCache) Add(chunk *Chunk, key string, list []Result) {
	if len(key) == 0 || !chunk.IsFull() || len(list) > queryCacheMax {
		return
	}

	cc.mutex.Lock()
	defer cc.mutex.Unlock()

	qc, ok := cc.cache[chunk]
	if !ok {
		cc.cache[chunk] = &queryCache{}
		qc = cc.cache[chunk]
	}
	(*qc)[key] = list
}

// Lookup is called to lookup ChunkCache
func (cc *ChunkCache) Lookup(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 {
		list, ok := (*qc)[key]
		if ok {
			return list
		}
	}
	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
}

Version data entries

87 entries across 87 versions & 1 rubygems

Version Path
doing-2.1.88 lib/helpers/fzf/src/cache.go
doing-2.1.87 lib/helpers/fzf/src/cache.go
doing-2.1.86 lib/helpers/fzf/src/cache.go
doing-2.1.85 lib/helpers/fzf/src/cache.go
doing-2.1.84 lib/helpers/fzf/src/cache.go
doing-2.1.83 lib/helpers/fzf/src/cache.go
doing-2.1.82 lib/helpers/fzf/src/cache.go
doing-2.1.81 lib/helpers/fzf/src/cache.go
doing-2.1.80 lib/helpers/fzf/src/cache.go
doing-2.1.79 lib/helpers/fzf/src/cache.go
doing-2.1.78 lib/helpers/fzf/src/cache.go
doing-2.1.77 lib/helpers/fzf/src/cache.go
doing-2.1.76 lib/helpers/fzf/src/cache.go
doing-2.1.75 lib/helpers/fzf/src/cache.go
doing-2.1.74 lib/helpers/fzf/src/cache.go
doing-2.1.73 lib/helpers/fzf/src/cache.go
doing-2.1.72 lib/helpers/fzf/src/cache.go
doing-2.1.69 lib/helpers/fzf/src/cache.go
doing-2.1.68 lib/helpers/fzf/src/cache.go
doing-2.1.66 lib/helpers/fzf/src/cache.go