Skip to content

Commit f07254f

Browse files
committed
db: extract determineLeftTableBounds from exciseTable
1 parent 1a34107 commit f07254f

File tree

1 file changed

+54
-45
lines changed

1 file changed

+54
-45
lines changed

excise.go

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -123,53 +123,13 @@ func (d *DB) exciseTable(
123123
LargestSeqNumAbsolute: m.LargestSeqNumAbsolute,
124124
SyntheticPrefixAndSuffix: m.SyntheticPrefixAndSuffix,
125125
}
126-
if m.HasPointKeys && !exciseSpan.ContainsInternalKey(d.cmp, m.SmallestPointKey) {
127-
// This file will probably contain point keys.
128-
if err := loadItersIfNecessary(); err != nil {
129-
return nil, err
130-
}
131-
smallestPointKey := m.SmallestPointKey
132-
if kv := iters.Point().SeekLT(exciseSpan.Start, base.SeekLTFlagsNone); kv != nil {
133-
leftFile.ExtendPointKeyBounds(d.cmp, smallestPointKey, kv.K.Clone())
134-
}
135-
// Store the min of (exciseSpan.Start, rdel.End) in lastRangeDel. This
136-
// needs to be a copy if the key is owned by the range del iter.
137-
var lastRangeDel []byte
138-
if rdel, err := iters.RangeDeletion().SeekLT(exciseSpan.Start); err != nil {
139-
return nil, err
140-
} else if rdel != nil {
141-
lastRangeDel = append(lastRangeDel[:0], rdel.End...)
142-
if d.cmp(lastRangeDel, exciseSpan.Start) > 0 {
143-
lastRangeDel = exciseSpan.Start
144-
}
145-
}
146-
if lastRangeDel != nil {
147-
leftFile.ExtendPointKeyBounds(d.cmp, smallestPointKey, base.MakeExclusiveSentinelKey(InternalKeyKindRangeDelete, lastRangeDel))
148-
}
126+
if err := loadItersIfNecessary(); err != nil {
127+
return nil, err
149128
}
150-
if m.HasRangeKeys && !exciseSpan.ContainsInternalKey(d.cmp, m.SmallestRangeKey) {
151-
// This file will probably contain range keys.
152-
if err := loadItersIfNecessary(); err != nil {
153-
return nil, err
154-
}
155-
smallestRangeKey := m.SmallestRangeKey
156-
// Store the min of (exciseSpan.Start, rkey.End) in lastRangeKey. This
157-
// needs to be a copy if the key is owned by the range key iter.
158-
var lastRangeKey []byte
159-
var lastRangeKeyKind InternalKeyKind
160-
if rkey, err := iters.RangeKey().SeekLT(exciseSpan.Start); err != nil {
161-
return nil, err
162-
} else if rkey != nil {
163-
lastRangeKey = append(lastRangeKey[:0], rkey.End...)
164-
if d.cmp(lastRangeKey, exciseSpan.Start) > 0 {
165-
lastRangeKey = exciseSpan.Start
166-
}
167-
lastRangeKeyKind = rkey.Keys[0].Kind()
168-
}
169-
if lastRangeKey != nil {
170-
leftFile.ExtendRangeKeyBounds(d.cmp, smallestRangeKey, base.MakeExclusiveSentinelKey(lastRangeKeyKind, lastRangeKey))
171-
}
129+
if err := determineLeftTableBounds(d.cmp, m, leftFile, exciseSpan.Start, iters); err != nil {
130+
return nil, err
172131
}
132+
173133
if leftFile.HasRangeKeys || leftFile.HasPointKeys {
174134
var err error
175135
leftFile.Size, err = d.fileCache.estimateSize(m, leftFile.Smallest.UserKey, leftFile.Largest.UserKey)
@@ -360,3 +320,52 @@ func exciseOverlapBounds(
360320
}
361321
return extended
362322
}
323+
324+
// determineLeftTableBounds calculates the bounds for the table that remains to
325+
// the left of the excise span after excising originalFile.
326+
//
327+
// Sets the smallest and largest keys, as well as HasPointKeys/HasRangeKeys in
328+
// the leftFile.
329+
func determineLeftTableBounds(
330+
cmp Compare, originalTable, leftTable *tableMetadata, exciseSpanStart []byte, iters iterSet,
331+
) error {
332+
if originalTable.HasPointKeys && cmp(originalTable.SmallestPointKey.UserKey, exciseSpanStart) < 0 {
333+
// This file will probably contain point keys.
334+
smallestPointKey := originalTable.SmallestPointKey
335+
if kv := iters.Point().SeekLT(exciseSpanStart, base.SeekLTFlagsNone); kv != nil {
336+
leftTable.ExtendPointKeyBounds(cmp, smallestPointKey, kv.K.Clone())
337+
}
338+
rdel, err := iters.RangeDeletion().SeekLT(exciseSpanStart)
339+
if err != nil {
340+
return err
341+
}
342+
if rdel != nil {
343+
// Use the smaller of exciseSpanStart and rdel.End.
344+
lastRangeDel := exciseSpanStart
345+
if cmp(rdel.End, exciseSpanStart) < 0 {
346+
// The key is owned by the range del iter, so we need to copy it.
347+
lastRangeDel = slices.Clone(rdel.End)
348+
}
349+
leftTable.ExtendPointKeyBounds(cmp, smallestPointKey, base.MakeExclusiveSentinelKey(InternalKeyKindRangeDelete, lastRangeDel))
350+
}
351+
}
352+
353+
if originalTable.HasRangeKeys && cmp(originalTable.SmallestRangeKey.UserKey, exciseSpanStart) < 0 {
354+
smallestRangeKey := originalTable.SmallestRangeKey
355+
rkey, err := iters.RangeKey().SeekLT(exciseSpanStart)
356+
if err != nil {
357+
return err
358+
}
359+
if rkey != nil {
360+
// Use the smaller of exciseSpanStart and rkey.End.
361+
lastRangeKey := exciseSpanStart
362+
if cmp(rkey.End, exciseSpanStart) < 0 {
363+
// The key is owned by the range key iter, so we need to copy it.
364+
lastRangeKey = slices.Clone(rkey.End)
365+
}
366+
lastRangeKeyKind := rkey.Keys[0].Kind()
367+
leftTable.ExtendRangeKeyBounds(cmp, smallestRangeKey, base.MakeExclusiveSentinelKey(lastRangeKeyKind, lastRangeKey))
368+
}
369+
}
370+
return nil
371+
}

0 commit comments

Comments
 (0)