Skip to content

Commit

Permalink
sstable: refactor sstable layout facilities, add decodeLayout
Browse files Browse the repository at this point in the history
This commit refactors some of the code surrounding the parsing and construction
of a sstable.Layout. It also adds a new decodeLayout helper that parses an
in-memory sstable's Layout without opening a Reader. Future work will use
decodeLayout to describe the binary layout of a sstable with columnar blocks in
tests.
  • Loading branch information
jbowens committed Sep 6, 2024
1 parent 729b6f7 commit b3a81bd
Show file tree
Hide file tree
Showing 14 changed files with 366 additions and 123 deletions.
2 changes: 2 additions & 0 deletions sstable/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ type DataBlockIterator interface {
// iterators over index blocks. It's currently satisifed by the
// *rowblk.IndexIter type.
type IndexBlockIterator interface {
// Init initializes the block iterator from the provided block.
Init(base.Compare, base.Split, []byte, IterTransforms) error
// InitHandle initializes an iterator from the provided block handle.
InitHandle(base.Compare, base.Split, BufferHandle, IterTransforms) error
// Valid returns true if the iterator is currently positioned at a valid
Expand Down
20 changes: 14 additions & 6 deletions sstable/colblk/index_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,19 @@ type IndexIter struct {
// Assert that IndexIter satisfies the block.IndexBlockIterator interface.
var _ block.IndexBlockIterator = (*IndexIter)(nil)

// Init initializes an index iterator from the provided reader.
func (i *IndexIter) Init(r *IndexReader) {
*i = IndexIter{r: r, n: int(r.br.header.Rows)}
// InitReader initializes an index iterator from the provided reader.
func (i *IndexIter) InitReader(r *IndexReader) {
*i = IndexIter{r: r, n: int(r.br.header.Rows), allocReader: i.allocReader}
}

// Init initializes an iterator from the provided block data slice.
func (i *IndexIter) Init(
cmp base.Compare, split base.Split, blk []byte, transforms block.IterTransforms,
) error {
// TODO(jackson): Handle the transforms.
i.allocReader.Init(blk)
i.InitReader(&i.allocReader)
return nil
}

// InitHandle initializes an iterator from the provided block handle.
Expand All @@ -181,9 +191,7 @@ func (i *IndexIter) InitHandle(
// overhead can be material.)
i.h.Release()
i.h = block
i.allocReader.Init(i.h.Get())
i.r = &i.allocReader
return nil
return i.Init(cmp, split, i.h.Get(), transforms)
}

// RowIndex returns the index of the block entry at the iterator's current
Expand Down
2 changes: 1 addition & 1 deletion sstable/colblk/index_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestIndexBlock(t *testing.T) {
return buf.String()
case "iter":
var it IndexIter
it.Init(&r)
it.InitReader(&r)
for _, line := range strings.Split(d.Input, "\n") {
fields := strings.Fields(line)
var valid bool
Expand Down
25 changes: 19 additions & 6 deletions sstable/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ func optsFromArgs(td *datadriven.TestData, writerOpts *WriterOptions) error {
return nil
}

func runBuildCmd(
td *datadriven.TestData, writerOpts *WriterOptions, cacheSize int,
) (*WriterMetadata, *Reader, error) {
f0 := &objstorage.MemObj{}
func runBuildMemObjCmd(
td *datadriven.TestData, writerOpts *WriterOptions,
) (*WriterMetadata, *objstorage.MemObj, error) {
obj := &objstorage.MemObj{}
if err := optsFromArgs(td, writerOpts); err != nil {
return nil, nil, err
}

w := NewRawWriter(f0, *writerOpts)
w := NewRawWriter(obj, *writerOpts)
defer func() {
if w != nil {
_ = w.Close()
Expand Down Expand Up @@ -122,7 +122,10 @@ func runBuildCmd(
if err != nil {
return nil, nil, err
}
return meta, obj, nil
}

func openReader(obj *objstorage.MemObj, writerOpts *WriterOptions, cacheSize int) (*Reader, error) {
readerOpts := ReaderOptions{Comparer: writerOpts.Comparer}
if writerOpts.FilterPolicy != nil {
readerOpts.Filters = map[string]FilterPolicy{
Expand All @@ -138,7 +141,17 @@ func runBuildCmd(
},
})
}
r, err := NewMemReader(f0.Data(), readerOpts)
return NewMemReader(obj.Data(), readerOpts)
}

func runBuildCmd(
td *datadriven.TestData, writerOpts *WriterOptions, cacheSize int,
) (*WriterMetadata, *Reader, error) {
meta, obj, err := runBuildMemObjCmd(td, writerOpts)
if err != nil {
return nil, nil, err
}
r, err := openReader(obj, writerOpts, cacheSize)
if err != nil {
return nil, nil, err
}
Expand Down
Loading

0 comments on commit b3a81bd

Please sign in to comment.