001/* 002 * Copyright 2025 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.indexing.solr; 017 018import java.util.Collection; 019import java.util.concurrent.CancellationException; 020import java.util.concurrent.ExecutionException; 021import java.util.concurrent.Future; 022 023import org.slf4j.Logger; 024 025/** 026 * Record to return indexation results: success and error count. 027 * @param successCount number of successful tasks 028 * @param errorCount number of fail tasks 029 */ 030public record IndexationResult(int successCount, int errorCount) 031{ 032 /** 033 * Test if the indexation had errors. 034 * @return <code>true</code> if the indexation had errors, <code>false</code> otherwise. 035 */ 036 public boolean hasErrors() 037 { 038 return errorCount() > 0; 039 } 040 041 /** 042 * Check each future of launched tasks and count success and errors. 043 * @param tasks The tasks launched in the executor service 044 * @param logger The logger 045 * @return The indexation result 046 */ 047 public static IndexationResult fromTasks(Collection<Future<Void>> tasks, Logger logger) 048 { 049 int successCount = 0; 050 int errorCount = 0; 051 052 // Now that everything is submitted, we can iterate and wait for result 053 for (Future<Void> task : tasks) 054 { 055 try 056 { 057 task.get(); 058 successCount++; 059 } 060 catch (CancellationException | InterruptedException | ExecutionException e) 061 { 062 logger.error("Error during parallel indexation", e); 063 errorCount++; 064 } 065 } 066 067 return new IndexationResult(successCount, errorCount); 068 } 069}