KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > access > conglomerate > ConglomerateUtil


1 /*
2
3    Derby - Class org.apache.derby.impl.store.access.conglomerate.ConglomerateUtil
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.conglomerate;
23
24 import org.apache.derby.iapi.reference.Property;
25
26 import org.apache.derby.iapi.services.sanity.SanityManager;
27 import org.apache.derby.iapi.services.io.Formatable;
28 import org.apache.derby.iapi.services.io.FormatIdUtil;
29
30 import org.apache.derby.iapi.error.StandardException;
31
32 import org.apache.derby.iapi.store.access.Qualifier;
33 import org.apache.derby.iapi.store.access.RowUtil;
34 import org.apache.derby.iapi.store.access.TransactionController;
35 import org.apache.derby.iapi.store.raw.ContainerHandle;
36 import org.apache.derby.iapi.store.raw.FetchDescriptor;
37 import org.apache.derby.iapi.store.raw.Page;
38 import org.apache.derby.iapi.store.raw.RawStoreFactory;
39 import org.apache.derby.iapi.store.raw.RecordHandle;
40
41 import org.apache.derby.iapi.types.DataValueDescriptor;
42
43 import org.apache.derby.iapi.services.io.FormatableBitSet;
44
45 import java.io.IOException JavaDoc;
46 import java.io.ObjectInput JavaDoc;
47 import java.io.ObjectOutput JavaDoc;
48
49 import java.util.Hashtable JavaDoc;
50 import java.util.Properties JavaDoc;
51
52 /**
53  * Static utility routine package for all Conglomerates.
54  * <p>
55  * A collection of static utility routines that are shared by multiple
56  * Conglomerate implementations.
57  * <p>
58  **/

59 public final class ConglomerateUtil
60 {
61
62     /* Public Methods of This class: (arranged Alphabetically ) */
63
64     /**
65      * Create a list of all the properties that Access wants to export
66      * through the getInternalTablePropertySet() call.
67      * <p>
68      * This utility routine creates a list of properties that are shared by
69      * all conglomerates. This list contains the following:
70      *
71      * derby.storage.initialPages
72      * derby.storage.minimumRecordSize
73      * derby.storage.pageReservedSpace
74      * derby.storage.pageSize
75      * derby.storage.reusableRecordId
76      *
77      * <p>
78      *
79      * @return The Property set filled in.
80      *
81      * @param prop If non-null the property set to fill in.
82      **/

83     public static Properties JavaDoc createRawStorePropertySet(
84     Properties JavaDoc prop)
85     {
86         prop = createUserRawStorePropertySet(prop);
87
88         prop.put(RawStoreFactory.PAGE_REUSABLE_RECORD_ID, "");
89
90         return(prop);
91     }
92
93     /**
94      * Create a list of all the properties that Access wants to export
95      * through the getInternalTablePropertySet() call.
96      * <p>
97      * This utility routine creates a list of properties that are shared by
98      * all conglomerates. This list contains the following:
99      *
100      * derby.storage.initialPages
101      * derby.storage.minimumRecordSize
102      * derby.storage.pageReservedSpace
103      * derby.storage.pageSize
104      *
105      * <p>
106      *
107      * @return The Property set filled in.
108      *
109      * @param prop If non-null the property set to fill in.
110      **/

111     public static Properties JavaDoc createUserRawStorePropertySet(
112     Properties JavaDoc prop)
113     {
114         if (prop == null)
115             prop = new Properties JavaDoc();
116
117         prop.put(Property.PAGE_SIZE_PARAMETER, "");
118         prop.put(RawStoreFactory.MINIMUM_RECORD_SIZE_PARAMETER, "");
119         prop.put(RawStoreFactory.PAGE_RESERVED_SPACE_PARAMETER, "");
120         prop.put(RawStoreFactory.CONTAINER_INITIAL_PAGES, "");
121
122         return(prop);
123     }
124
125
126     /**
127      * Given an array of objects, return an array of format id's.
128      * <p>
129      *
130      * @return An array of format id's describing the input array of objects.
131      *
132      * @param template a row.
133      *
134      **/

135     public static int[] createFormatIds(
136     DataValueDescriptor[] template)
137     {
138
139         // get format id's from each column in template
140
// conglomerate state.
141

142         int[] format_ids = new int[template.length];
143
144         for (int i = 0; i < template.length; i++)
145         {
146             if (SanityManager.DEBUG)
147             {
148                 if (template[i] == null)
149                 {
150                     SanityManager.THROWASSERT("row template is null for "+
151                             "column["+i+"].");
152                 }
153                 if (!(template[i] instanceof Formatable))
154                 {
155                     SanityManager.THROWASSERT("row template is not formatable "+
156                             "column["+i+"]. Type is "+template[i].getClass().getName());
157                 }
158             }
159
160             format_ids[i] = ((Formatable) template[i]).getTypeFormatId();
161         }
162
163         return(format_ids);
164     }
165
166     /**
167      * Read a format id array in from a stream.
168      * <p>
169      *
170      * @return A new array of format id's.
171      *
172      * @param num The number of format ids to read.
173      * @param in The stream to read the array of format id's from.
174      *
175      * @exception IOException Thown on read error.
176      **/

177     public static int[] readFormatIdArray(
178     int num,
179     ObjectInput JavaDoc in)
180         throws IOException JavaDoc
181     {
182         // read in the array of format id's
183

184         int[] format_ids = new int[num];
185         for (int i = 0; i < num; i++)
186         {
187             format_ids[i] = FormatIdUtil.readFormatIdInteger(in);
188         }
189
190         return(format_ids);
191     }
192
193     /**
194      * Write a format id array to a stream.
195      * <p>
196      *
197      * @param format_id_array The number of format ids to read.
198      * @param out The stream to write the array of format id's to.
199      *
200      * @exception IOException Thown on write error.
201      **/

202     public static void writeFormatIdArray(
203     int[] format_id_array,
204     ObjectOutput JavaDoc out)
205         throws IOException JavaDoc
206     {
207         for (int i = 0; i < format_id_array.length; i++)
208         {
209             FormatIdUtil.writeFormatIdInteger(out, format_id_array[i]);
210         }
211     }
212
213     /**
214      ** Format a page of data, as access see's it.
215      **/

216
217     public static String JavaDoc debugPage(
218     Page page,
219     int start_slot,
220     boolean full_rh,
221     DataValueDescriptor[] template)
222     {
223         if (SanityManager.DEBUG)
224         {
225             StringBuffer JavaDoc string = new StringBuffer JavaDoc(4096);
226
227             string.append("PAGE:(");
228             string.append(page.getPageNumber());
229             string.append(")------------------------------------------:\n");
230
231             try
232             {
233                 if (page != null)
234                 {
235                     int numrows = page.recordCount();
236
237                     for (int slot_no = start_slot; slot_no < numrows; slot_no++)
238                     {
239                         RecordHandle rh =
240                             page.fetchFromSlot(
241                                (RecordHandle) null, slot_no, template,
242                                (FetchDescriptor) null,
243                                true);
244
245                         // pre-pend either "D:" if deleted, or " :" if not.
246
string.append(
247                             page.isDeletedAtSlot(slot_no) ? "D:" : " :");
248
249                         // row[slot,id]:
250
string.append("row[");
251                         string.append(slot_no);
252                         string.append("](id:");
253                         string.append(rh.getId());
254                         string.append("):\t");
255
256                         // long record handle:
257
// Record id=78 Page(31,Container(0, 919707766934))
258
if (full_rh)
259                         {
260                             string.append("[");
261                             string.append(rh.toString());
262                             string.append("]:");
263                         }
264
265                         // row:
266
string.append(RowUtil.toString(template));
267                         string.append("\n");
268                     }
269
270                     // string.append(page.toString());
271
}
272             }
273             catch (Throwable JavaDoc t)
274             {
275                 string.append("Error encountered while building string");
276             }
277
278             return(string.toString());
279         }
280         else
281         {
282             return(null);
283         }
284     }
285 }
286
Popular Tags