1 10 package org.mmbase.storage.search.implementation.database.informix.excalibur; 11 12 import java.io.*; 13 import java.sql.*; 14 import java.util.*; 15 16 import org.mmbase.util.xml.ModuleReader; 17 import org.w3c.dom.*; 18 import org.xml.sax.*; 19 20 40 public class EtxIndexCreator { 41 42 43 private String configDir = null; 44 45 46 private Connection con = null; 47 48 53 public EtxIndexCreator(String configDir) throws Exception { 54 this.configDir = configDir; 55 56 ModuleReader moduleReader = new ModuleReader(new InputSource(new FileInputStream(configDir + "/modules/jdbc.xml"))); 59 Map properties = moduleReader.getProperties(); 60 String url = (String ) properties.get("url"); 61 String host = (String ) properties.get("host"); 62 String port = (String ) properties.get("port"); 63 String database = (String ) properties.get("database"); 64 String user = (String ) properties.get("user"); 65 String password = (String ) properties.get("password"); 66 String driver = (String ) properties.get("driver"); 67 int pos = url.indexOf("$DBM"); 69 if (pos != -1) { 70 url = url.substring(0, pos) + database + url.substring(pos + 4); 71 } 72 pos = url.indexOf("$HOST"); 73 if (pos !=- 1) { 74 url = url.substring(0, pos) + host + url.substring(pos + 5); 75 } 76 pos=url.indexOf("$PORT"); 77 if (pos != -1) { 78 url = url.substring(0, pos) + port + url.substring(pos + 5); 79 } 80 Class.forName(driver); 82 if (user.equals("url") && password.equals("url")) { 84 con = DriverManager.getConnection(url); 85 } else { 86 con = DriverManager.getConnection(url, user, password); 87 } 88 } 89 90 99 public static void main(String [] args) { 100 if (args.length != 1) { 101 System.out.println("Command line arguments not as expected," 102 + "should be path to MMBase configuration directory."); 103 System.exit(1); 104 } 105 try { 106 new EtxIndexCreator(args[0]).execute(); 108 } catch (Exception e) { 109 e.printStackTrace(); 110 } 111 } 112 113 117 public void execute() throws Exception { 118 try { 119 File etxConfigFile = new File( 121 configDir + "/databases/etxindices.xml"); 122 XmlEtxIndicesReader configReader = 123 new XmlEtxIndicesReader( 124 new InputSource( 125 new BufferedReader( 126 new FileReader(etxConfigFile)))); 127 128 for (Iterator iSbspaces = configReader.getSbspaceElements(); iSbspaces.hasNext();) { 129 Element sbspace = (Element) iSbspaces.next(); 130 String sbspaceName = configReader.getSbspaceName(sbspace); 131 132 for (Iterator iEtxindices = configReader.getEtxindexElements(sbspace); iEtxindices.hasNext();) { 133 Element etxindex = (Element) iEtxindices.next(); 134 String name = configReader.getEtxindexValue(etxindex); 135 String table = configReader.getEtxindexTable(etxindex); 136 String field = configReader.getEtxindexField(etxindex); 137 if (!etxIndexExists(name)) { 138 createEtxIndex(sbspaceName, 139 name, table, field); 140 } 141 } 142 } 143 144 } finally { 145 if (con != null) { 146 con.close(); 147 } 148 } 149 } 150 151 160 private boolean etxIndexExists(String etxindexName) throws SQLException { 161 PreparedStatement ps = null; 162 ResultSet rs = null; 163 try { 164 ps = con.prepareStatement( 165 "SELECT * FROM sysindexes WHERE idxname = ?"); 166 ps.setString(1, etxindexName); 167 try { 168 rs = ps.executeQuery(); 169 170 if (rs.next()) { 171 System.out.println("Index " + etxindexName + " exists already."); 172 return true; 173 } else { 174 System.out.println("Index " + etxindexName + " does not exist already."); 175 return false; 176 } 177 } finally { 178 if (rs != null) { 179 rs.close(); 180 } 181 } 182 } finally { 183 if (ps != null) { 184 ps.close(); 185 } 186 } 187 } 188 189 197 private void createEtxIndex(String sbspace, 198 String name, String table, String field) 199 throws SQLException { 200 String operatorclass = getOperatorClass(table, field); 201 String sqlCreateIndex = 202 "CREATE INDEX " + name 203 + " ON " + table + " (" + field + " " + operatorclass 204 + ") USING etx (CHAR_SET='OVERLAP_ISO', " 205 + "PHRASE_SUPPORT='MAXIMUM', " 206 + "WORD_SUPPORT='PATTERN') IN " + sbspace; 207 208 Statement st = null; 209 try { 210 System.out.println(sqlCreateIndex); 211 st = con.createStatement(); 212 st.executeUpdate(sqlCreateIndex); 213 System.out.println("Index " + name + " created."); 214 } finally { 215 if (st != null) { 216 st.close(); 217 } 218 } 219 } 220 221 229 private String getOperatorClass(String table, String field) throws SQLException { 230 DatabaseMetaData metadata = con.getMetaData(); 231 ResultSet columninfo = metadata.getColumns(null, null, table, field); 232 try { 233 boolean hasRows = columninfo.next(); 234 if (!hasRows) { 235 throw new IllegalArgumentException ( 236 "The field " + field + " of table " + table 237 + " does not exist."); 238 } 239 String typeName = columninfo.getString("TYPE_NAME").toLowerCase(); 240 if (typeName.equals("blob")) { 241 return "etx_blob_ops"; 242 } else if (typeName.equals("clob")) { 243 return "etx_clob_ops"; 244 } else if (typeName.equals("char")) { 245 return "etx_char_ops"; 246 } else if (typeName.equals("lvarchar")) { 247 return "etx_lvarc_ops"; 248 } else if (typeName.equals("varchar")) { 249 return ("etx_varc_ops"); 250 } else { 251 throw new IllegalArgumentException ( 252 "The field " + field + " of table " + table 253 + " is not of an appropriate type for an Etx index."); 254 } 255 } finally { 256 columninfo.close(); 257 } 258 } 259 } 260 | Popular Tags |