| 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 | /* |
| 21 | Todo: In case there are multiple threads searching, |
| 22 | the list should be synchrinized Collections.synchronizedList() |
| 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 | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 29 | // private Connection connection; |
| 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 | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 67 | public void setDBPool (String type, DataSource ds) throws SQLException { |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 68 | this.setDatabaseType(type); |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 69 | this.pool = ds; |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 70 | |
| 71 | // Create prepared statement for multiple requests |
| 72 | |
| 73 | /* |
| 74 | this.prepared = this.conn.prepareStatement( |
| 75 | "INSERT INTO people VALUES (?, ?);" |
| 76 | ); |
| 77 | |
| 78 | Only prepare if commit > buffersize! |
| 79 | Difference between mariadb and sqlite! |
| 80 | */ |
| 81 | |
| 82 | }; |
| 83 | |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 84 | /* TODO: Ensure the commit was successful! */ |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 85 | public void commit () { |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 86 | if (this.pool == null) |
| 87 | return; |
| 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 | try { |
| 90 | // This should be heavily optimized! It's aweful! |
| 91 | /* |
| 92 | * ARGHHHHHHH! |
| 93 | */ |
| 94 | |
| 95 | if (this.connection == null) |
| 96 | this.connection = this.pool.getConnection(); |
| 97 | |
| 98 | // TODO: Create a BEGIN ... COMMIT Transaction |
| 99 | // connection.setAutoCommit(true); |
| 100 | |
| 101 | StringBuilder sb = new StringBuilder(); |
| 102 | sb.append("INSERT INTO "); |
| 103 | sb.append(this.resultID); |
| 104 | sb.append(" (text_id, match_count) "); |
| 105 | |
| 106 | // SQLite insertion idiom |
| 107 | if (this.getDatabaseType().equals("sqlite")) { |
| 108 | for (int i = 1; i < this.docCollect; i++) { |
| 109 | sb.append("SELECT ?, ? UNION "); |
| 110 | } |
| 111 | if (this.docCollect == 1) |
| 112 | sb.append("VALUES (?, ?)"); |
| 113 | else |
| 114 | sb.append("SELECT ?, ?"); |
| 115 | } |
| 116 | |
| 117 | // MySQL insertion idiom |
| 118 | else if (this.getDatabaseType().equals("mysql")) { |
| 119 | sb.append(" VALUES "); |
| 120 | for (int i = 1; i < this.docCollect; i++) { |
| 121 | sb.append("(?,?),"); |
| 122 | }; |
| 123 | sb.append("(?,?)"); |
| 124 | } |
| 125 | else { |
| 126 | log.error("Unsupported Database type"); |
| 127 | return; |
| 128 | }; |
| 129 | |
| 130 | // System.err.println(sb.toString()); |
| 131 | |
| 132 | PreparedStatement prep = connection.prepareStatement(sb.toString()); |
| 133 | |
| 134 | int i = 1; |
| 135 | ListIterator li = this.matchCollector.listIterator(); |
| 136 | while (li.hasNext()) { |
| 137 | int[] v = (int[]) li.next(); |
| 138 | // System.err.println("Has " + i + ":" + v[0]); |
| 139 | prep.setInt(i++, v[0]); |
| 140 | // System.err.println("Has " + i + ":" + v[1]); |
| 141 | prep.setInt(i++, v[1]); |
| 142 | // System.err.println("-"); |
| 143 | }; |
| 144 | |
| 145 | // System.err.println(sb.toString()); |
| 146 | |
| 147 | prep.addBatch(); |
| 148 | prep.executeBatch(); |
| 149 | // connection.setAutoCommit(false); |
| 150 | // connection.close(); |
| 151 | this.matchCollector.clear(); |
| 152 | this.docCollect = 0; |
| 153 | } |
| 154 | catch (SQLException e) { |
| 155 | this.matchCollector.clear(); |
| 156 | this.docCollect = 0; |
| 157 | System.err.println("Error: " + e.getLocalizedMessage()); |
| 158 | log.error(e.getLocalizedMessage()); |
| 159 | }; |
| 160 | return; |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | public void close () { |
| 164 | this.commit(); |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 165 | /* |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 166 | try { |
| 167 | this.connection.close(); |
| 168 | } |
| 169 | catch (SQLException e) { |
| 170 | }; |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 171 | */ |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 172 | }; |
| 173 | }; |