| Nils Diewald | 50e90e9 | 2015-02-26 21:25:25 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.response.collector; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 2 | |
| Nils Diewald | 40996d8 | 2015-02-26 22:23:52 +0000 | [diff] [blame] | 3 | import de.ids_mannheim.korap.server.Node; |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 4 | import de.ids_mannheim.korap.response.Match; |
| Nils Diewald | 50e90e9 | 2015-02-26 21:25:25 +0000 | [diff] [blame] | 5 | import de.ids_mannheim.korap.response.MatchCollector; |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 6 | import com.fasterxml.jackson.annotation.*; |
| 7 | import java.sql.Connection; |
| 8 | import java.sql.PreparedStatement; |
| 9 | import javax.sql.DataSource; |
| 10 | import java.sql.SQLException; |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 11 | import java.util.*; |
| 12 | |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 13 | import org.slf4j.Logger; |
| 14 | import org.slf4j.LoggerFactory; |
| 15 | |
| Akron | 98b7854 | 2015-08-06 21:43:08 +0200 | [diff] [blame] | 16 | public final class MatchCollectorDB extends MatchCollector { |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 17 | |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 18 | // Logger |
| Nils Diewald | 40996d8 | 2015-02-26 22:23:52 +0000 | [diff] [blame] | 19 | private final static Logger log = LoggerFactory.getLogger(Node.class); |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 20 | |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 21 | /* |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 22 | * Todo: In case there are multiple threads searching, |
| 23 | * the list should be synchrinized Collections.synchronizedList() |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 24 | */ |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 25 | private String databaseType; |
| Akron | 98b7854 | 2015-08-06 21:43:08 +0200 | [diff] [blame] | 26 | private final List matchCollector; |
| 27 | private final int bufferSize; |
| 28 | private int docCollect; |
| 29 | private final String resultID; |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 30 | |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 31 | // private Connection connection; |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 32 | private DataSource pool; |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 33 | private Connection connection; |
| 34 | private PreparedStatement prepared; |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 35 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 36 | |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 37 | /* |
| 38 | * Create a new collector for database connections |
| 39 | */ |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 40 | public MatchCollectorDB (int bufferSize, String resultID) { |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 41 | this.bufferSize = bufferSize; |
| 42 | this.resultID = resultID; |
| 43 | this.matchCollector = new ArrayList<int[]>(bufferSize + 2); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 46 | |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 47 | /* |
| 48 | * Add matches till the bufferSize exceeds - then commit to the database. |
| 49 | */ |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 50 | public void add (int UID, int matchCount) { |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 51 | if (this.docCollect == bufferSize) |
| 52 | this.commit(); |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 53 | |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 54 | this.incrTotalResultDocs(1); |
| 55 | this.incrTotalResults(matchCount); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 56 | this.matchCollector.add(new int[] { UID, matchCount }); |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 57 | this.docCollect++; |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 60 | |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 61 | @JsonIgnore |
| 62 | public void setDatabaseType (String type) { |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 63 | this.databaseType = type; |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 66 | |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 67 | @JsonIgnore |
| 68 | public String getDatabaseType () { |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 69 | return this.databaseType; |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 72 | |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 73 | @JsonIgnore |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 74 | public void setDBPool (String type, DataSource ds, Connection conn) |
| 75 | throws SQLException { |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 76 | this.setDatabaseType(type); |
| 77 | this.connection = conn; |
| 78 | this.pool = ds; |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | |
| 82 | @JsonIgnore |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 83 | public void setDBPool (String type, DataSource ds) throws SQLException { |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 84 | this.setDatabaseType(type); |
| 85 | this.pool = ds; |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 86 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 87 | |
| 88 | |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 89 | /* |
| 90 | Create prepared statement for multiple requests |
| 91 | this.prepared = this.conn.prepareStatement( |
| 92 | "INSERT INTO people VALUES (?, ?);" |
| 93 | ); |
| 94 | Only prepare if commit > buffersize! |
| 95 | Difference between mariadb and sqlite! |
| 96 | */ |
| 97 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 98 | |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 99 | /* TODO: Ensure the commit was successful! */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 100 | public void commit () { |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 101 | if (this.pool == null) |
| 102 | return; |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 103 | |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 104 | try { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 105 | /* |
| 106 | * This should be heavily optimized! It's aweful! |
| 107 | * ARGHHHHHHH! |
| 108 | */ |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 109 | if (this.connection.isClosed()) |
| 110 | this.connection = this.pool.getConnection(); |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 111 | |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 112 | StringBuilder sb = new StringBuilder(); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 113 | sb.append("INSERT INTO ").append(this.resultID) |
| 114 | .append(" (text_id, match_count) "); |
| 115 | |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 116 | // SQLite batch insertion idiom |
| 117 | if (this.getDatabaseType().equals("sqlite")) { |
| 118 | for (int i = 1; i < this.docCollect; i++) { |
| 119 | sb.append("SELECT ?, ? UNION "); |
| 120 | } |
| 121 | if (this.docCollect == 1) |
| 122 | sb.append("VALUES (?, ?)"); |
| 123 | else |
| 124 | sb.append("SELECT ?, ?"); |
| 125 | } |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 126 | |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 127 | // MySQL batch insertion idiom |
| 128 | else if (this.getDatabaseType().equals("mysql")) { |
| 129 | sb.append(" VALUES "); |
| 130 | for (int i = 1; i < this.docCollect; i++) { |
| 131 | sb.append("(?,?),"); |
| 132 | }; |
| 133 | sb.append("(?,?)"); |
| 134 | } |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 135 | |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 136 | // Unknown idiom |
| 137 | else { |
| 138 | log.error("Unsupported Database type"); |
| 139 | return; |
| 140 | }; |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 141 | |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 142 | // Prepare statement based on the string |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 143 | PreparedStatement prep = this.connection.prepareStatement(sb |
| 144 | .toString()); |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 145 | |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 146 | int i = 1; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 147 | ListIterator li = this.matchCollector.listIterator(); |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 148 | while (li.hasNext()) { |
| 149 | int[] v = (int[]) li.next(); |
| 150 | prep.setInt(i++, v[0]); |
| 151 | prep.setInt(i++, v[1]); |
| 152 | }; |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 153 | |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 154 | prep.addBatch(); |
| 155 | prep.executeBatch(); |
| 156 | this.connection.commit(); |
| 157 | } |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 158 | |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 159 | // An SQL error occured ... |
| 160 | catch (SQLException e) { |
| 161 | log.error(e.getLocalizedMessage()); |
| 162 | }; |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 163 | |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 164 | this.matchCollector.clear(); |
| 165 | this.docCollect = 0; |
| 166 | return; |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 169 | |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 170 | /* |
| 171 | * Close collector and connection |
| 172 | */ |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 173 | public void close () { |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 174 | this.commit(); |
| 175 | try { |
| 176 | this.connection.close(); |
| 177 | } |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 178 | catch (SQLException e) { |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 179 | log.warn(e.getLocalizedMessage()); |
| 180 | } |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 183 | |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 184 | /* |
| 185 | * Close collector and probably connection |
| 186 | */ |
| 187 | public void close (boolean close) { |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 188 | if (close) |
| 189 | this.close(); |
| Akron | 98b7854 | 2015-08-06 21:43:08 +0200 | [diff] [blame] | 190 | |
| 191 | this.commit(); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 192 | }; |
| 193 | }; |