1 21 22 package org.apache.derby.diag; 23 24 import java.io.BufferedReader ; 25 import java.io.FileNotFoundException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.io.FileInputStream ; 29 30 import java.util.Hashtable ; 31 import java.util.Enumeration ; 32 import java.util.Properties ; 33 import java.sql.ResultSetMetaData ; 34 import java.sql.SQLException ; 35 import java.sql.Timestamp ; 36 import java.sql.Types ; 37 import org.apache.derby.vti.VTITemplate; 38 import org.apache.derby.iapi.sql.ResultColumnDescriptor; 39 import org.apache.derby.impl.jdbc.EmbedResultSetMetaData; 40 import org.apache.derby.iapi.reference.Limits; 41 import org.apache.derby.iapi.util.StringUtil; 42 43 75 public class StatementDuration extends VTITemplate 76 { 77 80 private boolean gotFile; 81 private InputStreamReader inputFileStreamReader; 82 private InputStream inputStream; 83 private BufferedReader bufferedReader; 84 private String inputFileName; 85 private Hashtable hashTable; 86 87 private String line; 89 private int gmtIndex; 90 private int threadIndex; 91 private int xidIndex; 92 private int lccidIndex; 93 private String [] currentRow; 94 95 private static final String GMT_STRING = " GMT"; 96 private static final String BEGIN_THREAD_STRING = "["; 97 private static final String END_THREAD_STRING = "]"; 98 private static final String BEGIN_XID_STRING = "= "; 99 private static final String END_XID_STRING = ")"; 100 private static final String BEGIN_EXECUTING_STRING = "Executing prepared"; 101 private static final String END_EXECUTING_STRING = " :End prepared"; 102 103 104 110 public StatementDuration() 111 { 112 String home = System.getProperty("derby.system.home"); 113 114 inputFileName = "derby.log"; 115 116 if (home != null) 117 { 118 inputFileName = home + "/" + inputFileName; 119 } 120 } 121 122 public StatementDuration(String inputFileName) 123 { 124 this.inputFileName = inputFileName; 125 } 126 127 130 public ResultSetMetaData getMetaData() 131 { 132 return metadata; 133 } 134 135 139 public boolean next() throws SQLException 140 { 141 if (! gotFile) 142 { 143 gotFile = true; 144 try 145 { 146 inputFileStreamReader = new InputStreamReader (new FileInputStream (inputFileName)); 147 bufferedReader = new BufferedReader (inputFileStreamReader, 32*1024); 148 } 149 catch (FileNotFoundException ex) 150 { 151 throw new SQLException (ex.getMessage()); 152 } 153 154 hashTable = new Hashtable (); 155 } 156 157 while (true) 158 { 159 try 160 { 161 line = bufferedReader.readLine(); 162 } 163 catch (java.io.IOException ioe) 164 { 165 throw new SQLException (ioe.getMessage()); 166 } 167 168 if (line == null) 169 { 170 return false; 171 } 172 173 gmtIndex = line.indexOf(GMT_STRING); 174 threadIndex = line.indexOf(BEGIN_THREAD_STRING); 175 xidIndex = line.indexOf(BEGIN_XID_STRING); 176 lccidIndex = line.indexOf(BEGIN_XID_STRING, xidIndex + 1); 177 178 if (gmtIndex != -1 && threadIndex != -1) 179 { 180 181 String [] newRow = new String [6]; 182 for (int index = 1; 183 index <= 5; 184 index++) 185 { 186 newRow[index - 1] = setupColumn(index); 187 } 188 189 191 Object previousRow = hashTable.put(newRow[3], 192 newRow); 193 if (previousRow == null) 194 { 195 continue; 196 } 197 198 currentRow = (String []) previousRow; 199 200 201 Timestamp endTs = Timestamp.valueOf(newRow[0]); 202 long end = endTs.getTime() + endTs.getNanos() / 1000000; 203 Timestamp startTs = Timestamp.valueOf(currentRow[0]); 204 long start = startTs.getTime() + startTs.getNanos() / 1000000; 205 currentRow[5] = Long.toString(end - start); 206 207 return true; 208 } 209 } 210 } 211 212 215 public void close() 216 { 217 if (bufferedReader != null) 218 { 219 try 220 { 221 bufferedReader.close(); 222 inputFileStreamReader.close(); 223 } 224 catch (java.io.IOException ioe) 225 { 226 } 228 finally 229 { 230 bufferedReader = null; 231 inputFileStreamReader = null; 232 } 233 } 234 } 235 236 241 public String getString(int columnNumber) 242 throws SQLException 243 { 244 return currentRow[columnNumber - 1]; 245 } 246 247 private String setupColumn(int columnNumber) 248 throws SQLException 249 { 250 switch (columnNumber) 251 { 252 case 1: 253 return line.substring(0, gmtIndex); 254 255 case 2: 256 return line.substring(threadIndex + 1, line.indexOf(END_THREAD_STRING)); 257 258 case 3: 259 return line.substring(xidIndex + 2, line.indexOf(END_XID_STRING, xidIndex)); 260 261 case 4: 262 return line.substring(lccidIndex + 2, line.indexOf(END_XID_STRING, lccidIndex)); 263 264 case 5: 265 268 String output; 269 if (line.indexOf(BEGIN_EXECUTING_STRING) == -1) 270 { 271 output = line.substring(line.indexOf(END_XID_STRING, lccidIndex) + 3); 272 } 273 else 274 { 275 276 277 int endIndex = line.indexOf(END_EXECUTING_STRING, lccidIndex); 278 if (endIndex == -1) 279 { 280 output = line.substring(line.indexOf(END_XID_STRING, lccidIndex) + 3); 281 } 282 else 283 { 284 output = line.substring(line.indexOf(END_XID_STRING, lccidIndex) + 3, 285 endIndex); 286 } 287 288 while (endIndex == -1) 289 { 290 try 291 { 292 line = bufferedReader.readLine(); 293 } 294 catch (java.io.IOException ioe) 295 { 296 throw new SQLException ("Error reading file " + ioe); 297 } 298 endIndex = line.indexOf(END_EXECUTING_STRING); 299 if (endIndex == -1) 300 { 301 output = output + line; 302 } 303 else 304 { 305 output = output + line.substring(0, endIndex); 306 } 307 } 308 } 309 310 output = StringUtil.truncate(output, Limits.DB2_VARCHAR_MAXWIDTH); 311 312 313 return output; 314 315 default: 316 return null; 317 } 318 } 319 320 321 324 public boolean wasNull() 325 { 326 return false; 327 } 328 329 332 private static final ResultColumnDescriptor[] columnInfo = { 333 334 EmbedResultSetMetaData.getResultColumnDescriptor("TS", Types.VARCHAR, false, 26), 335 EmbedResultSetMetaData.getResultColumnDescriptor("THREADID", Types.VARCHAR, false, 80), 336 EmbedResultSetMetaData.getResultColumnDescriptor("XID", Types.VARCHAR, false, 15), 337 EmbedResultSetMetaData.getResultColumnDescriptor("LCCID", Types.VARCHAR, false, 10), 338 EmbedResultSetMetaData.getResultColumnDescriptor("LOGTEXT", Types.VARCHAR, true, Limits.DB2_VARCHAR_MAXWIDTH), 339 EmbedResultSetMetaData.getResultColumnDescriptor("DURATION", Types.VARCHAR, false, 10), 340 }; 341 342 private static final ResultSetMetaData metadata = new EmbedResultSetMetaData(columnInfo); 343 } 344 345 | Popular Tags |