Skip to content

Commit

Permalink
internal/base: add MakeInternalKV
Browse files Browse the repository at this point in the history
Add a helper for constructing an base.InternalKV with an in-place value.
  • Loading branch information
jbowens committed Apr 12, 2024
1 parent d01e5e9 commit 26032dc
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 46 deletions.
5 changes: 1 addition & 4 deletions compaction_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,7 @@ func TestCompactionIter(t *testing.T) {
} else {
value = []byte(key[j+1:])
}
kvs = append(kvs, base.InternalKV{
K: ik,
V: base.MakeInPlaceValue(value),
})
kvs = append(kvs, base.MakeInternalKV(ik, value))
}
rangeDelFragmenter.Finish()
return ""
Expand Down
9 changes: 9 additions & 0 deletions internal/base/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,15 @@ func ParsePrettyInternalKey(s string) InternalKey {
return MakeInternalKey([]byte(ukey), seqNum, kind)
}

// MakeInternalKV constructs an InternalKV with the provided internal key and
// value. The value is encoded in-place.
func MakeInternalKV(k InternalKey, v []byte) InternalKV {
return InternalKV{
K: k,
V: MakeInPlaceValue(v),
}
}

// InternalKV represents a single internal key-value pair.
type InternalKV struct {
K InternalKey
Expand Down
5 changes: 1 addition & 4 deletions internal/itertest/dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ func NewParser() *dsl.Parser[Probe] {
})
probeParser.DefineFunc("ReturnKV",
func(p *dsl.Parser[Probe], s *dsl.Scanner) Probe {
kv := base.InternalKV{
K: base.ParseInternalKey(s.ConsumeString()),
V: base.MakeInPlaceValue([]byte(s.ConsumeString())),
}
kv := base.MakeInternalKV(base.ParseInternalKey(s.ConsumeString()), []byte(s.ConsumeString()))
s.Consume(token.RPAREN)
return ReturnKV(&kv)
})
Expand Down
4 changes: 1 addition & 3 deletions internal/keyspan/interleaving_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ func runInterleavingIterTest(t *testing.T, filename string) {
var points []base.InternalKV
lines := strings.Split(strings.TrimSpace(td.Input), "\n")
for _, line := range lines {
points = append(points, base.InternalKV{
K: base.ParseInternalKey(line),
})
points = append(points, base.MakeInternalKV(base.ParseInternalKey(line), nil))
}
pointIter = pointIterator{cmp: cmp, kvs: points}
hooks.maskSuffix = nil
Expand Down
10 changes: 2 additions & 8 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,7 @@ func TestIterator(t *testing.T) {
kvs = kvs[:0]
for _, key := range strings.Split(d.Input, "\n") {
j := strings.Index(key, ":")
kvs = append(kvs, base.InternalKV{
K: base.ParseInternalKey(key[:j]),
V: base.MakeInPlaceValue([]byte(key[j+1:])),
})
kvs = append(kvs, base.MakeInternalKV(base.ParseInternalKey(key[:j]), []byte(key[j+1:])))
}
return ""

Expand Down Expand Up @@ -1065,10 +1062,7 @@ func TestIteratorSeekOptErrors(t *testing.T) {
kvs = kvs[:0]
for _, key := range strings.Split(d.Input, "\n") {
j := strings.Index(key, ":")
kvs = append(kvs, base.InternalKV{
K: base.ParseInternalKey(key[:j]),
V: base.MakeInPlaceValue([]byte(key[j+1:])),
})
kvs = append(kvs, base.MakeInternalKV(base.ParseInternalKey(key[:j]), []byte(key[j+1:])))
}
return ""

Expand Down
20 changes: 9 additions & 11 deletions level_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,12 +699,10 @@ func (l *levelIter) SeekPrefixGE(prefix, key []byte, flags base.SeekGEFlags) *ba
// the table's bound with isIgnorableBoundaryKey set.
if l.rangeDelIterPtr != nil && *l.rangeDelIterPtr != nil {
if l.tableOpts.UpperBound != nil {
l.syntheticBoundary = base.InternalKV{
K: base.InternalKey{
UserKey: l.tableOpts.UpperBound,
Trailer: InternalKeyRangeDeleteSentinel,
},
}
l.syntheticBoundary = base.MakeInternalKV(base.InternalKey{
UserKey: l.tableOpts.UpperBound,
Trailer: InternalKeyRangeDeleteSentinel,
}, nil)
l.largestBoundary = &l.syntheticBoundary
if l.boundaryContext != nil {
l.boundaryContext.isSyntheticIterBoundsKey = true
Expand All @@ -716,7 +714,7 @@ func (l *levelIter) SeekPrefixGE(prefix, key []byte, flags base.SeekGEFlags) *ba
// the mergingIter advances beyond the file's bounds. We set
// isIgnorableBoundaryKey to signal that the actual key returned should
// be ignored, and does not represent a real key in the database.
l.syntheticBoundary = base.InternalKV{K: l.iterFile.LargestPointKey}
l.syntheticBoundary = base.MakeInternalKV(l.iterFile.LargestPointKey, nil)
l.largestBoundary = &l.syntheticBoundary
if l.boundaryContext != nil {
l.boundaryContext.isSyntheticIterBoundsKey = false
Expand Down Expand Up @@ -1001,7 +999,7 @@ func (l *levelIter) skipEmptyFileForward() *base.InternalKV {
// returned by the rangeDelIter after it's nil'd the ptr.
if l.iterFile.LargestPointKey.Kind() == InternalKeyKindRangeDelete ||
*l.rangeDelIterPtr != nil {
l.syntheticBoundary = base.InternalKV{K: l.iterFile.LargestPointKey}
l.syntheticBoundary = base.MakeInternalKV(l.iterFile.LargestPointKey, nil)
l.largestBoundary = &l.syntheticBoundary
if l.boundaryContext != nil {
l.boundaryContext.isIgnorableBoundaryKey = true
Expand Down Expand Up @@ -1052,10 +1050,10 @@ func (l *levelIter) skipEmptyFileBackward() *base.InternalKV {
// represent a real key.
if l.tableOpts.LowerBound != nil {
if *l.rangeDelIterPtr != nil {
l.syntheticBoundary.K = base.InternalKey{
l.syntheticBoundary = base.MakeInternalKV(base.InternalKey{
UserKey: l.tableOpts.LowerBound,
Trailer: InternalKeyRangeDeleteSentinel,
}
}, nil)
l.smallestBoundary = &l.syntheticBoundary
if l.boundaryContext != nil {
l.boundaryContext.isSyntheticIterBoundsKey = true
Expand All @@ -1078,7 +1076,7 @@ func (l *levelIter) skipEmptyFileBackward() *base.InternalKV {
// keep the level at the top of the heap and immediately skip the entry,
// advancing to the next file.
if *l.rangeDelIterPtr != nil {
l.syntheticBoundary = base.InternalKV{K: l.iterFile.SmallestPointKey}
l.syntheticBoundary = base.MakeInternalKV(l.iterFile.SmallestPointKey, nil)
l.smallestBoundary = &l.syntheticBoundary
if l.boundaryContext != nil {
l.boundaryContext.isIgnorableBoundaryKey = true
Expand Down
5 changes: 1 addition & 4 deletions level_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ func TestLevelIter(t *testing.T) {
f := &fakeIter{}
for _, key := range strings.Fields(line) {
j := strings.Index(key, ":")
f.kvs = append(f.kvs, base.InternalKV{
K: base.ParseInternalKey(key[:j]),
V: base.MakeInPlaceValue([]byte(key[j+1:])),
})
f.kvs = append(f.kvs, base.MakeInternalKV(base.ParseInternalKey(key[:j]), []byte(key[j+1:])))
}
iters = append(iters, f)

Expand Down
10 changes: 2 additions & 8 deletions merging_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ func TestMergingIterSeek(t *testing.T) {
f := &fakeIter{}
for _, key := range strings.Fields(line) {
j := strings.Index(key, ":")
f.kvs = append(f.kvs, base.InternalKV{
K: base.ParseInternalKey(key[:j]),
V: base.MakeInPlaceValue([]byte(key[j+1:])),
})
f.kvs = append(f.kvs, base.MakeInternalKV(base.ParseInternalKey(key[:j]), []byte(key[j+1:])))
}
iters = append(iters, f)
}
Expand Down Expand Up @@ -123,10 +120,7 @@ func TestMergingIterNextPrev(t *testing.T) {
iters[i] = f
for _, key := range strings.Fields(c[i]) {
j := strings.Index(key, ":")
f.kvs = append(f.kvs, base.InternalKV{
K: base.ParseInternalKey(key[:j]),
V: base.MakeInPlaceValue([]byte(key[j+1:])),
})
f.kvs = append(f.kvs, base.MakeInternalKV(base.ParseInternalKey(key[:j]), []byte(key[j+1:])))
}
}

Expand Down
5 changes: 1 addition & 4 deletions scan_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,7 @@ func TestPointCollapsingIter(t *testing.T) {
})
continue
}
f.kvs = append(f.kvs, base.InternalKV{
K: k,
V: base.MakeInPlaceValue(v),
})
f.kvs = append(f.kvs, base.MakeInternalKV(k, v))
}
}

Expand Down

0 comments on commit 26032dc

Please sign in to comment.