001: /*
002:
003: Derby - Class org.apache.derby.impl.services.locks.TableNameInfo
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.services.locks;
023:
024: import org.apache.derby.iapi.services.sanity.SanityManager;
025:
026: import org.apache.derby.iapi.error.StandardException;
027:
028: import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
029: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
030: import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
031: import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
032:
033: import org.apache.derby.iapi.store.access.TransactionController;
034:
035: import java.util.Hashtable;
036:
037: public class TableNameInfo {
038:
039: // things to look up table name etc
040: private DataDictionary dd;
041: private Hashtable ddCache; // conglomId -> conglomerateDescriptor
042: private Hashtable tdCache; // tableID UUID -> table descriptor
043: private Hashtable tableCache; // conglomId -> table descriptor
044: private Hashtable indexCache; // conglomId -> indexname
045:
046: public TableNameInfo(LanguageConnectionContext lcc, boolean andIndex)
047: throws StandardException {
048:
049: tableCache = new Hashtable(31);
050: if (andIndex)
051: indexCache = new Hashtable(13);
052:
053: TransactionController tc = lcc.getTransactionExecute();
054:
055: dd = lcc.getDataDictionary();
056: ddCache = dd.hashAllConglomerateDescriptorsByNumber(tc);
057: tdCache = dd.hashAllTableDescriptorsByTableId(tc);
058: }
059:
060: public String getTableName(Long conglomId) {
061: if (conglomId == null)
062: return "?";
063:
064: // see if we have already seen this conglomerate
065: TableDescriptor td = (TableDescriptor) tableCache
066: .get(conglomId);
067: if (td == null) {
068: // first time we see this conglomerate, get it from the
069: // ddCache
070: ConglomerateDescriptor cd = (ConglomerateDescriptor) ddCache
071: .get(conglomId);
072:
073: if (cd != null) {
074: // conglomerateDescriptor is not null, this table is known
075: // to the data dictionary
076:
077: td = (TableDescriptor) tdCache.get(cd.getTableID());
078: }
079:
080: if ((cd == null) || (td == null)) {
081: String name;
082:
083: // this table is not know to the data dictionary. This
084: // can be caused by one of two reasons:
085: // 1. the table has just been dropped
086: // 2. the table is an internal one that lives below
087: // the data dictionary
088: if (conglomId.longValue() > 20) {
089: // table probably dropped!
090: name = "*** TRANSIENT_" + conglomId;
091: } else {
092: // I am hoping here that we won't create more than
093: // 20 tables before starting the data dictionary!
094:
095: // one of the internal tables -- HACK!!
096: switch (conglomId.intValue()) {
097: case 0:
098: name = "*** INVALID CONGLOMERATE ***";
099: break;
100:
101: case 1:
102: name = "ConglomerateDirectory";
103: break;
104:
105: case 2:
106: name = "PropertyConglomerate";
107: break;
108:
109: default:
110: name = "*** INTERNAL TABLE " + conglomId;
111: break;
112: }
113: }
114:
115: return name;
116: }
117:
118: tableCache.put(conglomId, td);
119:
120: if ((indexCache != null) && cd.isIndex())
121: indexCache.put(conglomId, cd.getConglomerateName());
122: }
123:
124: return td.getName();
125: }
126:
127: public String getTableType(Long conglomId) {
128: if (conglomId == null)
129: return "?";
130:
131: String type;
132:
133: TableDescriptor td = (TableDescriptor) tableCache
134: .get(conglomId);
135: if (td != null) {
136: switch (td.getTableType()) {
137: case TableDescriptor.BASE_TABLE_TYPE:
138: type = "T";
139: break;
140:
141: case TableDescriptor.SYSTEM_TABLE_TYPE:
142: type = "S";
143: break;
144:
145: default:
146: if (SanityManager.DEBUG)
147: SanityManager.THROWASSERT("Illegal table type "
148: + td.getName() + " " + td.getTableType());
149: type = "?";
150: break;
151: }
152: } else if (conglomId.longValue() > 20) {
153: type = "T";
154: } else {
155: type = "S";
156: }
157:
158: return type;
159: }
160:
161: public String getIndexName(Long conglomId) {
162: if (conglomId == null)
163: return "?";
164: return (String) indexCache.get(conglomId);
165: }
166: }
|