diff --git a/bundles/org.openhab.persistence.jdbc/README.md b/bundles/org.openhab.persistence.jdbc/README.md index bec3af3fd3304..144cc7758df0a 100644 --- a/bundles/org.openhab.persistence.jdbc/README.md +++ b/bundles/org.openhab.persistence.jdbc/README.md @@ -195,6 +195,12 @@ This happened: In other words, extracting this information from the index before removing it, can be beneficial in order to understand the issues and possible causes. +#### Reload Index/Schema + +Manual changes in the index table, `Items`, will not be picked up automatically for performance reasons. +The same is true when manually adding new item tables or deleting existing ones. +After making such changes, the command `jdbc reload` can be used to reload the index. + ### For Developers * Clearly separated source files for the database-specific part of openHAB logic. diff --git a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcMapper.java b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcMapper.java index 27422eedbb30c..2a6ba22aa9e36 100644 --- a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcMapper.java +++ b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcMapper.java @@ -302,7 +302,7 @@ protected void checkDBSchema() throws JdbcSQLException { populateItemNameToTableNameMap(); } - private void populateItemNameToTableNameMap() throws JdbcSQLException { + public void populateItemNameToTableNameMap() throws JdbcSQLException { itemNameToTableNameMap.clear(); if (conf.getTableUseRealCaseSensitiveItemNames()) { for (String itemName : getItemTables().stream().map(t -> t.getTableName()).collect(Collectors.toList())) { diff --git a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/console/JdbcCommandExtension.java b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/console/JdbcCommandExtension.java index 8b6dcbbdb3998..14f674eef131d 100644 --- a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/console/JdbcCommandExtension.java +++ b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/console/JdbcCommandExtension.java @@ -45,11 +45,12 @@ public class JdbcCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter { private static final String CMD_TABLES = "tables"; + private static final String CMD_RELOAD = "reload"; private static final String SUBCMD_TABLES_LIST = "list"; private static final String SUBCMD_TABLES_CLEAN = "clean"; private static final String PARAMETER_ALL = "all"; private static final String PARAMETER_FORCE = "force"; - private static final StringsCompleter CMD_COMPLETER = new StringsCompleter(List.of(CMD_TABLES), false); + private static final StringsCompleter CMD_COMPLETER = new StringsCompleter(List.of(CMD_TABLES, CMD_RELOAD), false); private static final StringsCompleter SUBCMD_TABLES_COMPLETER = new StringsCompleter( List.of(SUBCMD_TABLES_LIST, SUBCMD_TABLES_CLEAN), false); @@ -63,7 +64,7 @@ public JdbcCommandExtension(final @Reference PersistenceServiceRegistry persiste @Override public void execute(String[] args, Console console) { - if (args.length < 2 || args.length > 4 || !CMD_TABLES.equals(args[0])) { + if (args.length < 1 || args.length > 4) { printUsage(console); return; } @@ -92,20 +93,25 @@ public void execute(String[] args, Console console) { private boolean execute(JdbcPersistenceService persistenceService, String[] args, Console console) throws JdbcSQLException { - if (SUBCMD_TABLES_LIST.equalsIgnoreCase(args[1])) { - listTables(persistenceService, console, args.length == 3 && PARAMETER_ALL.equalsIgnoreCase(args[2])); - return true; - } else if (SUBCMD_TABLES_CLEAN.equalsIgnoreCase(args[1])) { - if (args.length == 3) { - cleanupItem(persistenceService, console, args[2], false); - return true; - } else if (args.length == 4 && PARAMETER_FORCE.equalsIgnoreCase(args[3])) { - cleanupItem(persistenceService, console, args[2], true); - return true; - } else { - cleanupTables(persistenceService, console); + if (args.length > 1 && CMD_TABLES.equalsIgnoreCase(args[0])) { + if (SUBCMD_TABLES_LIST.equalsIgnoreCase(args[1])) { + listTables(persistenceService, console, args.length == 3 && PARAMETER_ALL.equalsIgnoreCase(args[2])); return true; + } else if (SUBCMD_TABLES_CLEAN.equalsIgnoreCase(args[1])) { + if (args.length == 3) { + cleanupItem(persistenceService, console, args[2], false); + return true; + } else if (args.length == 4 && PARAMETER_FORCE.equalsIgnoreCase(args[3])) { + cleanupItem(persistenceService, console, args[2], true); + return true; + } else { + cleanupTables(persistenceService, console); + return true; + } } + } else if (args.length == 1 && CMD_RELOAD.equalsIgnoreCase(args[0])) { + reload(persistenceService, console); + return true; } return false; } @@ -163,6 +169,11 @@ private void cleanupItem(JdbcPersistenceService persistenceService, Console cons } } + private void reload(JdbcPersistenceService persistenceService, Console console) throws JdbcSQLException { + persistenceService.populateItemNameToTableNameMap(); + console.println("Item index reloaded."); + } + @Override public List getUsages() { return Arrays.asList( @@ -170,7 +181,8 @@ public List getUsages() { "list tables (all = include valid)"), buildCommandUsage( CMD_TABLES + " " + SUBCMD_TABLES_CLEAN + " []" + " [" + PARAMETER_FORCE + "]", - "clean inconsistent items (remove from index and drop tables)")); + "clean inconsistent items (remove from index and drop tables)"), + buildCommandUsage(CMD_RELOAD, "reload item index/schema")); } @Override