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