chunk cache adds function ReadChunkAt

This commit is contained in:
chrislu
2022-02-25 21:55:04 -08:00
parent fc7a4957ea
commit 3ad5fa6f6f
5 changed files with 125 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package chunk_cache
import (
"bytes"
"fmt"
"github.com/chrislusf/seaweedfs/weed/util/mem"
"math/rand"
"testing"
)
@@ -18,7 +19,7 @@ func TestOnDisk(t *testing.T) {
type test_data struct {
data []byte
fileId string
size uint64
size int
}
testData := make([]*test_data, writeCount)
for i := 0; i < writeCount; i++ {
@@ -27,29 +28,35 @@ func TestOnDisk(t *testing.T) {
testData[i] = &test_data{
data: buff,
fileId: fmt.Sprintf("1,%daabbccdd", i+1),
size: uint64(len(buff)),
size: len(buff),
}
cache.SetChunk(testData[i].fileId, testData[i].data)
// read back right after write
data := cache.GetChunk(testData[i].fileId, testData[i].size)
data := mem.Allocate(testData[i].size)
cache.ReadChunkAt(data, testData[i].fileId, 0)
if bytes.Compare(data, testData[i].data) != 0 {
t.Errorf("failed to write to and read from cache: %d", i)
}
mem.Free(data)
}
for i := 0; i < 2; i++ {
data := cache.GetChunk(testData[i].fileId, testData[i].size)
data := mem.Allocate(testData[i].size)
cache.ReadChunkAt(data, testData[i].fileId, 0)
if bytes.Compare(data, testData[i].data) == 0 {
t.Errorf("old cache should have been purged: %d", i)
}
mem.Free(data)
}
for i := 2; i < writeCount; i++ {
data := cache.GetChunk(testData[i].fileId, testData[i].size)
data := mem.Allocate(testData[i].size)
cache.ReadChunkAt(data, testData[i].fileId, 0)
if bytes.Compare(data, testData[i].data) != 0 {
t.Errorf("failed to write to and read from cache: %d", i)
}
mem.Free(data)
}
cache.Shutdown()
@@ -57,10 +64,12 @@ func TestOnDisk(t *testing.T) {
cache = NewTieredChunkCache(2, tmpDir, totalDiskSizeInKB, 1024)
for i := 0; i < 2; i++ {
data := cache.GetChunk(testData[i].fileId, testData[i].size)
data := mem.Allocate(testData[i].size)
cache.ReadChunkAt(data, testData[i].fileId, 0)
if bytes.Compare(data, testData[i].data) == 0 {
t.Errorf("old cache should have been purged: %d", i)
}
mem.Free(data)
}
for i := 2; i < writeCount; i++ {
@@ -83,10 +92,12 @@ func TestOnDisk(t *testing.T) {
*/
continue
}
data := cache.GetChunk(testData[i].fileId, testData[i].size)
data := mem.Allocate(testData[i].size)
cache.ReadChunkAt(data, testData[i].fileId, 0)
if bytes.Compare(data, testData[i].data) != 0 {
t.Errorf("failed to write to and read from cache: %d", i)
}
mem.Free(data)
}
cache.Shutdown()