blob: c3627738be52a4a3c0f6857e2f6eb01e9570e9e3 [file] [log] [blame]
Michael Hanl72c7b832015-09-03 08:42:15 +02001package de.ids_mannheim.korap.handlers;
2
margaretha49cb6882018-07-04 04:19:54 +02003import org.apache.logging.log4j.LogManager;
4import org.apache.logging.log4j.Logger;
Michael Hanl72c7b832015-09-03 08:42:15 +02005import org.springframework.dao.DataAccessException;
6import org.springframework.jdbc.core.JdbcOperations;
7import org.springframework.jdbc.core.RowMapper;
8
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.List;
12
13/**
14 * @author hanl
15 * @date 24/03/2014
16 */
17public 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;
margaretha49cb6882018-07-04 04:19:54 +020024 private Logger log = LogManager.getLogger(BatchBuilder.class);
Michael Hanl72c7b832015-09-03 08:42:15 +020025
26 private JdbcOperations operations;
27
Michael Hanl8abaf9e2016-05-23 16:46:35 +020028 public BatchBuilder (JdbcOperations operations) {
Michael Hanl72c7b832015-09-03 08:42:15 +020029 this.operations = operations;
30 }
31
Michael Hanl8abaf9e2016-05-23 16:46:35 +020032 public <T> List<T> selectFromIDs (String query, Collection ids,
33 RowMapper<T> mapper) {
Michael Hanl72c7b832015-09-03 08:42:15 +020034 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 Hanl8abaf9e2016-05-23 16:46:35 +020064 }
65 catch (DataAccessException e) {
Michael Hanl72c7b832015-09-03 08:42:15 +020066 log.error("Exception during database retrieval", e);
67 }
68
69 }
70 return values;
71 }
72}