Repository: influxdata/influxdb
File: tsdb/engine/tsm1/compact_property_test.go (Go)

Code before:

	// Validate each property
	for _, property := range properties {
		if err := property.Validator(allFiles, allGroups); err != nil {
			errs = append(errs, fmt.Errorf("%s violation: %v", property.Name, err))
		}
	}

	return errors.Join(errs...)
}

// extractGroups extracts CompactionGroup from PlannedCompactionGroup
func extractGroups(plannedGroups []tsm1.PlannedCompactionGroup) []tsm1.CompactionGroup {
	var groups []tsm1.CompactionGroup
	for _, planned := range plannedGroups {
		groups = append(groups, planned.Group)
	}
	return groups
}

// ValidateTestCase validates both expected results and actual planner output
func ValidateTestCase(testCase TestEnginePlanCompactionsRunner, actualResults TestLevelResults) error {
	var errs []error

	// Extract all filenames from test case
	var allFiles []string
	for _, file := range testCase.files {
		allFiles = append(allFiles, file.Path)
	}

	// Validate expected results
	expectedResults := testCase.expectedResult()
	if expectedErr := ValidateCompactionProperties(allFiles, expectedResults, AdjacentFileProperty); expectedErr != nil {
		errs = append(errs, fmt.Errorf("expected results: %v", expectedErr))
	}

	// Validate actual results
	if actualErr := ValidateCompactionProperties(allFiles, actualResults, AdjacentFileProperty); actualErr != nil {
		errs = append(errs, fmt.Errorf("actual results: %v", actualErr))
	}

	return errors.Join(errs...)
}


Review comment (performance):
since you're going to touch this for the comment change, please preallocate this since you know how big it will be. ci pays for allocations too.

Code after:
			if !ok {
				return fmt.Errorf("file %s not found in group %d", file, groupIndex)
			}

			if lastIndex == -1 {
				lastIndex = f.index
			} else {
				// Check lastIndex wrt f.index
				if lastIndex+1 != f.index {
					return fmt.Errorf("file %s in compaction group %d violates adjacency policy", file, groupIndex+1)
				}
				lastIndex = f.index
			}
		}
	}

	return nil
}

// ValidateCompactionProperties validates that compaction results satisfy all properties
func ValidateCompactionProperties(allFiles []string, results TestLevelResults, properties ...CompactionProperty) error {
	var errs []error

	// Collect all compaction groups from results
	var allGroups []tsm1.CompactionGroup
	allGroups = append(allGroups, extractGroups(results.level1Groups)...)
	allGroups = append(allGroups, extractGroups(results.level2Groups)...)
	allGroups = append(allGroups, extractGroups(results.level3Groups)...)
	allGroups = append(allGroups, extractGroups(results.level4Groups)...)
	allGroups = append(allGroups, extractGroups(results.level5Groups)...)

	// Validate each property
	for _, property := range properties {
		if err := property.Validator(allFiles, allGroups); err != nil {
			errs = append(errs, fmt.Errorf("%s violation: %v", property.Name, err))
		}
	}

	return errors.Join(errs...)
}

// extractGroups extracts CompactionGroup from PlannedCompactionGroup
func extractGroups(plannedGroups []tsm1.PlannedCompactionGroup) []tsm1.CompactionGroup {
	var groups []tsm1.CompactionGroup
	for _, planned := range plannedGroups {
		groups = append(groups, planned.Group)
	}
	return groups
}

// ValidateTestCase validates both expected results and actual planner output