Skip to content

Commit

Permalink
Add console command for reloading index/schema (openhab#13733)
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur authored and nemerdaud committed Feb 28, 2023
1 parent eb51d1f commit 09a4ca7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
6 changes: 6 additions & 0 deletions bundles/org.openhab.persistence.jdbc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -163,14 +169,20 @@ 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<String> getUsages() {
return Arrays.asList(
buildCommandUsage(CMD_TABLES + " " + SUBCMD_TABLES_LIST + " [" + PARAMETER_ALL + "]",
"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
Expand Down

0 comments on commit 09a4ca7

Please sign in to comment.