@@ -1589,24 +1589,26 @@ func WriteSyncNoop(eng Engine) error {
15891589// either write a Pebble range tombstone or clear individual keys. If it uses
15901590// a range tombstone, it will tighten the span to the first encountered key.
15911591//
1592- // pointKeyThreshold and rangeKeyThreshold specify the number of point/range
1593- // keys respectively where it will switch from clearing individual keys to
1594- // Pebble range tombstones (RANGEDEL or RANGEKEYDEL respectively). A threshold
1595- // of 0 disables checking for and clearing that key type.
1592+ // The pointKeyThreshold parameter specifies the number of point keys where it
1593+ // will switch from clearing individual keys using point tombstones to clearing
1594+ // the entire range using Pebble range tombstones (RANGEDELs). The
1595+ // pointKeyThreshold value must be at least 1. NB: An initial scan will be done
1596+ // to determine the type of clear, so a large threshold will potentially involve
1597+ // scanning a large number of keys.
15961598//
1597- // NB: An initial scan will be done to determine the type of clear, so a large
1598- // threshold will potentially involve scanning a large number of keys twice.
1599- //
1600- // TODO(erikgrinaker): Consider tightening the end of the range tombstone span
1601- // too, by doing a SeekLT when we reach the threshold. It's unclear whether it's
1602- // really worth it.
1599+ // ClearRangeWithHeuristic will also check for the existence of range keys, and
1600+ // if any exist, it will write a RANGEKEYDEL clearing all range keys in the span.
16031601func ClearRangeWithHeuristic (
16041602 ctx context.Context ,
16051603 r Reader ,
16061604 w Writer ,
16071605 start , end roachpb.Key ,
1608- pointKeyThreshold , rangeKeyThreshold int ,
1606+ pointKeyThreshold int ,
16091607) error {
1608+ if pointKeyThreshold < 1 {
1609+ return errors .AssertionFailedf ("pointKeyThreshold must be at least 1" )
1610+ }
1611+
16101612 clearPointKeys := func (r Reader , w Writer , start , end roachpb.Key , threshold int ) error {
16111613 iter , err := r .NewEngineIterator (ctx , IterOptions {
16121614 KeyTypes : IterKeyTypePointsOnly ,
@@ -1655,7 +1657,7 @@ func ClearRangeWithHeuristic(
16551657 return err
16561658 }
16571659
1658- clearRangeKeys := func (r Reader , w Writer , start , end roachpb.Key , threshold int ) error {
1660+ clearRangeKeys := func (r Reader , w Writer , start , end roachpb.Key ) error {
16591661 iter , err := r .NewEngineIterator (ctx , IterOptions {
16601662 KeyTypes : IterKeyTypeRangesOnly ,
16611663 LowerBound : start ,
@@ -1666,51 +1668,29 @@ func ClearRangeWithHeuristic(
16661668 }
16671669 defer iter .Close ()
16681670
1669- // Scan, and drop a RANGEKEYDEL if we reach the threshold.
1670- var ok bool
1671- var count int
1672- var firstKey roachpb.Key
1673- for ok , err = iter .SeekEngineKeyGE (EngineKey {Key : start }); ok ; ok , err = iter .NextEngineKey () {
1674- count += len (iter .EngineRangeKeys ())
1675- if len (firstKey ) == 0 {
1676- bounds , err := iter .EngineRangeBounds ()
1677- if err != nil {
1678- return err
1679- }
1680- firstKey = bounds .Key .Clone ()
1681- }
1682- if count >= threshold {
1683- return w .ClearRawRange (firstKey , end , false /* pointKeys */ , true /* rangeKeys */ )
1684- }
1685- }
1686- if err != nil || count == 0 {
1671+ ok , err := iter .SeekEngineKeyGE (EngineKey {Key : start })
1672+ if err != nil {
16871673 return err
16881674 }
1689- // Clear individual range keys.
1690- for ok , err = iter .SeekEngineKeyGE (EngineKey {Key : start }); ok ; ok , err = iter .NextEngineKey () {
1691- bounds , err := iter .EngineRangeBounds ()
1692- if err != nil {
1693- return err
1694- }
1695- for _ , v := range iter .EngineRangeKeys () {
1696- if err := w .ClearEngineRangeKey (bounds .Key , bounds .EndKey , v .Version ); err != nil {
1697- return err
1698- }
1699- }
1675+ if ! ok {
1676+ // No range keys in the span.
1677+ return nil
17001678 }
1701- return err
1702- }
1703-
1704- if pointKeyThreshold > 0 {
1705- if err := clearPointKeys (r , w , start , end , pointKeyThreshold ); err != nil {
1679+ bounds , err := iter .EngineRangeBounds ()
1680+ if err != nil {
17061681 return err
17071682 }
1683+ // TODO(erikgrinaker): Consider tightening the end of the range
1684+ // tombstone span too, by doing a SeekLT when we reach the threshold.
1685+ // It's unclear whether it's really worth it.
1686+ return w .ClearRawRange (bounds .Key , end , false /* pointKeys */ , true /* rangeKeys */ )
17081687 }
17091688
1710- if rangeKeyThreshold > 0 {
1711- if err := clearRangeKeys (r , w , start , end , rangeKeyThreshold ); err != nil {
1712- return err
1713- }
1689+ if err := clearPointKeys (r , w , start , end , pointKeyThreshold ); err != nil {
1690+ return err
1691+ }
1692+ if err := clearRangeKeys (r , w , start , end ); err != nil {
1693+ return err
17141694 }
17151695
17161696 return nil
0 commit comments