diff --git a/cache.go b/cache.go index a2147cbc..7379fd05 100644 --- a/cache.go +++ b/cache.go @@ -123,11 +123,11 @@ type item struct { func NewCache(config *Config) (*Cache, error) { switch { case config.NumCounters == 0: - return nil, errors.New("NumCounters can't be zero.") + return nil, errors.New("NumCounters can't be zero") case config.MaxCost == 0: - return nil, errors.New("MaxCost can't be zero.") + return nil, errors.New("MaxCost can't be zero") case config.BufferItems == 0: - return nil, errors.New("BufferItems can't be zero.") + return nil, errors.New("BufferItems can't be zero") } policy := newPolicy(config.NumCounters, config.MaxCost) cache := &Cache{ diff --git a/cache_test.go b/cache_test.go index 7035bdda..c99f8535 100644 --- a/cache_test.go +++ b/cache_test.go @@ -6,7 +6,7 @@ import ( "time" ) -var wait time.Duration = time.Millisecond * 10 +var wait = time.Millisecond * 10 func TestCache(t *testing.T) { if _, err := NewCache(&Config{ @@ -135,10 +135,8 @@ func TestCacheSet(t *testing.T) { if val, ok := c.Get(1); val == nil || val.(int) != 1 || !ok { t.Fatal("set/get returned wrong value") } - } else { - if val, ok := c.Get(1); val != nil || ok { - t.Fatal("set was dropped but value still added") - } + } else if val, ok := c.Get(1); val != nil || ok { + t.Fatal("set was dropped but value still added") } c.Set(1, 2, 2) if val, ok := c.store.Get(1); val == nil || val.(int) != 2 || !ok { diff --git a/policy.go b/policy.go index 4845174d..45da5b81 100644 --- a/policy.go +++ b/policy.go @@ -155,11 +155,11 @@ func (p *defaultPolicy) Add(key uint64, cost int64) ([]*item, bool) { // fill up empty slots in sample sample = p.evict.fillSample(sample) // find minimally used item in sample - minKey, minHits, minId, minCost := uint64(0), int64(math.MaxInt64), 0, int64(0) + minKey, minHits, minID, minCost := uint64(0), int64(math.MaxInt64), 0, int64(0) for i, pair := range sample { // look up hit count for sample key if hits := p.admit.Estimate(pair.key); hits < minHits { - minKey, minHits, minId, minCost = pair.key, hits, i, pair.cost + minKey, minHits, minID, minCost = pair.key, hits, i, pair.cost } } // if the incoming item isn't worth keeping in the policy, reject. @@ -170,7 +170,7 @@ func (p *defaultPolicy) Add(key uint64, cost int64) ([]*item, bool) { // delete the victim from metadata p.evict.del(minKey) // delete the victim from sample - sample[minId] = sample[len(sample)-1] + sample[minID] = sample[len(sample)-1] sample = sample[:len(sample)-1] // store victim in evicted victims slice victims = append(victims, &item{ @@ -197,7 +197,7 @@ func (p *defaultPolicy) Del(key uint64) { func (p *defaultPolicy) Cap() int64 { p.Lock() - capacity := int64(p.evict.maxCost - p.evict.used) + capacity := p.evict.maxCost - p.evict.used p.Unlock() return capacity } @@ -325,7 +325,7 @@ func (p *tinyLFU) Push(keys []uint64) { func (p *tinyLFU) Estimate(key uint64) int64 { hits := p.freq.Estimate(key) if p.door.Has(key) { - hits += 1 + hits++ } return hits } diff --git a/sketch.go b/sketch.go index c2b8c0f4..6db23d58 100644 --- a/sketch.go +++ b/sketch.go @@ -102,7 +102,7 @@ func newCmRow(numCounters int64) cmRow { } func (r cmRow) get(n uint64) byte { - return byte(r[n/2]>>((n&1)*4)) & 0x0f + return (r[n/2] >> ((n & 1) * 4)) & 0x0f } func (r cmRow) increment(n uint64) { diff --git a/z/bbloom.go b/z/bbloom.go index 14b967fc..4ac314be 100644 --- a/z/bbloom.go +++ b/z/bbloom.go @@ -113,8 +113,7 @@ func (bl Bloom) Has(hash uint64) bool { h := hash >> bl.shift l := hash << bl.shift >> bl.shift for i := uint64(0); i < bl.setLocs; i++ { - switch bl.IsSet((h + i*l) & bl.size) { - case false: + if !bl.IsSet((h + i*l) & bl.size) { return false } } diff --git a/z/bbloom_test.go b/z/bbloom_test.go index 6d45b45d..e6e4d0a7 100644 --- a/z/bbloom_test.go +++ b/z/bbloom_test.go @@ -3,6 +3,7 @@ package z import ( "crypto/rand" "fmt" + "os" "testing" ) @@ -22,8 +23,8 @@ func TestMain(m *testing.M) { fmt.Println("\n###############\nbbloom_test.go") fmt.Print("Benchmarks relate to 2**16 OP. --> output/65536 op/ns\n###############\n\n") - m.Run() - + exitVal := m.Run() + os.Exit(exitVal) } func TestM_NumberOfWrongs(t *testing.T) { @@ -37,7 +38,6 @@ func TestM_NumberOfWrongs(t *testing.T) { } } fmt.Printf("Bloomfilter New(7* 2**16, 7) (-> size=%v bit): \n Check for 'false positives': %v wrong positive 'Has' results on 2**16 entries => %v %%\n", len(bf.bitset)<<6, cnt, float64(cnt)/float64(n)) - } func TestM_JSON(t *testing.T) { @@ -53,10 +53,10 @@ func TestM_JSON(t *testing.T) { } } - Json := bf.JSONMarshal() + data := bf.JSONMarshal() - // create new bloomfilter from bloomfilter's JSON representation - bf2 := JSONUnmarshal(Json) + // create new bloomfilter from bloomfilter's data representation + bf2 := JSONUnmarshal(data) cnt2 := 0 for i := range wordlist1 { @@ -69,7 +69,6 @@ func TestM_JSON(t *testing.T) { if cnt2 != shallBe { t.Errorf("FAILED !AddIfNotHasBytes = %v; want %v", cnt2, shallBe) } - } func BenchmarkM_New(b *testing.B) { @@ -99,7 +98,6 @@ func BenchmarkM_Add(b *testing.B) { bf.Add(hash) } } - } func BenchmarkM_Has(b *testing.B) { diff --git a/z/rtutil_test.go b/z/rtutil_test.go index 055a1915..5c5af734 100644 --- a/z/rtutil_test.go +++ b/z/rtutil_test.go @@ -53,7 +53,6 @@ func SipHash(p []byte) (l, h uint64) { // Compression. for len(p) >= 8 { - m := uint64(p[0]) | uint64(p[1])<<8 | uint64(p[2])<<16 | uint64(p[3])<<24 | uint64(p[4])<<32 | uint64(p[5])<<40 | uint64(p[6])<<48 | uint64(p[7])<<56 @@ -252,7 +251,6 @@ func SipHash(p []byte) (l, h uint64) { h = hash >> 1 l = hash << 1 >> 1 return l, h - } func BenchmarkNanoTime(b *testing.B) { for i := 0; i < b.N; i++ {