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