Gather data
This commit is contained in:
92
src/main/java/puzzle/HintScores.java
Normal file
92
src/main/java/puzzle/HintScores.java
Normal file
@@ -0,0 +1,92 @@
|
||||
package puzzle;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.function.ToIntFunction;
|
||||
|
||||
public final class HintScores {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
try (Connection conn = DriverManager.getConnection("jdbc:sqlite:/home/mike/dev/puzzle-generator/tools/hint/hint.sqlite")) {
|
||||
updateCrossScores(conn, HintScores::exampleScore, 1000);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Updates hints.cross_score by computing a score from hints.word.
|
||||
*
|
||||
* @param conn open JDBC connection (PostgreSQL)
|
||||
* @param scoreFn callback: scoreFn.applyAsInt(word)
|
||||
* @param batchSize e.g. 1000
|
||||
*/
|
||||
public static void updateCrossScores(
|
||||
Connection conn,
|
||||
ToIntFunction<String> scoreFn,
|
||||
int batchSize
|
||||
) throws SQLException {
|
||||
|
||||
// Use a transaction for speed + consistency
|
||||
final boolean prevAutoCommit = conn.getAutoCommit();
|
||||
conn.setAutoCommit(false);
|
||||
|
||||
// Server-side cursor behavior in pgjdbc requires autoCommit=false + fetchSize>0
|
||||
final String selectSql =
|
||||
"SELECT id, puzzle_norm " +
|
||||
"FROM hints " +
|
||||
"WHERE puzzle_norm IS NOT NULL"; // optionally add: " AND cross_score IS NULL"
|
||||
|
||||
final String updateSql =
|
||||
"UPDATE hints SET cross_score = ? WHERE id = ?";
|
||||
|
||||
try (PreparedStatement psSel = conn.prepareStatement(selectSql);
|
||||
PreparedStatement psUpd = conn.prepareStatement(updateSql)) {
|
||||
|
||||
psSel.setFetchSize(batchSize);
|
||||
|
||||
int pending = 0;
|
||||
|
||||
try (ResultSet rs = psSel.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
long id = rs.getLong("id");
|
||||
String word = rs.getString("puzzle_norm");
|
||||
|
||||
int score;
|
||||
try {
|
||||
score = scoreFn.applyAsInt(word);
|
||||
} catch (RuntimeException ex) {
|
||||
// If scoring fails, decide your policy: skip or set 0.
|
||||
// Here: skip row.
|
||||
continue;
|
||||
}
|
||||
|
||||
psUpd.setInt(1, score);
|
||||
psUpd.setLong(2, id);
|
||||
psUpd.addBatch();
|
||||
pending++;
|
||||
|
||||
if (pending >= batchSize) {
|
||||
psUpd.executeBatch();
|
||||
conn.commit();
|
||||
pending = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pending > 0) {
|
||||
psUpd.executeBatch();
|
||||
conn.commit();
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
conn.rollback();
|
||||
throw e;
|
||||
} finally {
|
||||
conn.setAutoCommit(prevAutoCommit);
|
||||
}
|
||||
}
|
||||
|
||||
// Example scoring callback
|
||||
public static int exampleScore(String word) {
|
||||
return ThemePoolBuilderLength.crossabilityScore(word);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user