KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > locks > TableNameInfo


1 /*
2
3    Derby - Class org.apache.derby.impl.services.locks.TableNameInfo
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.services.locks;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25
26 import org.apache.derby.iapi.error.StandardException;
27
28 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
29 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
30 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
31 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
32
33 import org.apache.derby.iapi.store.access.TransactionController;
34
35 import java.util.Hashtable JavaDoc;
36
37 public class TableNameInfo {
38
39     // things to look up table name etc
40
private DataDictionary dd;
41     private Hashtable JavaDoc ddCache; // conglomId -> conglomerateDescriptor
42
private Hashtable JavaDoc tdCache; // tableID UUID -> table descriptor
43
private Hashtable JavaDoc tableCache; // conglomId -> table descriptor
44
private Hashtable JavaDoc indexCache; // conglomId -> indexname
45

46     public TableNameInfo(LanguageConnectionContext lcc, boolean andIndex)
47         throws StandardException {
48
49         tableCache = new Hashtable JavaDoc(31);
50         if (andIndex)
51             indexCache = new Hashtable JavaDoc(13);
52
53         TransactionController tc = lcc.getTransactionExecute();
54
55         dd = lcc.getDataDictionary();
56         ddCache = dd.hashAllConglomerateDescriptorsByNumber(tc);
57         tdCache = dd.hashAllTableDescriptorsByTableId(tc);
58     }
59
60
61     public String JavaDoc getTableName(Long JavaDoc conglomId) {
62         if (conglomId == null)
63             return "?";
64
65         // see if we have already seen this conglomerate
66
TableDescriptor td = (TableDescriptor) tableCache.get(conglomId);
67         if (td == null)
68         {
69             // first time we see this conglomerate, get it from the
70
// ddCache
71
ConglomerateDescriptor cd =
72                 (ConglomerateDescriptor)ddCache.get(conglomId);
73
74             if (cd != null)
75             {
76                 // conglomerateDescriptor is not null, this table is known
77
// to the data dictionary
78

79                 td = (TableDescriptor) tdCache.get(cd.getTableID());
80             }
81
82             if ((cd == null) || (td == null))
83             {
84                 String JavaDoc name;
85
86                 // this table is not know to the data dictionary. This
87
// can be caused by one of two reasons:
88
// 1. the table has just been dropped
89
// 2. the table is an internal one that lives below
90
// the data dictionary
91
if (conglomId.longValue() > 20)
92                 {
93                     // table probably dropped!
94
name = "*** TRANSIENT_" + conglomId;
95                 }
96                 else
97                 {
98                     // I am hoping here that we won't create more than
99
// 20 tables before starting the data dictionary!
100

101                     // one of the internal tables -- HACK!!
102
switch (conglomId.intValue())
103                     {
104                     case 0:
105                         name = "*** INVALID CONGLOMERATE ***";
106                         break;
107
108                     case 1:
109                         name = "ConglomerateDirectory";
110                         break;
111
112                     case 2:
113                         name = "PropertyConglomerate";
114                         break;
115
116                     default:
117                         name = "*** INTERNAL TABLE " + conglomId;
118                         break;
119                     }
120                 }
121
122                 return name;
123             }
124
125             tableCache.put(conglomId, td);
126
127             if ((indexCache != null) && cd.isIndex())
128                 indexCache.put(conglomId, cd.getConglomerateName());
129         }
130
131         return td.getName();
132     }
133
134     public String JavaDoc getTableType(Long JavaDoc conglomId) {
135         if (conglomId == null)
136             return "?";
137
138         String JavaDoc type;
139
140         TableDescriptor td = (TableDescriptor) tableCache.get(conglomId);
141         if (td != null)
142         {
143             switch(td.getTableType())
144             {
145             case TableDescriptor.BASE_TABLE_TYPE:
146                 type = "T";
147                 break;
148
149             case TableDescriptor.SYSTEM_TABLE_TYPE:
150                 type = "S";
151                 break;
152
153             default:
154                 if (SanityManager.DEBUG)
155                     SanityManager.THROWASSERT("Illegal table type " +
156                           td.getName() + " " + td.getTableType());
157                 type = "?";
158                 break;
159             }
160         } else if (conglomId.longValue() > 20)
161         {
162             type = "T";
163         } else {
164             type = "S";
165         }
166
167         return type;
168     }
169
170     public String JavaDoc getIndexName(Long JavaDoc conglomId) {
171         if (conglomId == null)
172             return "?";
173         return (String JavaDoc) indexCache.get(conglomId);
174     }
175 }
176
Popular Tags