1 21 22 package org.apache.derby.diag; 23 24 import org.apache.derby.impl.services.locks.TableNameInfo; 26 27 import org.apache.derby.iapi.services.locks.LockFactory; 28 import org.apache.derby.iapi.services.locks.Latch; 29 import org.apache.derby.iapi.services.locks.Lockable; 30 import org.apache.derby.iapi.services.locks.VirtualLockTable; 31 import org.apache.derby.iapi.services.sanity.SanityManager; 32 33 import org.apache.derby.iapi.error.StandardException; 34 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 35 import org.apache.derby.iapi.sql.conn.ConnectionUtil; 36 import org.apache.derby.iapi.sql.conn.LanguageConnectionFactory; 37 import org.apache.derby.iapi.store.access.TransactionController; 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.util.Hashtable ; 44 import java.util.Enumeration ; 45 import java.sql.ResultSetMetaData ; 46 import java.sql.SQLException ; 47 import java.sql.Types ; 48 import org.apache.derby.vti.VTITemplate; 49 import org.apache.derby.vti.VTICosting; 50 import org.apache.derby.vti.VTIEnvironment; 51 52 86 public class LockTable extends VTITemplate implements VTICosting { 87 88 89 public static final int LATCH = VirtualLockTable.LATCH; 90 91 92 public static final int TABLE_AND_ROWLOCK = VirtualLockTable.TABLE_AND_ROWLOCK; 93 94 95 public static final int ALL = VirtualLockTable.ALL; 96 97 100 private TransactionController tc; 101 private LanguageConnectionFactory lcf; 102 private Hashtable currentRow; private Enumeration lockTable; 104 private boolean wasNull; 105 private boolean initialized; 106 private final int flag; 107 private TableNameInfo tabInfo; 108 109 117 public LockTable() 118 { 119 flag = TABLE_AND_ROWLOCK; 120 } 121 122 127 public LockTable(int flag) 128 { 129 this.flag = flag; 130 } 131 132 135 public ResultSetMetaData getMetaData() 136 { 137 return metadata; 138 } 139 140 145 public boolean next() throws SQLException 146 { 147 try 148 { 149 if (!initialized) 150 { 151 LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC(); 152 153 tc = lcc.getTransactionExecute(); 154 LanguageConnectionFactory lcf = lcc.getLanguageConnectionFactory(); 155 LockFactory lf = lcf.getAccessFactory().getLockFactory(); 156 lockTable = lf.makeVirtualLockTable(); 157 initialized = true; 158 tabInfo = new TableNameInfo(lcc, true); 159 } 160 161 currentRow = null; 162 if (lockTable != null) { 163 while (lockTable.hasMoreElements() && (currentRow == null)) { 164 currentRow = dumpLock((Latch) lockTable.nextElement()); 165 } 166 } 167 } 168 catch (StandardException se) 169 { 170 throw PublicAPI.wrapStandardException(se); 171 } 172 173 return (currentRow != null); 174 } 175 176 179 public void close() 180 { 181 lockTable = null; 182 } 183 184 188 public String getString(int columnNumber) 189 { 190 String val = (String )currentRow.get(columnInfo[columnNumber-1].getName()); 191 wasNull = (val == null); 192 193 return val; 194 } 195 196 197 200 public boolean wasNull() 201 { 202 return wasNull; 203 } 204 205 206 207 210 public double getEstimatedRowCount(VTIEnvironment vtiEnvironment) 211 { 212 return VTICosting.defaultEstimatedRowCount; 213 } 214 215 218 public double getEstimatedCostPerInstantiation(VTIEnvironment vtiEnvironment) 219 { 220 return VTICosting.defaultEstimatedCost; 221 } 222 226 public boolean supportsMultipleInstantiations(VTIEnvironment vtiEnvironment) 227 { 228 return false; 229 } 230 231 234 235 238 private Hashtable dumpLock( 239 Latch lock) 240 throws StandardException 241 { 242 Hashtable attributes = new Hashtable (17); 243 Object lock_type = lock.getQualifier(); 244 245 246 249 Lockable lockable = lock.getLockable(); 250 251 if (!lockable.lockAttributes(flag, attributes)) 253 return null; 254 255 if (SanityManager.DEBUG) 258 { 259 SanityManager.ASSERT(attributes.get(VirtualLockTable.LOCKNAME) != null, 260 "lock table can only represent locks that have a LOCKNAME"); 261 262 SanityManager.ASSERT(attributes.get(VirtualLockTable.LOCKTYPE) != null, 263 "lock table can only represent locks that have a LOCKTYPE"); 264 265 if (attributes.get(VirtualLockTable.CONTAINERID) == null && 266 attributes.get(VirtualLockTable.CONGLOMID) == null) 267 SanityManager.THROWASSERT( 268 "lock table can only represent locks that are associated with a container or conglomerate"); 269 } 270 271 if (attributes.get(VirtualLockTable.LOCKNAME) == null || 272 attributes.get(VirtualLockTable.LOCKTYPE) == null) 273 return null; 275 int lockCount = lock.getCount(); 284 String state; 285 if (lockCount != 0) 286 state = "GRANT"; 287 else if (!(lock instanceof org.apache.derby.impl.services.locks.ActiveLock)) 288 return null; 289 else 290 state = "WAIT"; 291 292 Long conglomId = (Long ) attributes.get(VirtualLockTable.CONGLOMID); 293 294 if (conglomId == null) 295 { 296 if (attributes.get(VirtualLockTable.CONTAINERID) == null) 298 return null; 300 Long value = (Long )attributes.get(VirtualLockTable.CONTAINERID); 301 conglomId = new Long (tc.findConglomid(value.longValue())); 302 attributes.put(VirtualLockTable.CONGLOMID, conglomId); 303 } 304 305 attributes.put(VirtualLockTable.LOCKOBJ, lock); 306 attributes.put(VirtualLockTable.XACTID, lock.getCompatabilitySpace().toString()); 307 attributes.put(VirtualLockTable.LOCKMODE, lock_type.toString()); 308 309 attributes.put(VirtualLockTable.LOCKCOUNT, Integer.toString(lockCount)); 310 311 attributes.put(VirtualLockTable.STATE, state); 312 313 String tableName = tabInfo.getTableName(conglomId); 314 315 attributes.put(VirtualLockTable.TABLENAME, tableName); 316 317 String indexName = tabInfo.getIndexName(conglomId); 318 319 if (indexName != null) 320 attributes.put(VirtualLockTable.INDEXNAME, indexName); 321 322 String tableType = tabInfo.getTableType(conglomId); 323 attributes.put(VirtualLockTable.TABLETYPE, tableType); 324 return attributes; 325 326 } 327 328 331 private static final ResultColumnDescriptor[] columnInfo = { 332 333 EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.XACTID, Types.VARCHAR, false, 15), 334 EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.LOCKTYPE, Types.VARCHAR, true, 5), 335 EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.LOCKMODE, Types.VARCHAR, false, 4), 336 EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.TABLENAME, Types.VARCHAR, false, 128), 337 EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.LOCKNAME, Types.VARCHAR, false, 20), 338 EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.STATE, Types.VARCHAR, true, 5), 339 EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.TABLETYPE, Types.VARCHAR, false, 9), 340 EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.LOCKCOUNT, Types.VARCHAR, false, 5), 341 EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.INDEXNAME, Types.VARCHAR, true, 128) 342 }; 343 344 private static final ResultSetMetaData metadata = new EmbedResultSetMetaData(columnInfo); 345 } 346 347 | Popular Tags |