@@ -250,7 +250,7 @@ type tableCompaction struct {
250250 // b) rewrite blob files: The compaction will write eligible values to new
251251 // blob files. This consumes more write bandwidth because all values are
252252 // rewritten. However it restores locality.
253- getValueSeparation func (JobID , * tableCompaction ) compact.ValueSeparation
253+ getValueSeparation func (JobID , * tableCompaction , ValueStoragePolicy ) compact.ValueSeparation
254254
255255 // startLevel is the level that is being compacted. Inputs from startLevel
256256 // and outputLevel will be merged to produce a set of outputLevel files.
@@ -542,7 +542,7 @@ func (c *tableCompaction) makeInfo(jobID JobID) CompactionInfo {
542542 return info
543543}
544544
545- type getValueSeparation func (JobID , * tableCompaction ) compact.ValueSeparation
545+ type getValueSeparation func (JobID , * tableCompaction , ValueStoragePolicy ) compact.ValueSeparation
546546
547547// newCompaction constructs a compaction from the provided picked compaction.
548548//
@@ -3300,10 +3300,7 @@ func (d *DB) runDefaultTableCompaction(
33003300 d .mu .Unlock ()
33013301 defer d .mu .Lock ()
33023302
3303- // Determine whether we should separate values into blob files.
3304- valueSeparation := c .getValueSeparation (jobID , c )
3305-
3306- result := d .compactAndWrite (jobID , c , snapshots , valueSeparation )
3303+ result := d .compactAndWrite (jobID , c , snapshots )
33073304 if result .Err == nil {
33083305 ve , result .Err = c .makeVersionEdit (result )
33093306 }
@@ -3344,10 +3341,7 @@ func (d *DB) runDefaultTableCompaction(
33443341// compactAndWrite runs the data part of a compaction, where we set up a
33453342// compaction iterator and use it to write output tables.
33463343func (d * DB ) compactAndWrite (
3347- jobID JobID ,
3348- c * tableCompaction ,
3349- snapshots compact.Snapshots ,
3350- valueSeparation compact.ValueSeparation ,
3344+ jobID JobID , c * tableCompaction , snapshots compact.Snapshots ,
33513345) (result compact.Result ) {
33523346 suggestedCacheReaders := blob .SuggestedCachedReaders (len (c .inputs ))
33533347 // Compactions use a pool of buffers to read blocks, avoiding polluting the
@@ -3442,25 +3436,36 @@ func (d *DB) compactAndWrite(
34423436 }
34433437 runner := compact .NewRunner (runnerCfg , iter )
34443438
3445- var spanPolicyValid bool
3446- var spanPolicy SpanPolicy
3447- // If spanPolicyValid is true and spanPolicyEndKey is empty, then spanPolicy
3448- // applies for the rest of the keyspace.
3439+ // Determine the value separation policy for this compaction.
3440+ // We pass the value storage span policy if one applies for the entire
3441+ // compaction keyspace in order to preserve blob references.
3442+ // Note that the value storage policy may change per table if
3443+ // there are different span policies in effect for this output
3444+ // range.
3445+ var compactionValueStoragePolicy ValueStoragePolicy
34493446 var spanPolicyEndKey []byte
3447+ var spanPolicy SpanPolicy
3448+ spanPolicy , spanPolicyEndKey , err = d .opts .Experimental .SpanPolicyFunc (c .bounds .Start )
3449+ if err != nil {
3450+ return runner .Finish ().WithError (err )
3451+ }
3452+ if len (spanPolicyEndKey ) == 0 || d .cmp (c .bounds .End .Key , spanPolicyEndKey ) < 0 {
3453+ compactionValueStoragePolicy = spanPolicy .ValueStoragePolicy
3454+ }
34503455
3456+ valueSeparation := c .getValueSeparation (jobID , c , compactionValueStoragePolicy )
34513457 for runner .MoreDataToWrite () {
34523458 if c .cancel .Load () {
34533459 return runner .Finish ().WithError (ErrCancelledCompaction )
34543460 }
34553461 // Create a new table.
34563462 firstKey := runner .FirstKey ()
3457- if ! spanPolicyValid || ( len (spanPolicyEndKey ) > 0 && d .cmp (firstKey , spanPolicyEndKey ) >= 0 ) {
3463+ if len (spanPolicyEndKey ) > 0 && d .cmp (firstKey , spanPolicyEndKey ) >= 0 {
34583464 var err error
34593465 spanPolicy , spanPolicyEndKey , err = d .opts .Experimental .SpanPolicyFunc (firstKey )
34603466 if err != nil {
34613467 return runner .Finish ().WithError (err )
34623468 }
3463- spanPolicyValid = true
34643469 }
34653470
34663471 writerOpts := d .makeWriterOptions (c .outputLevel .level )
0 commit comments