Skip to content

Commit d649f85

Browse files
Encho Belezirevenchobelezirev
Encho Belezirev
authored andcommitted
Refactor index sql changes
Change-Id: I5f8b0db3f9d03dde71cf52afe9323faa88f57348
1 parent 8242e21 commit d649f85

File tree

5 files changed

+65
-74
lines changed

5 files changed

+65
-74
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.sap.cloud.lm.sl.persistence.changes;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.SQLException;
6+
import java.text.MessageFormat;
7+
8+
import javax.sql.DataSource;
9+
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import com.sap.cloud.lm.sl.persistence.message.Messages;
14+
import com.sap.cloud.lm.sl.persistence.services.SqlExecutor;
15+
import com.sap.cloud.lm.sl.persistence.services.SqlExecutor.StatementExecutor;
16+
import com.sap.cloud.lm.sl.persistence.util.JdbcUtil;
17+
18+
public abstract class AbstractIndexSQLChange implements AsyncChange {
19+
20+
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractIndexSQLChange.class);
21+
22+
@Override
23+
public void execute(DataSource dataSource) throws SQLException {
24+
SqlExecutor executor = new SqlExecutor(dataSource);
25+
executor.execute(new StatementExecutor<Void>() {
26+
27+
@Override
28+
public Void execute(Connection connection) throws SQLException {
29+
PreparedStatement statement = null;
30+
try {
31+
LOGGER.info(MessageFormat.format(Messages.CREATING_INDEX_CONCURRENTLY, getIndexName()));
32+
statement = connection.prepareStatement(getQuery());
33+
statement.executeUpdate();
34+
LOGGER.info(Messages.INDEX_CREATED);
35+
} finally {
36+
JdbcUtil.closeQuietly(statement);
37+
}
38+
return null;
39+
}
40+
41+
});
42+
}
43+
44+
protected abstract String getQuery();
45+
46+
protected abstract String getIndexName();
47+
}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,15 @@
11
package com.sap.cloud.lm.sl.persistence.changes;
22

3-
import java.sql.Connection;
4-
import java.sql.PreparedStatement;
5-
import java.sql.SQLException;
6-
7-
import javax.sql.DataSource;
8-
9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
11-
12-
import com.sap.cloud.lm.sl.persistence.message.Messages;
13-
import com.sap.cloud.lm.sl.persistence.services.SqlExecutor;
14-
import com.sap.cloud.lm.sl.persistence.services.SqlExecutor.StatementExecutor;
15-
import com.sap.cloud.lm.sl.persistence.util.JdbcUtil;
16-
17-
public class IndexProcessIdsOfProgressMessagesPostgreSQLChange implements AsyncChange {
18-
19-
private static final Logger LOGGER = LoggerFactory.getLogger(IndexProcessIdsOfProgressMessagesPostgreSQLChange.class);
20-
private static final String CREATE_INDEX = "CREATE INDEX CONCURRENTLY IDX_PROGRESS_MESSAGE_PROCESS_ID ON PROGRESS_MESSAGE(PROCESS_ID)";
3+
public class IndexProcessIdsOfProgressMessagesPostgreSQLChange extends AbstractIndexSQLChange {
214

225
@Override
23-
public void execute(DataSource dataSource) throws SQLException {
24-
SqlExecutor executor = new SqlExecutor(dataSource);
25-
executor.execute(new StatementExecutor<Void>() {
26-
27-
@Override
28-
public Void execute(Connection connection) throws SQLException {
29-
PreparedStatement statement = null;
30-
try {
31-
LOGGER.info(Messages.INDEXING_PROCESS_IDS_OF_PROGRESS_MESSAGES);
32-
statement = connection.prepareStatement(CREATE_INDEX);
33-
statement.executeUpdate();
34-
LOGGER.info(Messages.PROCESS_IDS_INDEXED);
35-
} finally {
36-
JdbcUtil.closeQuietly(statement);
37-
}
38-
return null;
39-
}
6+
protected String getQuery() {
7+
return "CREATE INDEX CONCURRENTLY IDX_PROGRESS_MESSAGE_PROCESS_ID ON PROGRESS_MESSAGE(PROCESS_ID)";
8+
}
409

41-
});
10+
@Override
11+
protected String getIndexName() {
12+
return "IDX_PROGRESS_MESSAGE_PROCESS_ID";
4213
}
4314

4415
}
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
11
package com.sap.cloud.lm.sl.persistence.changes;
22

3-
import java.sql.Connection;
4-
import java.sql.PreparedStatement;
5-
import java.sql.SQLException;
6-
7-
import javax.sql.DataSource;
8-
9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
11-
12-
import com.sap.cloud.lm.sl.persistence.services.SqlExecutor;
13-
import com.sap.cloud.lm.sl.persistence.services.SqlExecutor.StatementExecutor;
14-
import com.sap.cloud.lm.sl.persistence.util.JdbcUtil;
15-
16-
public class IndexSpaceOfLmSlPersistenceFilePostgreSQLChange implements AsyncChange {
17-
18-
private static final Logger LOGGER = LoggerFactory.getLogger(IndexProcessIdsOfProgressMessagesPostgreSQLChange.class);
19-
private static final String CREATE_INDEX = "CREATE INDEX CONCURRENTLY IDX_LM_SL_PERSISTENCE_SPACE ON LM_SL_PERSISTENCE_FILE(SPACE)";
3+
public class IndexSpaceOfLmSlPersistenceFilePostgreSQLChange extends AbstractIndexSQLChange {
204

215
@Override
22-
public void execute(DataSource dataSource) throws SQLException {
23-
SqlExecutor executor = new SqlExecutor(dataSource);
24-
executor.execute(new StatementExecutor<Void>() {
25-
26-
@Override
27-
public Void execute(Connection connection) throws SQLException {
28-
PreparedStatement statement = null;
29-
try {
30-
LOGGER.info("Processing index for column space of lm_sl_persistence_file");
31-
statement = connection.prepareStatement(CREATE_INDEX);
32-
statement.executeUpdate();
33-
LOGGER.info("Space column indexed");
34-
} finally {
35-
JdbcUtil.closeQuietly(statement);
36-
}
37-
return null;
38-
}
6+
protected String getQuery() {
7+
return "CREATE INDEX CONCURRENTLY IDX_LM_SL_PERSISTENCE_SPACE ON LM_SL_PERSISTENCE_FILE(SPACE)";
8+
}
399

40-
});
10+
@Override
11+
protected String getIndexName() {
12+
return "IDX_LM_SL_PERSISTENCE_SPACE";
4113
}
14+
4215
}

com.sap.cloud.lm.sl.persistence/src/main/java/com/sap/cloud/lm/sl/persistence/message/Messages.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public final class Messages {
4646
public static final String SCANNING_FILE_SUCCESS = "File \"{0}\" is not infected";
4747
public static final String FAILED_TO_DELETE_FILE = "Failed to delete file {0}";
4848
public static final String DELETING_FILE_IN_TABLE = "File \"{0}\" in space \"{1}\" will be deleted from \"{2}\".";
49-
public static final String INDEXING_PROCESS_IDS_OF_PROGRESS_MESSAGES = "Indexing process IDs of progress messages...";
50-
public static final String PROCESS_IDS_INDEXED = "Process IDs of progress messages indexed successfully!";
49+
public static final String CREATING_INDEX_CONCURRENTLY = "Creating index {0} concurrently";
50+
public static final String INDEX_CREATED = "Index created.";
5151

5252
// DEBUG log messages:
5353
public static final String DELETING_FILE_WITH_PATH = "Deleting file with path {0}...";

com.sap.cloud.lm.sl.persistence/src/main/resources/com/sap/cloud/lm/sl/persistence/db/changelog/db-changelog-index_space_lm_sl_persistence_file.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<changeSet author="sap.com" id="index_space_of_lm_sl_persistence_file">
77
<preConditions onFail="MARK_RAN">
8-
<!-- TODO: See: com.sap.cloud.lm.sl.persistence.changes.IndexSpaceOfLmSlPersistenceFilePostgreSQLChange -->
8+
<!-- See: com.sap.cloud.lm.sl.persistence.changes.IndexSpaceOfLmSlPersistenceFilePostgreSQLChange -->
99
<not>
1010
<dbms type="postgresql" />
1111
</not>

0 commit comments

Comments
 (0)