Skip to content

Commit

Permalink
Merge pull request #4 from bparli/fix/bytes-counts
Browse files Browse the repository at this point in the history
Fix size calculation
  • Loading branch information
bparli authored Nov 24, 2020
2 parents 45efd6a + 7873796 commit 7fd5b96
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
23 changes: 14 additions & 9 deletions simplelfuda/lfuda.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package simplelfuda

import (
"container/list"
"encoding/binary"
"fmt"
)

Expand Down Expand Up @@ -111,15 +112,7 @@ func (l *LFUDA) Set(key interface{}, value interface{}) bool {
} else {
// check if we need to evict
// convert to bytes so we can get the size of the value

var numBytes float64
// if the value is binary
if valBytes, ok := value.([]byte); ok {
numBytes = float64(len(valBytes))
} else {
// otherwise use the default format
numBytes = float64(len([]byte(fmt.Sprintf("%v", value.(interface{})))))
}
numBytes := calcBytes(value)

// check this value will even fit in the cache. if not just return
if l.size < numBytes {
Expand Down Expand Up @@ -305,3 +298,15 @@ func gdsfPolicy(element *item, cacheAge float64) float64 {
func lfuPolicy(element *item, cacheAge float64) float64 {
return element.hits
}

func calcBytes(value interface{}) float64 {
// if the value is binary
if valBytes, ok := value.([]byte); ok {
return float64(len(valBytes))
} else if valBytes := binary.Size(value); valBytes != -1 {
return float64(valBytes)
} else {
// otherwise use the default format
return float64(len([]byte(fmt.Sprintf("%v", value.(interface{})))))
}
}
35 changes: 35 additions & 0 deletions simplelfuda/lfuda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,38 @@ func TestEvictLFU(t *testing.T) {
t.Errorf("cache should still contain key a")
}
}

func TestCalcBytes(t *testing.T) {
a := make([]int16, 0, 1) // 0
b := [...]int8{2, 3, 5, 7, 11} // 5
c := []int64{2, 3, 5, 7, 11} // 40
d := []int32{} // 0

if res := calcBytes(append(a, 2)); res != float64(2) {
t.Errorf("Size is not correct. Got %f but should be %d", res, 3)
}

if res := calcBytes(b); res != float64(5) {
t.Errorf("Size is not correct. Got %f but should be %d", res, 5)
}
if res := calcBytes(c); res != float64(40) {
t.Errorf("Size is not correct. Got %f but should be %d", res, 40)
}
if res := calcBytes(d); res != float64(0) {
t.Errorf("Size is not correct. Got %f but should be %d", res, 0)
}

tests := map[interface{}]int{
true: 1,
int16(5): 2,
1278624387349734: 16,
"We must not confuse dissent with disloyalty. We must remember always that accusation is not proof and that conviction depends upon evidence and due process of law. We will not walk in fear, one of another. We will not be driven by fear into an age of unreason, if we dig deep in our history and our doctrine, and remember that we are not descended from fearful men — not from men who feared to write, to speak, to associate and to defend causes that were, for the moment, unpopular.": 484,
12: 2,
}

for test, res := range tests {
if calcBytes(test) != float64(res) {
t.Errorf("cache should have size 10 bytes at this point: %d", res)
}
}
}

0 comments on commit 7fd5b96

Please sign in to comment.