]> git.basschouten.com Git - openhab-addons.git/commitdiff
Add console command for reloading index/schema (#13733)
authorJacob Laursen <jacob-github@vindvejr.dk>
Thu, 17 Nov 2022 18:52:54 +0000 (19:52 +0100)
committerGitHub <noreply@github.com>
Thu, 17 Nov 2022 18:52:54 +0000 (19:52 +0100)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
bundles/org.openhab.persistence.jdbc/README.md
bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcMapper.java
bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/console/JdbcCommandExtension.java

index bec3af3fd330468158d2c15bc23c8c26a1c25320..144cc7758df0a101a11ca610ac269dcddb18cf54 100644 (file)
@@ -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.
index 27422eedbb30c8cd009f3e98a4b0a8db3240d59a..2a6ba22aa9e36316bc65d155f6945dc3ed4096cb 100644 (file)
@@ -302,7 +302,7 @@ public class JdbcMapper {
         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())) {
index 8b6dcbbdb39986a4eef72d8df0166f236b8c808a..14f674eef131d94a755034fa42e0d7e1b6f9490a 100644 (file)
@@ -45,11 +45,12 @@ import org.osgi.service.component.annotations.Reference;
 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 class JdbcCommandExtension extends AbstractConsoleCommandExtension implem
 
     @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 class JdbcCommandExtension extends AbstractConsoleCommandExtension implem
 
     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 @@ public class JdbcCommandExtension extends AbstractConsoleCommandExtension implem
         }
     }
 
+    private void reload(JdbcPersistenceService persistenceService, Console console) throws JdbcSQLException {
+        persistenceService.populateItemNameToTableNameMap();
+        console.println("Item index reloaded.");
+    }
+
     @Override
     public List<String> getUsages() {
         return Arrays.asList(
@@ -170,7 +181,8 @@ public class JdbcCommandExtension extends AbstractConsoleCommandExtension implem
                         "list tables (all = include valid)"),
                 buildCommandUsage(
                         CMD_TABLES + " " + SUBCMD_TABLES_CLEAN + " [<itemName>]" + " [" + 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