Skip to content

Commit

Permalink
sstable: support columnar blocks in Layout.Describe
Browse files Browse the repository at this point in the history
Adapt Layout.Describe to support describing sstables containing columnar
blocks.
  • Loading branch information
jbowens committed Sep 26, 2024
1 parent c88c747 commit 575f7a0
Show file tree
Hide file tree
Showing 8 changed files with 1,092 additions and 274 deletions.
7 changes: 7 additions & 0 deletions internal/binfmt/binfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ type Formatter struct {

// config
lineWidth int
linePrefix string
offsetFormatStr string
}

// SetLinePrefix sets a prefix for each line of formatted output.
func (f *Formatter) SetLinePrefix(prefix string) {
f.linePrefix = prefix
}

// SetAnchorOffset sets the reference point for relative offset calculations to
// the current offset. Future calls to RelativeOffset() will return an offset
// relative to the current offset.
Expand Down Expand Up @@ -183,6 +189,7 @@ func (f *Formatter) String() string {
binaryLineWidth = max(binaryLineWidth, len(lineData[0]))
}
for _, lineData := range f.lines {
fmt.Fprint(&f.buf, f.linePrefix)
fmt.Fprint(&f.buf, lineData[0])
if len(lineData[1]) > 0 {
if len(lineData[0]) == 0 {
Expand Down
5 changes: 3 additions & 2 deletions sstable/colblk/data_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,9 @@ func (i *DataBlockIter) Init(
keyIter: PrefixBytesIter{},
}
// Allocate a keyIter buffer that's large enough to hold the largest user
// key in the block.
n := int(r.maximumKeyLength)
// key in the block with 1 byte to spare (so that pointer arithmetic is
// never pointing beyond the allocation, which would violate Go rules).
n := int(r.maximumKeyLength) + 1
if cap(i.keyIter.buf) < n {
ptr := mallocgc(uintptr(n), nil, false)
i.keyIter.buf = unsafe.Slice((*byte)(ptr), n)[:0]
Expand Down
2 changes: 1 addition & 1 deletion sstable/colblk_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,6 @@ func (w *RawColumnWriter) flushBufferedIndexBlocks() (rootIndex block.Handle, er
// writing a large file or the index separators happen to be excessively
// long, we may have several index blocks and need to construct a
// "two-level" index structure.
w.props.IndexPartitions = uint64(len(w.indexBuffering.partitions))
switch len(w.indexBuffering.partitions) {
case 0:
// This is impossible because we'll flush the index block immediately
Expand Down Expand Up @@ -696,6 +695,7 @@ func (w *RawColumnWriter) flushBufferedIndexBlocks() (rootIndex block.Handle, er
}
w.props.IndexSize += rootIndex.Length + block.TrailerLen
w.props.IndexType = twoLevelIndex
w.props.IndexPartitions = uint64(len(w.indexBuffering.partitions))
}
return rootIndex, nil
}
Expand Down
10 changes: 10 additions & 0 deletions sstable/colblk_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package sstable

import (
"bytes"
"context"
"fmt"
"testing"
Expand All @@ -30,8 +31,10 @@ func TestColumnarWriter(t *testing.T) {
}
}()
keySchema := colblk.DefaultKeySchema(testkeys.Comparer, 16)
var buf bytes.Buffer
datadriven.Walk(t, "testdata/columnar_writer", func(t *testing.T, path string) {
datadriven.RunTest(t, path, func(t *testing.T, td *datadriven.TestData) string {
buf.Reset()
switch td.Cmd {
case "build":
var writerOpts WriterOptions
Expand Down Expand Up @@ -60,6 +63,13 @@ func TestColumnarWriter(t *testing.T) {
})
require.NoError(t, err)
return "ok"
case "layout":
l, err := r.Layout()
if err != nil {
return err.Error()
}
l.Describe(&buf, true /* verbose */, r, nil)
return buf.String()
case "props":
return r.Properties.String()
case "describe-binary":
Expand Down
10 changes: 10 additions & 0 deletions sstable/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package sstable
import (
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/sstable/block"
"github.com/cockroachdb/pebble/sstable/colblk"
"github.com/cockroachdb/pebble/sstable/rowblk"
)

// TableFormat specifies the format version for sstables. The legacy LevelDB
Expand Down Expand Up @@ -251,6 +254,13 @@ func (f TableFormat) BlockColumnar() bool {
return f >= TableFormatPebblev5
}

func (f TableFormat) newIndexIter() block.IndexBlockIterator {
if !f.BlockColumnar() {
return new(rowblk.IndexIter)
}
return new(colblk.IndexIter)
}

// AsTuple returns the TableFormat's (Magic String, Version) tuple.
func (f TableFormat) AsTuple() (string, uint32) {
switch f {
Expand Down
Loading

0 comments on commit 575f7a0

Please sign in to comment.