@@ -1589,24 +1589,22 @@ 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 (
1604- ctx context.Context ,
1605- r Reader ,
1606- w Writer ,
1607- start , end roachpb.Key ,
1608- pointKeyThreshold , rangeKeyThreshold int ,
1602+ ctx context.Context , r Reader , w Writer , start , end roachpb.Key , pointKeyThreshold int ,
16091603) error {
1604+ if pointKeyThreshold < 1 {
1605+ return errors .AssertionFailedf ("pointKeyThreshold must be at least 1" )
1606+ }
1607+
16101608 clearPointKeys := func (r Reader , w Writer , start , end roachpb.Key , threshold int ) error {
16111609 iter , err := r .NewEngineIterator (ctx , IterOptions {
16121610 KeyTypes : IterKeyTypePointsOnly ,
@@ -1655,7 +1653,7 @@ func ClearRangeWithHeuristic(
16551653 return err
16561654 }
16571655
1658- clearRangeKeys := func (r Reader , w Writer , start , end roachpb.Key , threshold int ) error {
1656+ clearRangeKeys := func (r Reader , w Writer , start , end roachpb.Key ) error {
16591657 iter , err := r .NewEngineIterator (ctx , IterOptions {
16601658 KeyTypes : IterKeyTypeRangesOnly ,
16611659 LowerBound : start ,
@@ -1666,51 +1664,29 @@ func ClearRangeWithHeuristic(
16661664 }
16671665 defer iter .Close ()
16681666
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 {
1667+ ok , err := iter .SeekEngineKeyGE (EngineKey {Key : start })
1668+ if err != nil {
16871669 return err
16881670 }
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- }
1671+ if ! ok {
1672+ // No range keys in the span.
1673+ return nil
17001674 }
1701- return err
1702- }
1703-
1704- if pointKeyThreshold > 0 {
1705- if err := clearPointKeys (r , w , start , end , pointKeyThreshold ); err != nil {
1675+ bounds , err := iter .EngineRangeBounds ()
1676+ if err != nil {
17061677 return err
17071678 }
1679+ // TODO(erikgrinaker): Consider tightening the end of the range
1680+ // tombstone span too, by doing a SeekLT when we reach the threshold.
1681+ // It's unclear whether it's really worth it.
1682+ return w .ClearRawRange (bounds .Key , end , false /* pointKeys */ , true /* rangeKeys */ )
17081683 }
17091684
1710- if rangeKeyThreshold > 0 {
1711- if err := clearRangeKeys (r , w , start , end , rangeKeyThreshold ); err != nil {
1712- return err
1713- }
1685+ if err := clearPointKeys (r , w , start , end , pointKeyThreshold ); err != nil {
1686+ return err
1687+ }
1688+ if err := clearRangeKeys (r , w , start , end ); err != nil {
1689+ return err
17141690 }
17151691
17161692 return nil
0 commit comments