github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/segment/overlap.go (about)

     1  package segment
     2  
     3  import (
     4  	"math/big"
     5  	"time"
     6  )
     7  
     8  func tmin(a, b time.Time) time.Time {
     9  	if a.Before(b) {
    10  		return a
    11  	}
    12  	return b
    13  }
    14  
    15  func tmax(a, b time.Time) time.Time {
    16  	if a.After(b) {
    17  		return a
    18  	}
    19  	return b
    20  }
    21  
    22  func dmax(a, b time.Duration) time.Duration {
    23  	if a > b {
    24  		return a
    25  	}
    26  	return b
    27  }
    28  
    29  //  relationship                               overlap read             overlap write
    30  // 	inside  rel = iota   // | S E |            <1                       1/1
    31  // 	match                // matching ranges    1/1                      1/1
    32  // 	outside              // | | S E            0/1                      0/1
    33  // 	overlap              // | S | E            <1                       <1
    34  // 	contain              // S | | E            1/1                      <1
    35  
    36  // t1, t2 represent segment node, st, et represent the read query time range
    37  func overlapRead(t1, t2, st, et time.Time, dur time.Duration) *big.Rat {
    38  	m := int64(dmax(0, tmin(t2, et).Sub(tmax(t1, st))) / dur)
    39  	d := int64(t2.Sub(t1) / dur)
    40  	return big.NewRat(m, d)
    41  }
    42  
    43  // t1, t2 represent segment node, st, et represent the write query time range
    44  func overlapWrite(t1, t2, st, et time.Time, dur time.Duration) *big.Rat {
    45  	m := int64(dmax(0, tmin(t2, et).Sub(tmax(t1, st))) / dur)
    46  	d := int64(et.Sub(st) / dur)
    47  	return big.NewRat(m, d)
    48  }