use of org.mycore.util.concurrent.MCRTransactionableCallable in project mycore by MyCoRe-Org.
the class MCRMetadataHistoryCommands method buildHistory.
@MCRCommand(syntax = "build metadata history of base {0}", help = "build metadata history of all objects with base id {0}")
public static List<String> buildHistory(String baseId) {
MCRMetadataStore store = MCRXMLMetadataManager.instance().getStore(baseId);
if (store instanceof MCRVersioningMetadataStore) {
LogManager.getLogger().info("Verify SVN history of {}", baseId);
((MCRVersioningMetadataStore) store).verify();
}
ExecutorService executorService = Executors.newWorkStealingPool();
MCRSession currentSession = MCRSessionMgr.getCurrentSession();
int maxId = store.getHighestStoredID();
AtomicInteger completed = new AtomicInteger(maxId);
IntStream.rangeClosed(1, maxId).parallel().mapToObj(i -> MCRObjectID.formatID(baseId, i)).map(MCRObjectID::getInstance).map(id -> new MCRTransactionableCallable<>(Executors.callable(() -> {
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
getHistoryItems(id).sequential().forEach(em::persist);
completed.decrementAndGet();
}), currentSession)).forEach(executorService::submit);
executorService.shutdown();
boolean waitToFinish = true;
while (!executorService.isTerminated() && waitToFinish) {
LogManager.getLogger().info("Waiting for history of {} objects/derivates.", completed.get());
try {
executorService.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
waitToFinish = false;
}
}
return Collections.emptyList();
}
Aggregations