Skip to content

Commit

Permalink
- add helper tool to optimize history db. Especially older converted …
Browse files Browse the repository at this point in the history
…histories may contain duplicate data which might sloe down processing.
  • Loading branch information
derreisende77 committed Aug 11, 2024
1 parent fecd883 commit 403e055
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- **FEATURE:** Dialog "Filminformation" kann nun vollständig vergrößert oder verkleinert werden.
- **FEATURE:** Die Filter werden nun über einen eigenen Button umbenannt und nicht mehr durch Eingabe in der ComboBox.
- **FEATURE:** Bei der Lucene-Suche dürfen Wildcards nun auch am Anfang des Suchtexts verwendet werden.
- **FEATURE:** Die Datenbank der gesehenen Filme kann nun über das Menü *"Hilfe/Hilfsmittel/History-Datenbank optimieren..."* von eventuell vorhandenen Duplikaten befreit werden. Dies kann die Performance des Programms bei einer großen (und älteren) Datenbank positiv beeinflussen.
- Network Timeout wurde auf 10 Sekunden erhöht. Dies sollte bei schlechten Netzwerkverbindungen etwas Abhilfe schaffen.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import java.nio.file.Files
import java.nio.file.Path
import java.sql.*
import kotlin.system.exitProcess
import kotlin.use

/**
* Database based seen history controller.
Expand Down Expand Up @@ -126,8 +127,7 @@ class SeenHistoryController : AutoCloseable {
it.executeUpdate("REINDEX seen_history")
it.executeUpdate("VACUUM")
}
}
catch (e: SQLException) {
} catch (e: SQLException) {
logger.error("Failed to execute maintenance script", e)
}
logger.trace("Finished maintenance")
Expand Down Expand Up @@ -271,6 +271,49 @@ class SeenHistoryController : AutoCloseable {
Runtime.getRuntime().addShutdownHook(shutdownThread)
}

@JvmRecord
data class DuplicateSearchResult(val total: Long, val distinct: Long)

@Throws(SQLException::class)
fun removeDuplicates() {
val prevAutoCommitState = connection!!.autoCommit
connection!!.autoCommit = false
try {
connection!!.createStatement().use { statement ->
statement.executeUpdate("CREATE TABLE temp_history AS SELECT DISTINCT datum,thema,titel,url FROM seen_history ORDER BY datum")
statement.executeUpdate("DELETE FROM seen_history")
statement.executeUpdate("INSERT INTO seen_history(datum,thema,titel,url) SELECT datum,thema,titel,url FROM temp_history")
statement.executeUpdate("DROP TABLE temp_history")
}
connection!!.commit()
}
catch (e: SQLException) {
connection!!.rollback()
throw e
}
connection!!.autoCommit = prevAutoCommitState
}

@Throws(SQLException::class)
fun checkDuplicates(): DuplicateSearchResult {
var total: Long = 0
var distinct: Long = 0

connection!!.createStatement().use { statement ->
statement!!.executeQuery("SELECT COUNT(*) FROM seen_history").use {
it.next()
total = it.getLong(1)
}
}
connection!!.createStatement().use { statement ->
statement.executeQuery("SELECT COUNT(*) FROM (SELECT DISTINCT datum,thema,titel,url FROM seen_history)").use {
it.next()
distinct = it.getLong(1)
}
}
return DuplicateSearchResult(total, distinct)
}

init {
try {
if (!Files.exists(SqlDatabaseConfig.historyDbPath)) {
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/mediathek/gui/actions/OptimizeHistoryDbAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package mediathek.gui.actions;

import mediathek.config.Konstanten;
import mediathek.controller.history.SeenHistoryController;
import mediathek.mainwindow.MediathekGui;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

public class OptimizeHistoryDbAction extends AbstractAction {
private final MediathekGui mediathekGui;
private static final Logger logger = LogManager.getLogger();

public OptimizeHistoryDbAction(MediathekGui mediathekGui) {
this.mediathekGui = mediathekGui;
putValue(NAME, "History-Datenbank optimieren...");
}

@Override
public void actionPerformed(ActionEvent e) {
try (SeenHistoryController controller = new SeenHistoryController()) {
var dupes = controller.checkDuplicates();
var numDuplicates = dupes.total() - dupes.distinct();
logger.trace("{} duplicates found in history", numDuplicates);
if (numDuplicates == 0) {
//no duplicates
var msg = "Es sind keine Duplikate vorhanden.\nEine Optimierung ist nicht notwendig.";
JOptionPane.showMessageDialog(mediathekGui, msg, Konstanten.PROGRAMMNAME, JOptionPane.INFORMATION_MESSAGE);
}
else {
var msg = String.format("Es wurden %d Duplikate gefunden.\nMöchten Sie diese bereinigen?", numDuplicates);
var answer = JOptionPane.showOptionDialog(mediathekGui, msg, Konstanten.PROGRAMMNAME,
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
if (answer == JOptionPane.YES_OPTION) {
controller.removeDuplicates();
dupes = controller.checkDuplicates();
numDuplicates = dupes.total() - dupes.distinct();
logger.trace("{} duplicates found in history after cleanup", numDuplicates);
msg = String.format("Datenbank wurde bereinigt.\n%d Duplikate blieben übrig.", numDuplicates);
JOptionPane.showMessageDialog(mediathekGui, msg, Konstanten.PROGRAMMNAME, JOptionPane.INFORMATION_MESSAGE);
}
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(mediathekGui, ex.getMessage(), Konstanten.PROGRAMMNAME, JOptionPane.ERROR_MESSAGE);
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/mediathek/mainwindow/MediathekGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,12 @@ private void handleFilmlistWriteStopEvent(FilmListWriteStopEvent e) {
SwingUtilities.invokeLater(() -> loadFilmListAction.setEnabled(true));
}

private void createHelperToolsEntries() {
var menu = new JMenu("Hilfsmittel");
menu.add(new OptimizeHistoryDbAction(this));
jMenuHilfe.add(menu);
}

private void createHelpMenu() {
jMenuHilfe.add(new ShowOnlineHelpAction());
jMenuHilfe.add(new ShowOnlineFaqAction(this));
Expand All @@ -1003,6 +1009,9 @@ private void createHelpMenu() {
jMenuHilfe.addSeparator();
jMenuHilfe.add(new ResetFilterDialogPosition(this));
jMenuHilfe.addSeparator();
createHelperToolsEntries();
jMenuHilfe.addSeparator();

//do not show menu entry if we have external update support
if (GuiFunktionen.isNotUsingExternalUpdater()) {
jMenuHilfe.add(searchProgramUpdateAction);
Expand Down

0 comments on commit 403e055

Please sign in to comment.