Skip to content

Commit

Permalink
fix block cache logic for table with no compression
Browse files Browse the repository at this point in the history
  • Loading branch information
hugy718 committed Oct 17, 2023
1 parent fb1b009 commit d01e622
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
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

0 comments on commit d01e622

Please sign in to comment.