Skip to content

Commit

Permalink
sstable: remove iterAdapter
Browse files Browse the repository at this point in the history
Remove the iterAdapter type, updating unit tests to use the current
InternalIterator API.
  • Loading branch information
jbowens committed Sep 16, 2024
1 parent fc50f62 commit 98fae22
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 194 deletions.
80 changes: 44 additions & 36 deletions sstable/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,35 +241,36 @@ func runIterCmdMaskingFilter(maskingFilter TestKeysMaskingFilter) runIterCmdOpti
}

func runIterCmd(
td *datadriven.TestData, origIter Iterator, printValue bool, opt ...runIterCmdOption,
td *datadriven.TestData, iter Iterator, printValue bool, opt ...runIterCmdOption,
) string {
var opts runIterCmdOptions
for _, o := range opt {
o(&opts)
}

iter := newIterAdapter(origIter)
defer iter.Close()

var b bytes.Buffer
var prefix []byte
var maskingSuffix []byte
skipMaskedKeys := func(direction int) {
var kv *base.InternalKV
skipMaskedKeys := func(direction int, kv *base.InternalKV) *base.InternalKV {
if len(maskingSuffix) == 0 {
return
return kv
}
for iter.Valid() {
k := iter.Key().UserKey
for kv != nil {
k := kv.K.UserKey
suffix := k[testkeys.Comparer.Split(k):]
if len(suffix) == 0 || testkeys.Comparer.CompareSuffixes(suffix, maskingSuffix) <= 0 {
return
return kv
}
if direction > 0 {
iter.Next()
kv = iter.Next()
} else {
iter.Prev()
kv = iter.Prev()
}
}
return kv
}
for _, line := range crstrings.Lines(td.Input) {
parts := strings.Fields(line)
Expand All @@ -287,8 +288,8 @@ func runIterCmd(
flags = flags.EnableTrySeekUsingNext()
}
}
iter.SeekGE([]byte(strings.TrimSpace(parts[1])), flags)
skipMaskedKeys(+1)
kv = iter.SeekGE([]byte(strings.TrimSpace(parts[1])), flags)
kv = skipMaskedKeys(+1, kv)
case "seek-prefix-ge":
if len(parts) != 2 && len(parts) != 3 {
return "seek-prefix-ge <key> [<try-seek-using-next>]\n"
Expand All @@ -302,44 +303,44 @@ func runIterCmd(
flags = flags.EnableTrySeekUsingNext()
}
}
iter.SeekPrefixGE(prefix, prefix /* key */, flags)
skipMaskedKeys(+1)
kv = iter.SeekPrefixGE(prefix, prefix /* key */, flags)
kv = skipMaskedKeys(+1, kv)
case "seek-lt":
if len(parts) != 2 {
return "seek-lt <key>\n"
}
prefix = nil
iter.SeekLT([]byte(strings.TrimSpace(parts[1])), base.SeekLTFlagsNone)
skipMaskedKeys(-1)
kv = iter.SeekLT([]byte(strings.TrimSpace(parts[1])), base.SeekLTFlagsNone)
kv = skipMaskedKeys(-1, kv)
case "first":
prefix = nil
iter.First()
skipMaskedKeys(+1)
kv = iter.First()
kv = skipMaskedKeys(+1, kv)
case "last":
prefix = nil
iter.Last()
skipMaskedKeys(-1)
kv = iter.Last()
kv = skipMaskedKeys(-1, kv)
case "next":
iter.Next()
skipMaskedKeys(+1)
kv = iter.Next()
kv = skipMaskedKeys(+1, kv)
case "next-ignore-result":
iter.NextIgnoreResult()
_ = iter.Next()
case "prev":
iter.Prev()
skipMaskedKeys(-1)
kv = iter.Prev()
kv = skipMaskedKeys(-1, kv)
case "next-prefix":
if len(parts) != 1 {
return "next-prefix should have no parameter\n"
}
if iter.Key() == nil {
if kv == nil {
return "next-prefix cannot be called on exhauster iterator\n"
}
k := iter.Key().UserKey
k := kv.K.UserKey
prefixLen := testkeys.Comparer.Split(k)
k = k[:prefixLen]
kSucc := testkeys.Comparer.ImmediateSuccessor(nil, k)
iter.NextPrefix(kSucc)
skipMaskedKeys(+1)
kv = iter.NextPrefix(kSucc)
kv = skipMaskedKeys(+1, kv)
case "set-bounds":
if len(parts) <= 1 || len(parts) > 3 {
return "set-bounds lower=<lower> upper=<upper>\n"
Expand All @@ -364,6 +365,7 @@ func runIterCmd(
}
}
iter.SetBounds(lower, upper)
kv = nil
case "stats":
// The timing is non-deterministic, so set to 0.
opts.stats.BlockReadDuration = 0
Expand All @@ -379,9 +381,9 @@ func runIterCmd(
}
continue
case "internal-iter-state":
fmt.Fprintf(&b, "| %T:\n", origIter)
si, _ := origIter.(*singleLevelIteratorRowBlocks)
if twoLevelIter, ok := origIter.(*twoLevelIteratorRowBlocks); ok {
fmt.Fprintf(&b, "| %T:\n", iter)
si, _ := iter.(*singleLevelIteratorRowBlocks)
if twoLevelIter, ok := iter.(*twoLevelIteratorRowBlocks); ok {
si = &twoLevelIter.secondLevel
if twoLevelIter.topLevelIndex.Valid() {
fmt.Fprintf(&b, "| topLevelIndex.Key() = %q\n", twoLevelIter.topLevelIndex.Separator())
Expand Down Expand Up @@ -421,13 +423,19 @@ func runIterCmd(
if opts.everyOp != nil {
opts.everyOp(&b)
}
if iter.Valid() && checkValidPrefix(prefix, iter.Key().UserKey) {
fmt.Fprintf(&b, "<%s:%d>", iter.Key().UserKey, iter.Key().SeqNum())
var v []byte
err := iter.Error()
if err == nil && kv != nil {
v, _, err = kv.Value(nil)
}

if err != nil {
fmt.Fprintf(&b, "<err=%v>", err)
} else if kv != nil && checkValidPrefix(prefix, kv.K.UserKey) {
fmt.Fprintf(&b, "<%s:%d>", kv.K.UserKey, kv.K.SeqNum())
if printValue {
fmt.Fprintf(&b, ":%s", string(iter.Value()))
fmt.Fprintf(&b, ":%s", string(v))
}
} else if err := iter.Error(); err != nil {
fmt.Fprintf(&b, "<err=%v>", err)
} else {
fmt.Fprintf(&b, ".")
}
Expand Down
93 changes: 0 additions & 93 deletions sstable/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,99 +92,6 @@ func (r *Reader) get(key []byte) (value []byte, err error) {
return newValue, nil
}

// iterAdapter adapts the new Iterator API which returns the key and value from
// positioning methods (Seek*, First, Last, Next, Prev) to the old API which
// returned a boolean corresponding to Valid. Only used by test code.
type iterAdapter struct {
Iterator
key *InternalKey
val []byte
}

func newIterAdapter(iter Iterator) *iterAdapter {
return &iterAdapter{
Iterator: iter,
}
}

func (i *iterAdapter) update(kv *base.InternalKV) bool {
if kv == nil {
i.key = nil
i.val = nil
return false
}
i.key = &kv.K
if v, _, err := kv.Value(nil); err != nil {
i.key = nil
i.val = nil
} else {
i.val = v
}
return i.key != nil
}

func (i *iterAdapter) String() string {
return "iter-adapter"
}

func (i *iterAdapter) SeekGE(key []byte, flags base.SeekGEFlags) bool {
return i.update(i.Iterator.SeekGE(key, flags))
}

func (i *iterAdapter) SeekPrefixGE(prefix, key []byte, flags base.SeekGEFlags) bool {
return i.update(i.Iterator.SeekPrefixGE(prefix, key, flags))
}

func (i *iterAdapter) SeekLT(key []byte, flags base.SeekLTFlags) bool {
return i.update(i.Iterator.SeekLT(key, flags))
}

func (i *iterAdapter) First() bool {
return i.update(i.Iterator.First())
}

func (i *iterAdapter) Last() bool {
return i.update(i.Iterator.Last())
}

func (i *iterAdapter) Next() bool {
return i.update(i.Iterator.Next())
}

func (i *iterAdapter) NextPrefix(succKey []byte) bool {
return i.update(i.Iterator.NextPrefix(succKey))
}

func (i *iterAdapter) NextIgnoreResult() {
i.Iterator.Next()
i.update(nil)
}

func (i *iterAdapter) Prev() bool {
return i.update(i.Iterator.Prev())
}

func (i *iterAdapter) Key() *InternalKey {
return i.key
}

func (i *iterAdapter) Value() []byte {
return i.val
}

func (i *iterAdapter) Valid() bool {
return i.key != nil
}

func (i *iterAdapter) SetBounds(lower, upper []byte) {
i.Iterator.SetBounds(lower, upper)
i.key = nil
}

func (i *iterAdapter) SetContext(ctx context.Context) {
i.Iterator.SetContext(ctx)
}

func TestVirtualReader(t *testing.T) {
defer leaktest.AfterTest(t)()
t.Run("props", func(t *testing.T) {
Expand Down
Loading

0 comments on commit 98fae22

Please sign in to comment.