| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.handlers; |
| 2 | |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 3 | import org.apache.logging.log4j.LogManager; |
| 4 | import org.apache.logging.log4j.Logger; |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 5 | import org.springframework.dao.DataAccessException; |
| 6 | import org.springframework.jdbc.core.JdbcOperations; |
| 7 | import org.springframework.jdbc.core.RowMapper; |
| 8 | |
| 9 | import java.util.ArrayList; |
| 10 | import java.util.Collection; |
| 11 | import java.util.List; |
| 12 | |
| 13 | /** |
| 14 | * @author hanl |
| 15 | * @date 24/03/2014 |
| 16 | */ |
| 17 | public class BatchBuilder { |
| 18 | |
| 19 | private static final int SINGLE_BATCH = 1; |
| 20 | private static final int SMALL_BATCH = 4; |
| 21 | private static final int SMALL_MEDIUM_BATCH = 6; |
| 22 | private static final int MEDIUM_BATCH = 8; |
| 23 | private static final int LARGE_BATCH = 12; |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 24 | private Logger log = LogManager.getLogger(BatchBuilder.class); |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 25 | |
| 26 | private JdbcOperations operations; |
| 27 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 28 | public BatchBuilder (JdbcOperations operations) { |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 29 | this.operations = operations; |
| 30 | } |
| 31 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 32 | public <T> List<T> selectFromIDs (String query, Collection ids, |
| 33 | RowMapper<T> mapper) { |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 34 | List l = new ArrayList(ids); |
| 35 | int size = ids.size(); |
| 36 | List<T> values = new ArrayList<>(); |
| 37 | while (size > 0) { |
| 38 | int batchSize = SINGLE_BATCH; |
| 39 | if (size >= LARGE_BATCH) |
| 40 | batchSize = LARGE_BATCH; |
| 41 | else if (size >= MEDIUM_BATCH) |
| 42 | batchSize = MEDIUM_BATCH; |
| 43 | else if (size >= SMALL_MEDIUM_BATCH) |
| 44 | batchSize = SMALL_MEDIUM_BATCH; |
| 45 | else if (size >= SMALL_BATCH) |
| 46 | batchSize = SMALL_BATCH; |
| 47 | size -= batchSize; |
| 48 | StringBuilder inClause = new StringBuilder(); |
| 49 | for (int i = 0; i < batchSize; i++) { |
| 50 | inClause.append('?'); |
| 51 | inClause.append(','); |
| 52 | } |
| 53 | inClause.deleteCharAt(inClause.length() - 1); |
| 54 | String sql = query + " (" + inClause.toString() + ");"; |
| 55 | Object[] args = new Object[batchSize]; |
| 56 | List d = new ArrayList(); |
| 57 | for (int idx = 0; idx < batchSize; idx++) { |
| 58 | args[idx] = l.get(idx); |
| 59 | d.add(idx, args[idx]); |
| 60 | } |
| 61 | l.removeAll(d); |
| 62 | try { |
| 63 | values.addAll(this.operations.query(sql, args, mapper)); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 64 | } |
| 65 | catch (DataAccessException e) { |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 66 | log.error("Exception during database retrieval", e); |
| 67 | } |
| 68 | |
| 69 | } |
| 70 | return values; |
| 71 | } |
| 72 | } |