Skip to content

Commit

Permalink
Code organizing
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavdangeti committed Apr 16, 2024
1 parent f43df2c commit 11a2ad6
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions faiss_vector_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,19 @@ import (
faiss "github.com/blevesearch/go-faiss"
)

type vectorIndexCache struct {
closeCh chan struct{}
m sync.RWMutex
cache map[uint16]*cacheEntry
}

type ewma struct {
alpha float64
avg float64
// every hit to the cache entry is recorded as part of a sample
// which will be used to calculate the average in the next cycle of average
// computation (which is average traffic for the field till now). this is
// used to track the per second hits to the cache entries.
sample uint64
}

func newVectorIndexCache() *vectorIndexCache {
return &vectorIndexCache{
cache: make(map[uint16]*cacheEntry),
closeCh: make(chan struct{}),
}
}

type vectorIndexCache struct {
closeCh chan struct{}
m sync.RWMutex
cache map[uint16]*cacheEntry
}

func (vc *vectorIndexCache) Clear() {
vc.m.Lock()
close(vc.closeCh)
Expand Down Expand Up @@ -231,6 +221,18 @@ func (vc *vectorIndexCache) monitor() {
}
}

// -----------------------------------------------------------------------------

type ewma struct {
alpha float64
avg float64
// every hit to the cache entry is recorded as part of a sample
// which will be used to calculate the average in the next cycle of average
// computation (which is average traffic for the field till now). this is
// used to track the per second hits to the cache entries.
sample uint64
}

func (e *ewma) add(val uint64) {
if e.avg == 0.0 {
e.avg = float64(val)
Expand All @@ -241,6 +243,8 @@ func (e *ewma) add(val uint64) {
}
}

// -----------------------------------------------------------------------------

func createCacheEntry(index *faiss.IndexImpl, vecDocIDMap map[int64]uint32, alpha float64) *cacheEntry {
return &cacheEntry{
index: index,
Expand Down

0 comments on commit 11a2ad6

Please sign in to comment.