KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > access > btree > index > B2IFactory


1 /*
2
3    Derby - Class org.apache.derby.impl.store.access.btree.index.B2IFactory
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.store.access.btree.index;
23
24 import java.util.Properties JavaDoc;
25
26 import org.apache.derby.iapi.reference.SQLState;
27
28 import org.apache.derby.iapi.services.monitor.ModuleControl;
29 import org.apache.derby.iapi.services.monitor.Monitor;
30 import org.apache.derby.iapi.services.property.PropertyUtil;
31 import org.apache.derby.iapi.services.sanity.SanityManager;
32 import org.apache.derby.iapi.services.io.FormatIdUtil;
33
34 import org.apache.derby.catalog.UUID;
35 import org.apache.derby.iapi.services.uuid.UUIDFactory;
36 import org.apache.derby.iapi.error.StandardException;
37 import org.apache.derby.iapi.store.access.conglomerate.Conglomerate;
38 import org.apache.derby.iapi.store.access.conglomerate.ConglomerateFactory;
39 import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo;
40 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager;
41 import org.apache.derby.iapi.store.access.AccessFactory;
42 import org.apache.derby.iapi.store.access.ColumnOrdering;
43 import org.apache.derby.iapi.store.access.ConglomerateController;
44 import org.apache.derby.iapi.store.access.DynamicCompiledOpenConglomInfo;
45 import org.apache.derby.iapi.store.access.ScanController;
46 import org.apache.derby.iapi.store.access.TransactionController;
47
48 import org.apache.derby.iapi.store.raw.ContainerKey;
49 import org.apache.derby.iapi.store.raw.ContainerHandle;
50 import org.apache.derby.iapi.store.raw.LockingPolicy;
51 import org.apache.derby.iapi.store.raw.Transaction;
52
53 import org.apache.derby.iapi.types.DataValueDescriptor;
54
55 import org.apache.derby.impl.store.access.btree.BTree;
56 import org.apache.derby.impl.store.access.btree.BranchControlRow;
57 import org.apache.derby.impl.store.access.btree.ControlRow;
58 import org.apache.derby.impl.store.access.btree.BTreeController;
59 import org.apache.derby.impl.store.access.btree.LeafControlRow;
60 import org.apache.derby.impl.store.access.btree.OpenBTree;
61
62 /**
63
64   The "B2I" (acronym for b-tree secondary index) factory manages b-tree
65   conglomerates implemented on the raw store which are used as secondary
66   indexes.
67   <p>
68   Most of this code is generic to all conglomerates. This class might be
69   more easily maintained as an abstract class in Raw/Conglomerate/Generic.
70   The concrete ConglomerateFactories would simply have to supply the
71   IMPLEMENTATIONID, FORMATUUIDSTRING, and implement createConglomerate
72   and defaultProperties. Conglomerates which support more than one format
73   would have to override supportsFormat, and conglomerates which support
74   more than one implementation would have to override supportsImplementation.
75
76 **/

77
78 public class B2IFactory implements ConglomerateFactory, ModuleControl
79 {
80
81     private static final String JavaDoc IMPLEMENTATIONID = "BTREE";
82     private static final String JavaDoc FORMATUUIDSTRING = "C6CEEEF0-DAD3-11d0-BB01-0060973F0942";
83     private UUID formatUUID;
84
85
86     /*
87     ** Methods of MethodFactory (via ConglomerateFactory)
88     */

89
90     /**
91     Return the default properties for this kind of conglomerate.
92     @see org.apache.derby.iapi.store.access.conglomerate.MethodFactory#defaultProperties
93     **/

94     public Properties JavaDoc defaultProperties()
95     {
96         // XXX (nat) Need to return the default b-tree secondary index properties.
97
return new Properties JavaDoc();
98     }
99
100     /**
101     Return whether this access method implements the implementation
102     type given in the argument string.
103     The btree only has one implementation type, "BTREE".
104
105     @see org.apache.derby.iapi.store.access.conglomerate.MethodFactory#supportsImplementation
106     **/

107     public boolean supportsImplementation(String JavaDoc implementationId)
108     {
109         return implementationId.equals(IMPLEMENTATIONID);
110     }
111
112     /**
113     Return the primary implementation type for this access method.
114     The btree only has one implementation type, "BTREE".
115
116     @see org.apache.derby.iapi.store.access.conglomerate.MethodFactory#primaryImplementationType
117     **/

118     public String JavaDoc primaryImplementationType()
119     {
120         return IMPLEMENTATIONID;
121     }
122
123     /**
124     Return whether this access method supports the format supplied in
125     the argument.
126     The btree currently only supports one format.
127
128     @see org.apache.derby.iapi.store.access.conglomerate.MethodFactory#supportsFormat
129     **/

130     public boolean supportsFormat(UUID formatid)
131     {
132         return formatid.equals(formatUUID);
133     }
134
135     /**
136     Return the primary format that this access method supports.
137     The btree currently only supports one format.
138
139     @see org.apache.derby.iapi.store.access.conglomerate.MethodFactory#primaryFormat
140     **/

141     public UUID primaryFormat()
142     {
143         return formatUUID;
144     }
145
146     /*
147     ** Methods of ConglomerateFactory
148     */

149
150     /**
151      * Return the conglomerate factory id.
152      * <p>
153      * Return a number in the range of 0-15 which identifies this factory.
154      * Code which names conglomerates depends on this range currently, but
155      * could be easily changed to handle larger ranges. One hex digit seemed
156      * reasonable for the number of conglomerate types being currently
157      * considered (heap, btree, gist, gist btree, gist rtree, hash, others? ).
158      * <p>
159      * @see ConglomerateFactory#getConglomerateFactoryId
160      *
161      * @return an unique identifier used to the factory into the conglomid.
162      **/

163     public int getConglomerateFactoryId()
164     {
165         return(ConglomerateFactory.BTREE_FACTORY_ID);
166     }
167
168     /**
169     Create the conglomerate and return a conglomerate object for it.
170
171     @see ConglomerateFactory#createConglomerate
172
173     @exception StandardException Standard exception policy.
174     **/

175     public Conglomerate createConglomerate(
176     TransactionManager xact_mgr,
177     int segment,
178     long input_containerid,
179     DataValueDescriptor[] template,
180     ColumnOrdering[] columnOrder,
181     Properties JavaDoc properties,
182     int temporaryFlag)
183             throws StandardException
184     {
185         B2I btree = new B2I();
186         btree.create(
187             xact_mgr, segment, input_containerid, template, columnOrder,
188             properties, temporaryFlag);
189
190         return(btree);
191     }
192
193     /**
194      * Return Conglomerate object for conglomerate with conglomid.
195      * <p>
196      * Return the Conglomerate Object. This is implementation specific.
197      * Examples of what will be done is using the id to find the file where
198      * the conglomerate is located, and then executing implementation specific
199      * code to instantiate an object from reading a "special" row from a
200      * known location in the file. In the btree case the btree conglomerate
201      * is stored as a column in the control row on the root page.
202      * <p>
203      * This operation is costly so it is likely an implementation using this
204      * will cache the conglomerate row in memory so that subsequent accesses
205      * need not perform this operation.
206      * <p>
207      * The btree object returned by this routine may be installed in a cache
208      * so the object must not change.
209      *
210      * @return An instance of the conglomerate.
211      *
212      * @exception StandardException Standard exception policy.
213      **/

214     public Conglomerate readConglomerate(
215     TransactionManager xact_manager,
216     ContainerKey container_key)
217         throws StandardException
218     {
219         Conglomerate btree = null;
220         ContainerHandle container = null;
221         ControlRow root = null;
222
223         try
224         {
225             // open readonly, with no locks. Dirty read is ok as it is the
226
// responsibility of client code to make sure this data is not
227
// changing while being read. The only changes that currently
228
// happen to this data is creation and deletion - no updates
229
// ever happen to btree conglomerates.
230
container =
231                 (xact_manager.getRawStoreXact()).openContainer(
232                     container_key,
233                     (LockingPolicy) null,
234                     ContainerHandle.MODE_READONLY);
235
236             if (container == null)
237             {
238                 // thrown a "known" error if the conglomerate does not exist
239
// which is checked for explicitly by callers of the store
240
// interface.
241

242                 throw StandardException.newException(
243                     SQLState.STORE_CONGLOMERATE_DOES_NOT_EXIST,
244                     new Long JavaDoc(container_key.getContainerId()));
245             }
246
247             // The conglomerate is located in the control row on the root page.
248
root = ControlRow.Get(container, BTree.ROOTPAGEID);
249
250             if (SanityManager.DEBUG)
251                 SanityManager.ASSERT(root.getPage().isLatched());
252
253             // read the Conglomerate from it's entry in the control row.
254
btree = (B2I) root.getConglom(B2I.FORMAT_NUMBER);
255
256             if (SanityManager.DEBUG)
257                 SanityManager.ASSERT(btree instanceof B2I);
258         }
259         finally
260         {
261
262             if (root != null)
263                 root.release();
264
265             if (container != null)
266                 container.close();
267         }
268
269         // if any error, just return null - meaning can't access the container.
270

271         return(btree);
272     }
273
274     /*
275     ** Methods of ModuleControl.
276     */

277
278     public boolean canSupport(Properties JavaDoc startParams) {
279
280         String JavaDoc impl = startParams.getProperty("derby.access.Conglomerate.type");
281         if (impl == null)
282             return false;
283
284         return supportsImplementation(impl);
285     }
286
287     public void boot(boolean create, Properties JavaDoc startParams)
288         throws StandardException
289     {
290         // Find the UUID factory.
291
UUIDFactory uuidFactory =
292             Monitor.getMonitor().getUUIDFactory();
293
294         // Make a UUID that identifies this conglomerate's format.
295
formatUUID = uuidFactory.recreateUUID(FORMATUUIDSTRING);
296     }
297
298     public void stop()
299     {
300     }
301 }
302
Popular Tags