func appendPartsToMerge(dst, src []*partWrapper, maxPartsToMerge int, maxItems uint64) []*partWrapper {
	if len(src) < 2 {
		// There is no need in merging zero or one part :)
		return dst
	}
	if maxPartsToMerge < 2 {
		logger.Panicf("BUG: maxPartsToMerge cannot be smaller than 2; got %d", maxPartsToMerge)
	}

	// Filter out too big parts.
	// This should reduce N for O(n^2) algorithm below.
	maxInPartItems := maxItems / 2
	tmp := make([]*partWrapper, 0, len(src))
	for _, pw := range src {
		if pw.p.ph.itemsCount > maxInPartItems {
			continue
		}
		tmp = append(tmp, pw)
	}
	src = tmp

	// Sort src parts by itemsCount.
	sort.Slice(src, func(i, j int) bool { return src[i].p.ph.itemsCount < src[j].p.ph.itemsCount })

	n := maxPartsToMerge
	if len(src) < n {
		n = len(src)
	}

	// Exhaustive search for parts giving the lowest write amplification
	// when merged.
	var pws []*partWrapper
	maxM := float64(0)
	for i := 2; i <= n; i++ {
		for j := 0; j <= len(src)-i; j++ {
			itemsSum := uint64(0)
			for _, pw := range src[j : j+i] {
				itemsSum += pw.p.ph.itemsCount
			}
			if itemsSum > maxItems {
				continue
			}
			m := float64(itemsSum) / float64(src[j+i-1].p.ph.itemsCount)
			if m < maxM {
				continue
			}
			maxM = m
			pws = src[j : j+i]
		}
	}

	minM := float64(maxPartsToMerge / 2)
	if minM < 2 {
		minM = 2
	}
	if maxM < minM {
		// There is no sense in merging parts with too small m.
		return dst
	}

	return append(dst, pws...)
}
// appendPartsToMerge finds optimal parts to merge from src, appends
// them to dst and returns the result.
appendPartsToMerge finds optimal parts to merge from src, appends
them to dst and returns the result.
// appendPartsToMerge finds optimal parts to merge from src appends // them to dst and returns the result .
func appendPartsToMerge(dst, src []*partWrapper, maxPartsToMerge int, maxItems uint64) []*partWrapper {
	if len(src) < 2 {
		return dst
	}
	if maxPartsToMerge < 2 {
		logger.Panicf("BUG: maxPartsToMerge cannot be smaller than 2; got %d", maxPartsToMerge)
	}
	maxInPartItems := maxItems / 2
	tmp := make([]*partWrapper, 0, len(src))
	for _, pw := range src {
		if pw.p.ph.itemsCount > maxInPartItems {
			continue
		}
		tmp = append(tmp, pw)
	}
	src = tmp
	sort.Slice(src, func(i, j int) bool { return src[i].p.ph.itemsCount < src[j].p.ph.itemsCount })
	n := maxPartsToMerge
	if len(src) < n {
		n = len(src)
	}
	var pws []*partWrapper
	maxM := float64(0)
	for i := 2; i <= n; i++ {
		for j := 0; j <= len(src)-i; j++ {
			itemsSum := uint64(0)
			for _, pw := range src[j : j+i] {
				itemsSum += pw.p.ph.itemsCount
			}
			if itemsSum > maxItems {
				continue
			}
			m := float64(itemsSum) / float64(src[j+i-1].p.ph.itemsCount)
			if m < maxM {
				continue
			}
			maxM = m
			pws = src[j : j+i]
		}
	}
	minM := float64(maxPartsToMerge / 2)
	if minM < 2 {
		minM = 2
	}
	if maxM < minM {
		return dst
	}
	return append(dst, pws...)
}
func appendPartsToMerge ( dst , src [ ] * partWrapper , maxPartsToMerge int , maxItems uint64 ) [ ] * partWrapper { if len ( src ) < 2 { return dst } if maxPartsToMerge < 2 { logger . Panicf ( " " , maxPartsToMerge ) } maxInPartItems := maxItems / 2 tmp := make ( [ ] * partWrapper , 0 , len ( src ) ) for _ , pw := range src { if pw . p . ph . itemsCount > maxInPartItems { continue } tmp = append ( tmp , pw ) } src = tmp sort . Slice ( src , func ( i , j int ) bool { return src [ i ] . p . ph . itemsCount < src [ j ] . p . ph . itemsCount } ) n := maxPartsToMerge if len ( src ) < n { n = len ( src ) } var pws [ ] * partWrapper maxM := float64 ( 0 ) for i := 2 ; i <= n ; i ++ { for j := 0 ; j <= len ( src ) - i ; j ++ { itemsSum := uint64 ( 0 ) for _ , pw := range src [ j : j + i ] { itemsSum += pw . p . ph . itemsCount } if itemsSum > maxItems { continue } m := float64 ( itemsSum ) / float64 ( src [ j + i - 1 ] . p . ph . itemsCount ) if m < maxM { continue } maxM = m pws = src [ j : j + i ] } } minM := float64 ( maxPartsToMerge / 2 ) if minM < 2 { minM = 2 } if maxM < minM { return dst } return append ( dst , pws ... ) }
appendPartsToMerge finds optimal parts to merge from src, appends
them to dst and returns the result.
appendPartsToMerge finds optimal parts to merge from src appends them to dst and returns the result .
// There is no need in merging zero or one part :) // Filter out too big parts. // This should reduce N for O(n^2) algorithm below. // Sort src parts by itemsCount. // Exhaustive search for parts giving the lowest write amplification // when merged. // There is no sense in merging parts with too small m.