Skip to content

Commit

Permalink
internal/keyspan: refactor interleaving iterator
Browse files Browse the repository at this point in the history
This commit refactors the interleaving iterator with a goal of simplifying its
logic, but also to support a future mode in which the interleaving iterator may
interleave end boundaries during iteration.

The logic to interleave span bounds and point keys is disentangled from the
logic to determine when to yield a key-value pair to the caller. This also
allows the yielding logic to be shared among forward and reverse iteration.
  • Loading branch information
jbowens committed Aug 25, 2023
1 parent 3e9abad commit 1253033
Show file tree
Hide file tree
Showing 12 changed files with 467 additions and 451 deletions.
6 changes: 3 additions & 3 deletions compaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ func (c *compaction) newInputIter(
if rangeKeyIter := f.newRangeKeyIter(nil); rangeKeyIter != nil {
mi := &keyspan.MergingIter{}
mi.Init(c.cmp, rangeKeyCompactionTransform(c.equal, snapshots, c.elideRangeKey), new(keyspan.MergingBuffers), rangeKeyIter)
c.rangeKeyInterleaving.Init(c.comparer, iter, mi, nil /* hooks */, nil /* lowerBound */, nil /* upperBound */)
c.rangeKeyInterleaving.Init(c.comparer, iter, mi, keyspan.InterleavingIterOpts{})
iter = &c.rangeKeyInterleaving
}
return iter, nil
Expand All @@ -1271,7 +1271,7 @@ func (c *compaction) newInputIter(
if len(rangeKeyIters) > 0 {
mi := &keyspan.MergingIter{}
mi.Init(c.cmp, rangeKeyCompactionTransform(c.equal, snapshots, c.elideRangeKey), new(keyspan.MergingBuffers), rangeKeyIters...)
c.rangeKeyInterleaving.Init(c.comparer, iter, mi, nil /* hooks */, nil /* lowerBound */, nil /* upperBound */)
c.rangeKeyInterleaving.Init(c.comparer, iter, mi, keyspan.InterleavingIterOpts{})
iter = &c.rangeKeyInterleaving
}
return iter, nil
Expand Down Expand Up @@ -1557,7 +1557,7 @@ func (c *compaction) newInputIter(
mi.Init(c.cmp, rangeKeyCompactionTransform(c.equal, snapshots, c.elideRangeKey), new(keyspan.MergingBuffers), rangeKeyIters...)
di := &keyspan.DefragmentingIter{}
di.Init(c.comparer, mi, keyspan.DefragmentInternal, keyspan.StaticDefragmentReducer, new(keyspan.DefragmentingBuffers))
c.rangeKeyInterleaving.Init(c.comparer, pointKeyIter, di, nil /* hooks */, nil /* lowerBound */, nil /* upperBound */)
c.rangeKeyInterleaving.Init(c.comparer, pointKeyIter, di, keyspan.InterleavingIterOpts{})
return &c.rangeKeyInterleaving, nil
}

Expand Down
2 changes: 1 addition & 1 deletion compaction_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestCompactionIter(t *testing.T) {
base.DefaultComparer,
fi,
keyspan.NewIter(base.DefaultComparer.Compare, rangeKeys),
nil, nil, nil)
keyspan.InterleavingIterOpts{})
iter := newInvalidatingIter(interleavingIter)
iter.ignoreKind(InternalKeyKindRangeDelete)
if merge == nil {
Expand Down
11 changes: 9 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,11 @@ func finishInitializingIter(ctx context.Context, buf *iterAlloc) *Iterator {
// dbi already had an initialized range key iterator, in case the point
// iterator changed or the range key masking suffix changed.
dbi.rangeKey.iiter.Init(&dbi.comparer, dbi.iter, dbi.rangeKey.rangeKeyIter,
&dbi.rangeKeyMasking, dbi.opts.LowerBound, dbi.opts.UpperBound)
keyspan.InterleavingIterOpts{
Mask: &dbi.rangeKeyMasking,
LowerBound: dbi.opts.LowerBound,
UpperBound: dbi.opts.UpperBound,
})
dbi.iter = &dbi.rangeKey.iiter
}
} else {
Expand Down Expand Up @@ -1314,7 +1318,10 @@ func finishInitializingInternalIter(buf *iterAlloc, i *scanInternalIterator) *sc
// iterator that interleaves range keys pulled from
// i.rangeKey.rangeKeyIter.
i.rangeKey.iiter.Init(i.comparer, i.iter, i.rangeKey.rangeKeyIter,
nil /* mask */, i.opts.LowerBound, i.opts.UpperBound)
keyspan.InterleavingIterOpts{
LowerBound: i.opts.LowerBound,
UpperBound: i.opts.UpperBound,
})
i.iter = &i.rangeKey.iiter

return i
Expand Down
8 changes: 6 additions & 2 deletions external_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,12 @@ func finishInitializingExternal(ctx context.Context, it *Iterator) {
}
}
if it.rangeKey != nil {
it.rangeKey.iiter.Init(&it.comparer, it.iter, it.rangeKey.rangeKeyIter, &it.rangeKeyMasking,
it.opts.LowerBound, it.opts.UpperBound)
it.rangeKey.iiter.Init(&it.comparer, it.iter, it.rangeKey.rangeKeyIter,
keyspan.InterleavingIterOpts{
Mask: &it.rangeKeyMasking,
LowerBound: it.opts.LowerBound,
UpperBound: it.opts.UpperBound,
})
it.iter = &it.rangeKey.iiter
}
}
Expand Down
Loading

0 comments on commit 1253033

Please sign in to comment.