From d01e622973593d0a2abdcbe177a6770ce063984b Mon Sep 17 00:00:00 2001 From: hugy718 Date: Tue, 17 Oct 2023 17:07:08 +0800 Subject: [PATCH] fix block cache logic for table with no compression --- table/table.go | 5 +++++ table/table_test.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/table/table.go b/table/table.go index 010cbd1cf..b7970d8e0 100644 --- a/table/table.go +++ b/table/table.go @@ -595,6 +595,11 @@ func (t *Table) block(idx int, useCache bool) (*block, error) { "corrupted or the table options are incorrectly set") } + if useCache && t.opt.BlockCache != nil && t.opt.Compression == options.None && !t.shouldDecrypt() { + // make a copy for caching later + blk.data = y.Copy(blk.data) + } + // Read checksum and store it readPos -= blk.chkLen blk.checksum = blk.data[readPos : readPos+blk.chkLen] diff --git a/table/table_test.go b/table/table_test.go index d1fef13b7..f28773eca 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -32,6 +32,7 @@ import ( "github.com/cespare/xxhash/v2" "github.com/stretchr/testify/require" + "github.com/dgraph-io/badger/v4/fb" "github.com/dgraph-io/badger/v4/options" "github.com/dgraph-io/badger/v4/y" "github.com/dgraph-io/ristretto" @@ -85,6 +86,20 @@ func buildTable(t *testing.T, keyValues [][]string, opts Options) *Table { return tbl } +func TestTableCacheNoCompression(t *testing.T) { + opts := getTestTableOptions() + opts.Compression = options.None + opts.BlockCache, _ = ristretto.NewCache(&cacheConfig) + + table := buildTestTable(t, "key", 1000, opts) + blockIdx := 0 + var ko fb.BlockOffset + y.AssertTrue(table.offsets(&ko, blockIdx)) + expected := ko.Len() + blk, _ := table.block(blockIdx, true) + require.Equal(t, int(expected), cap(blk.data), "incorrect cached block size") +} + func TestTableIterator(t *testing.T) { for _, n := range []int{99, 100, 101} { t.Run(fmt.Sprintf("n=%d", n), func(t *testing.T) {