1616
1717package 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+
1935public 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