Skip to content

Commit e6cf76a

Browse files
committed
Add helper to create additional container indexes
1 parent 914ec46 commit e6cf76a

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

discvrcore/src/org/labkey/discvrcore/DiscvrCoreController.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,36 @@ public URLHelper getRedirectURL(Object o) throws Exception
302302
return DetailsURL.fromString("admin/manageFileRoot.view", getContainer()).getActionURL();
303303
}
304304
}
305+
306+
@UtilityAction(label = "Add Custom Core.Container Indexes", description = "Provides a mechanism to truncate the query and dataset audit tables for a container")
307+
@RequiresPermission(AdminPermission.class)
308+
public static class AddCustomIndexesAction extends ConfirmAction<Object>
309+
{
310+
@Override
311+
public ModelAndView getConfirmView(Object o, BindException errors) throws Exception
312+
{
313+
setTitle("Add Custom Core.Container Indexes");
314+
315+
return HtmlView.of("This action will add custom indexes to core.contains. Only do this if you are absolutely certain about the consequences. Do you want to continue?");
316+
}
317+
318+
@Override
319+
public boolean handlePost(Object o, BindException errors) throws Exception
320+
{
321+
return DiscvrCoreManager.get().addCoreContainersIndexes();
322+
}
323+
324+
@Override
325+
public void validateCommand(Object o, Errors errors)
326+
{
327+
328+
}
329+
330+
@NotNull
331+
@Override
332+
public URLHelper getSuccessURL(Object o)
333+
{
334+
return PageFlowUtil.urlProvider(PipelineUrls.class).urlBegin(getContainer());
335+
}
336+
}
305337
}

discvrcore/src/org/labkey/discvrcore/DiscvrCoreManager.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,26 @@
1616

1717
package org.labkey.discvrcore;
1818

19+
import org.apache.commons.lang3.StringUtils;
20+
import org.apache.logging.log4j.Logger;
21+
import org.labkey.api.collections.CaseInsensitiveHashSet;
22+
import org.labkey.api.data.CoreSchema;
23+
import org.labkey.api.data.SQLFragment;
24+
import org.labkey.api.data.SqlExecutor;
25+
import org.labkey.api.data.TableInfo;
26+
import org.labkey.api.util.logging.LogHelper;
27+
28+
import java.sql.DatabaseMetaData;
29+
import java.sql.ResultSet;
30+
import java.sql.SQLException;
31+
import java.util.Arrays;
32+
import java.util.List;
33+
import java.util.Set;
34+
1935
public class DiscvrCoreManager
2036
{
37+
private static final Logger _log = LogHelper.getLogger(DiscvrCoreManager.class, "Messages from DiscvrCoreManager");
38+
2139
private static final DiscvrCoreManager _instance = new DiscvrCoreManager();
2240

2341
private DiscvrCoreManager()
@@ -29,4 +47,59 @@ public static DiscvrCoreManager get()
2947
{
3048
return _instance;
3149
}
50+
51+
public boolean addCoreContainersIndexes()
52+
{
53+
try
54+
{
55+
TableInfo ti = CoreSchema.getInstance().getTableInfoContainers();
56+
addCustomIndex(ti, Arrays.asList("EntityId", "RowId", "Type", "Parent"));
57+
addCustomIndex(ti, Arrays.asList("Parent", "EntityId", "Type", "RowId"));
58+
59+
return true;
60+
}
61+
catch (Exception e)
62+
{
63+
_log.error("Unable to create container indexes", e);
64+
return false;
65+
}
66+
}
67+
68+
private void addCustomIndex(TableInfo ti, List<String> columnNames) throws Exception
69+
{
70+
String idxName = getIndexName(ti.getName(), columnNames);
71+
if (doesIndexExist(ti, idxName))
72+
{
73+
return;
74+
}
75+
76+
createIndex(ti, idxName, columnNames);
77+
}
78+
79+
private String getIndexName(String tableName, List<String> indexCols)
80+
{
81+
return "IDX_discvr_" + tableName + "_" + StringUtils.join(indexCols, "_");
82+
}
83+
84+
private boolean doesIndexExist(TableInfo ti, String indexName) throws SQLException
85+
{
86+
Set<String> indexNames = new CaseInsensitiveHashSet();
87+
DatabaseMetaData meta = ti.getSchema().getScope().getConnection().getMetaData();
88+
try (ResultSet rs = meta.getIndexInfo(ti.getSchema().getScope().getDatabaseName(), ti.getSchema().getName(), ti.getName(), false, false))
89+
{
90+
while (rs.next())
91+
{
92+
indexNames.add(rs.getString("INDEX_NAME"));
93+
}
94+
}
95+
96+
return indexNames.contains(indexName);
97+
}
98+
99+
private void createIndex(TableInfo realTable, String indexName, List<String> columns)
100+
{
101+
_log.info("Creating index on column(s): " + StringUtils.join(columns, ", ") + " for table: " + realTable.getName());
102+
SQLFragment sql = new SQLFragment("CREATE NONCLUSTERED INDEX " + indexName + " ON " + realTable.getSelectName() + "(" + StringUtils.join(columns, ", ") + ")");
103+
new SqlExecutor(realTable.getSchema()).execute(sql);
104+
}
32105
}

0 commit comments

Comments
 (0)