[#861] server-network config system now stores last modified date instead

This prevents unnecessary config update after it is saved into the db
This commit is contained in:
Rsl1122 2019-01-17 10:44:44 +02:00
parent 88fd623718
commit d6ef6ec566
7 changed files with 23 additions and 17 deletions

View File

@ -85,5 +85,5 @@ public interface SaveOperations {
void setAsUninstalled(UUID serverUUID); void setAsUninstalled(UUID serverUUID);
void saveConfig(UUID serverUUID, Config config); void saveConfig(UUID serverUUID, Config config, long lastModified);
} }

View File

@ -155,7 +155,7 @@ public class SQLSaveOps extends SQLOps implements SaveOperations {
} }
@Override @Override
public void saveConfig(UUID serverUUID, Config config) { public void saveConfig(UUID serverUUID, Config config, long lastModified) {
settingsTable.storeConfig(serverUUID, config); settingsTable.storeConfig(serverUUID, config, lastModified);
} }
} }

View File

@ -66,22 +66,23 @@ public class SettingsTable extends Table {
* <p> * <p>
* Only one config is stored per server uuid. * Only one config is stored per server uuid.
* *
* @param serverUUID UUID of the server. * @param serverUUID UUID of the server.
* @param config Config of the server. * @param config Config of the server.
* @param lastModified Epoch ms the config file was last modified.
*/ */
public void storeConfig(UUID serverUUID, Config config) { public void storeConfig(UUID serverUUID, Config config, long lastModified) {
TextStringBuilder configTextBuilder = new TextStringBuilder(); TextStringBuilder configTextBuilder = new TextStringBuilder();
List<String> lines = new ConfigWriter().parseLines(config); List<String> lines = new ConfigWriter().parseLines(config);
configTextBuilder.appendWithSeparators(lines, "\n"); configTextBuilder.appendWithSeparators(lines, "\n");
String configSettings = configTextBuilder.toString(); String configSettings = configTextBuilder.toString();
if (isConfigStored(serverUUID)) { if (isConfigStored(serverUUID)) {
updateConfig(serverUUID, configSettings); updateConfig(serverUUID, configSettings, lastModified);
} else { } else {
insertConfig(serverUUID, configSettings); insertConfig(serverUUID, configSettings, lastModified);
} }
} }
private void insertConfig(UUID serverUUID, String configSettings) { private void insertConfig(UUID serverUUID, String configSettings, long lastModified) {
String sql = "INSERT INTO " + tableName + " (" + String sql = "INSERT INTO " + tableName + " (" +
Col.SERVER_UUID + ", " + Col.SERVER_UUID + ", " +
Col.UPDATED + ", " + Col.UPDATED + ", " +
@ -91,13 +92,13 @@ public class SettingsTable extends Table {
@Override @Override
public void prepare(PreparedStatement statement) throws SQLException { public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString()); statement.setString(1, serverUUID.toString());
statement.setLong(2, System.currentTimeMillis()); statement.setLong(2, lastModified);
statement.setString(3, configSettings); statement.setString(3, configSettings);
} }
}); });
} }
private void updateConfig(UUID serverUUID, String configSettings) { private void updateConfig(UUID serverUUID, String configSettings, long lastModified) {
String sql = "UPDATE " + tableName + " SET " + String sql = "UPDATE " + tableName + " SET " +
Col.CONFIG_CONTENT + "=?," + Col.CONFIG_CONTENT + "=?," +
Col.UPDATED + "=? WHERE " + Col.UPDATED + "=? WHERE " +
@ -108,7 +109,7 @@ public class SettingsTable extends Table {
@Override @Override
public void prepare(PreparedStatement statement) throws SQLException { public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, configSettings); statement.setString(1, configSettings);
statement.setLong(2, System.currentTimeMillis()); statement.setLong(2, lastModified);
statement.setString(3, serverUUID.toString()); statement.setString(3, serverUUID.toString());
statement.setString(4, configSettings); statement.setString(4, configSettings);
} }

View File

@ -203,7 +203,7 @@ public class NetworkSettingManager implements SubSystem {
try (ConfigReader reader = new ConfigReader(file.toPath())) { try (ConfigReader reader = new ConfigReader(file.toPath())) {
Config config = reader.read(); Config config = reader.read();
database.save().saveConfig(serverUUID, config); database.save().saveConfig(serverUUID, config, file.lastModified());
String serverName = config.getNode(PluginSettings.SERVER_NAME.getPath()).map(ConfigNode::getString).orElse("Unknown"); String serverName = config.getNode(PluginSettings.SERVER_NAME.getPath()).map(ConfigNode::getString).orElse("Unknown");
logger.debug("Server config '" + serverName + "' in db now up to date."); logger.debug("Server config '" + serverName + "' in db now up to date.");
} catch (IOException e) { } catch (IOException e) {

View File

@ -109,7 +109,7 @@ public class ServerSettingsManager implements SubSystem {
try (ConfigReader reader = new ConfigReader(file.toPath())) { try (ConfigReader reader = new ConfigReader(file.toPath())) {
Config config = reader.read(); Config config = reader.read();
database.save().saveConfig(serverInfo.getServerUUID(), config); database.save().saveConfig(serverInfo.getServerUUID(), config, file.lastModified());
logger.debug("Server config saved to database."); logger.debug("Server config saved to database.");
} catch (IOException e) { } catch (IOException e) {
throw new UncheckedIOException(e); throw new UncheckedIOException(e);

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.system.tasks.server; package com.djrapitops.plan.system.tasks.server;
import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.file.PlanFiles;
import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plugin.logging.console.PluginLogger; import com.djrapitops.plugin.logging.console.PluginLogger;
@ -33,6 +34,7 @@ import javax.inject.Singleton;
@Singleton @Singleton
public class ConfigStoreTask extends AbsRunnable { public class ConfigStoreTask extends AbsRunnable {
private final PlanFiles files;
private final PlanConfig config; private final PlanConfig config;
private final ServerInfo serverInfo; private final ServerInfo serverInfo;
private final DBSystem dbSystem; private final DBSystem dbSystem;
@ -40,11 +42,13 @@ public class ConfigStoreTask extends AbsRunnable {
@Inject @Inject
public ConfigStoreTask( public ConfigStoreTask(
PlanFiles files,
PlanConfig config, PlanConfig config,
ServerInfo serverInfo, ServerInfo serverInfo,
DBSystem dbSystem, DBSystem dbSystem,
PluginLogger logger PluginLogger logger
) { ) {
this.files = files;
this.config = config; this.config = config;
this.serverInfo = serverInfo; this.serverInfo = serverInfo;
this.dbSystem = dbSystem; this.dbSystem = dbSystem;
@ -53,7 +57,8 @@ public class ConfigStoreTask extends AbsRunnable {
@Override @Override
public void run() { public void run() {
dbSystem.getDatabase().save().saveConfig(serverInfo.getServerUUID(), config); long lastModified = files.getConfigFile().lastModified();
dbSystem.getDatabase().save().saveConfig(serverInfo.getServerUUID(), config, lastModified);
logger.debug("Config Store Task - Config in db now up to date."); logger.debug("Config Store Task - Config in db now up to date.");
cancel(); cancel();
} }

View File

@ -1039,7 +1039,7 @@ public abstract class CommonDBTest {
PlanConfig config = system.getConfigSystem().getConfig(); PlanConfig config = system.getConfigSystem().getConfig();
SettingsTable settingsTable = db.getSettingsTable(); SettingsTable settingsTable = db.getSettingsTable();
settingsTable.storeConfig(serverUUID, config); settingsTable.storeConfig(serverUUID, config, System.currentTimeMillis());
Optional<Config> foundConfig = settingsTable.fetchNewerConfig(0, serverUUID); Optional<Config> foundConfig = settingsTable.fetchNewerConfig(0, serverUUID);
assertTrue(foundConfig.isPresent()); assertTrue(foundConfig.isPresent());
@ -1054,7 +1054,7 @@ public abstract class CommonDBTest {
PlanConfig config = system.getConfigSystem().getConfig(); PlanConfig config = system.getConfigSystem().getConfig();
SettingsTable settingsTable = db.getSettingsTable(); SettingsTable settingsTable = db.getSettingsTable();
settingsTable.storeConfig(serverUUID, config); settingsTable.storeConfig(serverUUID, config, System.currentTimeMillis());
assertFalse(settingsTable.fetchNewerConfig(savedMs, serverUUID).isPresent()); assertFalse(settingsTable.fetchNewerConfig(savedMs, serverUUID).isPresent());
} }