Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: can not resuce bytes queue #218

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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