/** * Ensure values {@code inputs} are unique (which avoids useless arguments) and sorted before creating the partition. */ public static <INPUT extends Comparable<INPUT>> Iterable<List<INPUT>> toUniqueAndSortedPartitions(Collection<INPUT> inputs) { return toUniqueAndSortedPartitions(inputs, i -> i); }
public IssueIteratorForMultipleChunks(DbClient dbClient, Collection<String> issueKeys) { this.dbClient = dbClient; iteratorOverChunks = DatabaseUtils.toUniqueAndSortedPartitions(issueKeys).iterator(); }
/** * Partition by 1000 elements a list of input and execute a consumer on each part. * * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' * and with MsSQL when there's more than 2000 parameters in a query * * @param inputs the whole list of elements to be partitioned * @param consumer the mapper method to be executed, for example {@code mapper(dbSession)::selectByUuids} * @param partitionSizeManipulations the function that computes the number of usages of a partition, for example * {@code partitionSize -> partitionSize / 2} when the partition of elements * in used twice in the SQL request. */ public static <INPUT extends Comparable<INPUT>> void executeLargeUpdates(Collection<INPUT> inputs, Consumer<List<INPUT>> consumer, IntFunction<Integer> partitionSizeManipulations) { Iterable<List<INPUT>> partitions = toUniqueAndSortedPartitions(inputs, partitionSizeManipulations); for (List<INPUT> partition : partitions) { consumer.accept(partition); } }
private static <OUTPUT, INPUT extends Comparable<INPUT>, RESULT extends Collection<OUTPUT>> RESULT executeLargeInputs(Collection<INPUT> input, Function<List<INPUT>, RESULT> function, java.util.function.IntFunction<RESULT> outputInitializer, IntFunction<Integer> partitionSizeManipulations) { if (input.isEmpty()) { return outputInitializer.apply(0); } RESULT results = outputInitializer.apply(input.size()); for (List<INPUT> partition : toUniqueAndSortedPartitions(input, partitionSizeManipulations)) { RESULT subResults = function.apply(partition); if (subResults != null) { results.addAll(subResults); } } return results; }
/** * Scroll line hashes of all <strong>enabled</strong> components (should be files, but not enforced) with specified * uuids in no specific order with 'SOURCE' source and a non null path. */ public void scrollLineHashes(DbSession dbSession, Collection<String> fileUUids, ResultHandler<LineHashesWithUuidDto> rowHandler) { for (List<String> partition : toUniqueAndSortedPartitions(fileUUids)) { mapper(dbSession).scrollLineHashes(partition, rowHandler); } }
/** * Ensure values {@code inputs} are unique (which avoids useless arguments) and sorted before creating the partition. */ public static <INPUT extends Comparable<INPUT>> Iterable<List<INPUT>> toUniqueAndSortedPartitions(Collection<INPUT> inputs) { return toUniqueAndSortedPartitions(inputs, i -> i); }
public IssueIteratorForMultipleChunks(DbClient dbClient, Collection<String> issueKeys) { this.dbClient = dbClient; iteratorOverChunks = DatabaseUtils.toUniqueAndSortedPartitions(issueKeys).iterator(); }
/** * Partition by 1000 elements a list of input and execute a consumer on each part. * * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' * and with MsSQL when there's more than 2000 parameters in a query */ public static <INPUT extends Comparable<INPUT>> void executeLargeUpdates(Collection<INPUT> inputs, Consumer<List<INPUT>> consumer) { Iterable<List<INPUT>> partitions = toUniqueAndSortedPartitions(inputs); for (List<INPUT> partition : partitions) { consumer.accept(partition); } }
/** * Partition by 1000 elements a list of input and execute a consumer on each part. * * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' * and with MsSQL when there's more than 2000 parameters in a query * * @param sqlCaller a {@link Function} which calls the SQL update/delete and returns the number of updated/deleted rows. * * @return the total number of updated/deleted rows (computed as the sum of the values returned by {@code sqlCaller}). */ public static <INPUT extends Comparable<INPUT>> int executeLargeUpdates(Collection<INPUT> inputs, Function<List<INPUT>, Integer> sqlCaller) { Iterable<List<INPUT>> partitions = toUniqueAndSortedPartitions(inputs); Integer res = 0; for (List<INPUT> partition : partitions) { res += sqlCaller.apply(partition); } return res; }
/** * Partition by 1000 elements a list of input and execute a consumer on each part. * * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' * and with MsSQL when there's more than 2000 parameters in a query * * @param inputs the whole list of elements to be partitioned * @param consumer the mapper method to be executed, for example {@code mapper(dbSession)::selectByUuids} * @param partitionSizeManipulations the function that computes the number of usages of a partition, for example * {@code partitionSize -> partitionSize / 2} when the partition of elements * in used twice in the SQL request. */ public static <INPUT extends Comparable<INPUT>> void executeLargeUpdates(Collection<INPUT> inputs, Consumer<List<INPUT>> consumer, IntFunction<Integer> partitionSizeManipulations) { Iterable<List<INPUT>> partitions = toUniqueAndSortedPartitions(inputs, partitionSizeManipulations); for (List<INPUT> partition : partitions) { consumer.accept(partition); } }
private static <OUTPUT, INPUT extends Comparable<INPUT>, RESULT extends Collection<OUTPUT>> RESULT executeLargeInputs(Collection<INPUT> input, Function<List<INPUT>, RESULT> function, java.util.function.Function<Integer, RESULT> outputInitializer) { if (input.isEmpty()) { return outputInitializer.apply(0); } RESULT results = outputInitializer.apply(input.size()); for (List<INPUT> partition : toUniqueAndSortedPartitions(input)) { RESULT subResults = function.apply(partition); if (subResults != null) { results.addAll(subResults); } } return results; }
private static <OUTPUT, INPUT extends Comparable<INPUT>, RESULT extends Collection<OUTPUT>> RESULT executeLargeInputs(Collection<INPUT> input, Function<List<INPUT>, RESULT> function, java.util.function.IntFunction<RESULT> outputInitializer, IntFunction<Integer> partitionSizeManipulations) { if (input.isEmpty()) { return outputInitializer.apply(0); } RESULT results = outputInitializer.apply(input.size()); for (List<INPUT> partition : toUniqueAndSortedPartitions(input, partitionSizeManipulations)) { RESULT subResults = function.apply(partition); if (subResults != null) { results.addAll(subResults); } } return results; }