Skip to content

Commit

Permalink
fix: can not resuce bytes queue
Browse files Browse the repository at this point in the history
Change-Id: Id068d2a9baf44a9e4e8941464c0316aa0965ac4f
  • Loading branch information
mangotree committed Apr 29, 2020
1 parent 37b9eb2 commit 88d8d54
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
38 changes: 38 additions & 0 deletions bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,44 @@ func TestOldestEntryDeletionWhenMaxCacheSizeIsReached(t *testing.T) {
assertEqual(t, blob('c', 1024*800), entry3)
}

func TestNewEntryWriteWhenMaxCacheSizeIsReached(t *testing.T) {
t.Parallel()

// given
cache, _ := NewBigCache(Config{
Shards: 1,
LifeWindow: 5 * time.Second,
MaxEntriesInWindow: 2,
MaxEntrySize: 400,
HardMaxCacheSize: 1,
})

// when
key1Err := cache.Set("key1", blob('a', 1024*400))
key2Err := cache.Set("key2", blob('b', 1024*400))
key3Err := cache.Set("key3", blob('c', 1024*400))
key4Err := cache.Set("key4", blob('d', 1024*400))

//then
noError(t, key1Err)
noError(t, key2Err)
noError(t, key3Err)
noError(t, key4Err)

// when
_, key1Err = cache.Get("key1")
_, key2Err = cache.Get("key2")
entry3, _ := cache.Get("key3")
entry4, _ := cache.Get("key4")

// then
assertEqual(t, key1Err, ErrEntryNotFound)
assertEqual(t, key2Err, ErrEntryNotFound)
assertEqual(t, blob('c', 1024*400), entry3)
assertEqual(t, blob('d', 1024*400), entry4)

}

func TestRetrievingEntryShouldCopy(t *testing.T) {
t.Parallel()

Expand Down
11 changes: 0 additions & 11 deletions queue/bytes_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var (
// BytesQueue is a non-thread safe queue type of fifo based on bytes array.
// For every push operation index of entry is returned. It can be used to read the entry later
type BytesQueue struct {
full bool
array []byte
capacity int
maxCapacity int
Expand Down Expand Up @@ -78,7 +77,6 @@ func (q *BytesQueue) Reset() {
q.head = leftMarginIndex
q.rightMargin = leftMarginIndex
q.count = 0
q.full = false
}

// Push copies entry at the end of queue and moves tail pointer. Allocates more space if needed.
Expand Down Expand Up @@ -143,9 +141,6 @@ func (q *BytesQueue) push(data []byte, len int) {
if q.tail > q.head {
q.rightMargin = q.tail
}
if q.tail == q.head {
q.full = true
}

q.count++
}
Expand Down Expand Up @@ -238,9 +233,6 @@ func (q *BytesQueue) peek(index int) ([]byte, int, error) {

// canInsertAfterTail returns true if it's possible to insert an entry of size of need after the tail of the queue
func (q *BytesQueue) canInsertAfterTail(need int) bool {
if q.full {
return false
}
if q.tail >= q.head {
return q.capacity-q.tail >= need
}
Expand All @@ -253,9 +245,6 @@ func (q *BytesQueue) canInsertAfterTail(need int) bool {

// canInsertBeforeHead returns true if it's possible to insert an entry of size of need before the head of the queue
func (q *BytesQueue) canInsertBeforeHead(need int) bool {
if q.full {
return false
}
if q.tail >= q.head {
return q.head-leftMarginIndex == need || q.head-leftMarginIndex >= need+minimumHeaderSize
}
Expand Down

0 comments on commit 88d8d54

Please sign in to comment.