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(table cache) block cache logic for table with no compression #2022

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
15 changes: 15 additions & 0 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down