1 21 22 package org.apache.derby.diag; 23 24 import org.apache.derby.iapi.services.monitor.Monitor; 25 import org.apache.derby.iapi.services.sanity.SanityManager; 26 27 import org.apache.derby.iapi.error.StandardException; 28 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 29 import org.apache.derby.iapi.sql.conn.ConnectionUtil; 30 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 31 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 32 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 33 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 34 import org.apache.derby.iapi.store.access.AccessFactory; 35 import org.apache.derby.iapi.store.access.TransactionController; 36 import org.apache.derby.iapi.store.access.ConglomerateController; 37 import org.apache.derby.iapi.store.access.SpaceInfo; 38 import org.apache.derby.iapi.error.PublicAPI; 39 40 import org.apache.derby.iapi.sql.ResultColumnDescriptor; 41 import org.apache.derby.impl.jdbc.EmbedResultSetMetaData; 42 43 import java.sql.ResultSetMetaData ; 44 import java.sql.SQLException ; 45 import java.sql.Types ; 46 import org.apache.derby.vti.VTITemplate; 47 import org.apache.derby.vti.VTICosting; 48 import org.apache.derby.vti.VTIEnvironment; 49 50 109 public class SpaceTable extends VTITemplate implements VTICosting { 110 111 private ConglomInfo[] conglomTable; 112 boolean initialized; 113 int currentRow; 114 private boolean wasNull; 115 private String schemaName; 116 private String tableName; 117 private SpaceInfo spaceInfo; 118 private TransactionController tc; 119 120 121 public SpaceTable(String schemaName, String tableName) 122 { 123 this.schemaName = schemaName; 124 this.tableName = tableName; 125 } 126 127 public SpaceTable(String tableName) 128 { 129 this.tableName = tableName; 130 } 131 132 private void getConglomInfo(LanguageConnectionContext lcc) 133 throws StandardException 134 { 135 DataDictionary dd = lcc.getDataDictionary(); 136 137 if (schemaName == null) 138 schemaName = lcc.getCurrentSchemaName(); 139 140 SchemaDescriptor sd = dd.getSchemaDescriptor(schemaName, lcc.getTransactionExecute(), true); 142 TableDescriptor td = dd.getTableDescriptor(tableName,sd); 143 if (td == null) { 145 conglomTable = new ConglomInfo[0]; return; 147 } 148 ConglomerateDescriptor[] cds = td.getConglomerateDescriptors(); 149 conglomTable = new ConglomInfo[cds.length]; 151 for (int i = 0; i < cds.length; i++) 152 conglomTable[i] = new ConglomInfo( 153 cds[i].getConglomerateNumber(), 154 cds[i].isIndex() ? cds[i].getConglomerateName() : tableName, 155 cds[i].isIndex()); 156 } 157 158 159 private void getSpaceInfo(int index) 160 throws StandardException 161 { 162 ConglomerateController cc = tc.openConglomerate( 163 conglomTable[index].getConglomId(), 164 false, 165 0, TransactionController.MODE_RECORD, 167 TransactionController.ISOLATION_READ_COMMITTED 168 ); 169 spaceInfo = cc.getSpaceInfo(); 170 cc.close(); 171 cc = null; 172 } 173 174 175 178 public ResultSetMetaData getMetaData() 179 { 180 return metadata; 181 } 182 183 187 public boolean next() throws SQLException 188 { 189 try 190 { 191 if (!initialized) 192 { 193 LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC(); 194 getConglomInfo(lcc); 195 tc = lcc.getTransactionExecute(); 196 initialized = true; 197 currentRow = -1; 198 } 199 if (conglomTable == null) 200 return false; 201 currentRow++; 202 if (currentRow >= conglomTable.length) 203 return false; 204 spaceInfo = null; 205 getSpaceInfo(currentRow); 206 return true; 207 } 208 catch (StandardException se) 209 { 210 throw PublicAPI.wrapStandardException(se); 211 } 212 } 213 214 215 218 public void close() 219 { 220 conglomTable = null; 221 spaceInfo = null; 222 tc = null; 223 } 224 225 228 public String getString(int columnNumber) 229 { 230 ConglomInfo conglomInfo = conglomTable[currentRow]; 231 String str = conglomInfo.getConglomName(); 232 wasNull = (str == null); 233 return str; 234 } 235 236 239 public long getLong(int columnNumber) 240 { 241 long longval; 242 ConglomInfo conglomInfo = conglomTable[currentRow]; 243 switch(columnNumber) 244 { 245 case 3: 246 longval = spaceInfo.getNumAllocatedPages(); 247 break; 248 case 4: 249 longval = spaceInfo.getNumFreePages(); 250 break; 251 case 5: 252 longval = spaceInfo.getNumUnfilledPages(); 253 break; 254 case 7: 255 int psize = spaceInfo.getPageSize(); 256 longval = (spaceInfo.getNumFreePages() * psize); 257 break; 260 default: 261 longval = -1; 262 } 263 wasNull = false; 264 if (SanityManager.DEBUG) 265 if (longval < 0) 266 SanityManager.THROWASSERT("SpaceTable column number " + columnNumber + 267 " has a negative value at row " + currentRow); 268 return longval; 269 } 270 271 274 public short getShort(int columnNumber) 275 { 276 ConglomInfo conglomInfo = conglomTable[currentRow]; 277 wasNull = false; 278 return (short) (conglomInfo.getIsIndex() ? 1 : 0); 279 } 280 281 282 285 public int getInt(int columnNumber) 286 { 287 return spaceInfo.getPageSize(); 288 } 289 290 291 294 public boolean wasNull() 295 { 296 return wasNull; 297 } 298 299 300 301 302 305 public double getEstimatedRowCount(VTIEnvironment vtiEnvironment) 306 { 307 return VTICosting.defaultEstimatedRowCount; 308 } 309 310 313 public double getEstimatedCostPerInstantiation(VTIEnvironment vtiEnvironment) 314 { 315 return VTICosting.defaultEstimatedCost; 316 } 317 318 322 public boolean supportsMultipleInstantiations(VTIEnvironment vtiEnvironment) 323 { 324 return true; 325 } 326 327 330 private static final ResultColumnDescriptor[] columnInfo = { 331 332 EmbedResultSetMetaData.getResultColumnDescriptor("CONGLOMERATENAME", Types.VARCHAR, true, 128), 333 EmbedResultSetMetaData.getResultColumnDescriptor("ISINDEX", Types.SMALLINT, false), 334 EmbedResultSetMetaData.getResultColumnDescriptor("NUMALLOCATEDPAGES", Types.BIGINT, false), 335 EmbedResultSetMetaData.getResultColumnDescriptor("NUMFREEPAGES", Types.BIGINT, false), 336 EmbedResultSetMetaData.getResultColumnDescriptor("NUMUNFILLEDPAGES", Types.BIGINT, false), 337 EmbedResultSetMetaData.getResultColumnDescriptor("PAGESIZE", Types.INTEGER, false), 338 EmbedResultSetMetaData.getResultColumnDescriptor("ESTIMSPACESAVING", Types.BIGINT, false), 339 }; 340 341 private static final ResultSetMetaData metadata = new EmbedResultSetMetaData(columnInfo); 342 343 } 344 345 class ConglomInfo 346 { 347 private long conglomId; 348 private String conglomName; 349 private boolean isIndex; 350 351 public ConglomInfo(long conglomId, String conglomName, boolean isIndex) 352 { 353 this.conglomId = conglomId; 354 this.conglomName = conglomName; 355 this.isIndex = isIndex; 356 } 357 358 public long getConglomId() 359 { 360 return conglomId; 361 } 362 363 public String getConglomName() 364 { 365 return conglomName; 366 } 367 368 public boolean getIsIndex() 369 { 370 return isIndex; 371 } 372 } 373 374 375 376 377 | Popular Tags |