KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > DropIndexConstantAction


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.DropIndexConstantAction
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.sql.execute;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25 import org.apache.derby.iapi.error.StandardException;
26 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
27
28 import org.apache.derby.iapi.sql.depend.Dependency;
29 import org.apache.derby.iapi.sql.depend.Dependent;
30
31 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
32 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
33 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
34 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
35 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
36
37 import org.apache.derby.iapi.sql.depend.DependencyManager;
38 import org.apache.derby.iapi.reference.SQLState;
39 import org.apache.derby.iapi.sql.execute.ConstantAction;
40
41 import org.apache.derby.iapi.sql.Activation;
42
43 import org.apache.derby.iapi.store.access.TransactionController;
44
45 import org.apache.derby.catalog.UUID;
46
47 import java.util.Enumeration JavaDoc;
48
49
50 /**
51  * This class describes actions that are ALWAYS performed for a
52  * DROP INDEX Statement at Execution time.
53  *
54  * @author Jeff Lichtman Cribbed from DropTableConstantAction
55  */

56
57 class DropIndexConstantAction extends IndexConstantAction
58 {
59
60     private String JavaDoc fullIndexName;
61     private long tableConglomerateId;
62
63
64     // CONSTRUCTORS
65

66     /**
67      * Make the ConstantAction for a DROP INDEX statement.
68      *
69      *
70      * @param fullIndexName Fully qualified index name
71      * @param indexName Index name.
72      * @param tableName The table name
73      * @param schemaName Schema that index lives in.
74      * @param tableId UUID for table
75      * @param tableConglomerateId heap Conglomerate Id for table
76      *
77      */

78     DropIndexConstantAction(
79                                 String JavaDoc fullIndexName,
80                                 String JavaDoc indexName,
81                                 String JavaDoc tableName,
82                                 String JavaDoc schemaName,
83                                 UUID tableId,
84                                 long tableConglomerateId)
85     {
86         super(tableId, indexName, tableName, schemaName);
87         this.fullIndexName = fullIndexName;
88         this.tableConglomerateId = tableConglomerateId;
89     }
90
91     // OBJECT METHODS
92

93     public String JavaDoc toString()
94     {
95         // Do not put this under SanityManager.DEBUG - it is needed for
96
// error reporting.
97
return "DROP INDEX " + fullIndexName;
98     }
99
100     // INTERFACE METHODS
101

102
103     /**
104      * This is the guts of the Execution-time logic for DROP INDEX.
105      *
106      *
107      * @exception StandardException Thrown on failure
108      */

109     public void executeConstantAction(Activation activation)
110                         throws StandardException
111     {
112         TableDescriptor td;
113         ConglomerateDescriptor cd;
114
115         LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
116         DataDictionary dd = lcc.getDataDictionary();
117         DependencyManager dm = dd.getDependencyManager();
118         TransactionController tc = lcc.getTransactionExecute();
119
120         /*
121         ** Inform the data dictionary that we are about to write to it.
122         ** There are several calls to data dictionary "get" methods here
123         ** that might be done in "read" mode in the data dictionary, but
124         ** it seemed safer to do this whole operation in "write" mode.
125         **
126         ** We tell the data dictionary we're done writing at the end of
127         ** the transaction.
128         */

129         dd.startWriting(lcc);
130
131         // need to lock heap in exclusive mode first. Because we can't first
132
// shared lock the row in SYSCONGLOMERATES and later exclusively lock
133
// it, this is potential deadlock (track 879). Also td need to be
134
// gotten after we get the lock, a concurrent thread could be modifying
135
// table shape (track 3804, 3825)
136

137         // older version (or target) has to get td first, potential deadlock
138
if (tableConglomerateId == 0)
139         {
140             td = dd.getTableDescriptor(tableId);
141             if (td == null)
142             {
143                 throw StandardException.newException(
144                     SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, tableName);
145             }
146             tableConglomerateId = td.getHeapConglomerateId();
147         }
148         lockTableForDDL(tc, tableConglomerateId, true);
149
150         td = dd.getTableDescriptor(tableId);
151         if (td == null)
152         {
153             throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, tableName);
154         }
155
156         /*
157         ** If the schema descriptor is null, then
158         ** we must have just read ourselves in.
159         ** So we will get the corresponding schema
160         ** descriptor from the data dictionary.
161         */

162         SchemaDescriptor sd = dd.getSchemaDescriptor(schemaName, tc, true) ;
163
164         /* Get the conglomerate descriptor for the index, along
165          * with an exclusive row lock on the row in sys.sysconglomerates
166          * in order to ensure that no one else compiles against the
167          * index.
168          */

169         cd = dd.getConglomerateDescriptor(indexName, sd, true);
170
171         if (cd == null)
172         {
173             throw StandardException.newException(SQLState.LANG_INDEX_NOT_FOUND_DURING_EXECUTION, fullIndexName);
174         }
175
176         /* Prepare all dependents to invalidate. (This is there chance
177          * to say that they can't be invalidated.)
178          * We check for invalidation before we drop the conglomerate descriptor
179          * since the conglomerate descriptor may be looked up as part of
180          * decoding tuples in SYSDEPENDS.
181          */

182         dropIndex(dm, dd, tc, cd, td, activation.getLanguageConnectionContext());
183     }
184
185     public static void dropIndex(DependencyManager dm,
186                             DataDictionary dd,
187                             TransactionController tc,
188                             ConglomerateDescriptor cd,
189                             TableDescriptor td,
190                             LanguageConnectionContext lcc)
191         throws StandardException
192     {
193         if (SanityManager.DEBUG)
194         {
195             SanityManager.ASSERT(tc != null, "tc is null");
196             SanityManager.ASSERT(cd != null, "cd is null");
197         }
198
199         // only drop the conglomerate if no similar index but with different
200
// name. Get from dd in case we drop other dup indexes with a cascade operation
201

202         if (dd.getConglomerateDescriptors(cd.getConglomerateNumber()).length == 1)
203         {
204             /* Drop statistics */
205             dd.dropStatisticsDescriptors(td.getUUID(), cd.getUUID(), tc);
206
207             /* Drop the conglomerate */
208             tc.dropConglomerate(cd.getConglomerateNumber());
209         }
210
211         // invalidate any prepared statements that
212
// depended on the index (including this one)
213
dm.invalidateFor(cd, DependencyManager.DROP_INDEX, lcc);
214
215         /* Drop the conglomerate descriptor */
216         dd.dropConglomerateDescriptor(cd, tc);
217
218         /*
219         ** Remove the conglomerate descriptor from the list hanging off of the
220         ** table descriptor
221         */

222         td.removeConglomerateDescriptor(cd);
223     }
224
225 }
226
Popular Tags