(candidates []recoveryCandidate, totalCharsBudget int)
| 283 | } |
| 284 | |
| 285 | func selectRecoveryRecords(candidates []recoveryCandidate, totalCharsBudget int) []recoveryCandidate { |
| 286 | if len(candidates) == 0 { |
| 287 | return nil |
| 288 | } |
| 289 | fixedOverhead := runeLen(recoveryHeader) + runeLen(recoveryFooter) |
| 290 | if fixedOverhead >= totalCharsBudget { |
| 291 | return nil |
| 292 | } |
| 293 | latestTurn := 0 |
| 294 | for _, candidate := range candidates { |
| 295 | if candidate.record.LastTurnIndex > latestTurn { |
| 296 | latestTurn = candidate.record.LastTurnIndex |
| 297 | } |
| 298 | } |
| 299 | sort.SliceStable(candidates, func(i, j int) bool { |
| 300 | return recoverySelectionLess(candidates[j], candidates[i], latestTurn) |
| 301 | }) |
| 302 | var selected []recoveryCandidate |
| 303 | totalChars := fixedOverhead |
| 304 | for _, candidate := range candidates { |
| 305 | separatorChars := 0 |
| 306 | if len(selected) > 0 { |
| 307 | separatorChars = runeLen(recoverySectionSeparator) |
| 308 | } |
| 309 | nextTotal := totalChars + separatorChars + runeLen(candidate.section) |
| 310 | if nextTotal > totalCharsBudget { |
| 311 | continue |
| 312 | } |
| 313 | selected = append(selected, candidate) |
| 314 | totalChars = nextTotal |
| 315 | } |
| 316 | return selected |
| 317 | } |
| 318 | |
| 319 | func recoverySelectionLess(a, b recoveryCandidate, latestTurn int) bool { |
| 320 | ad := recoveryDensity(a, latestTurn) |
no test coverage detected