001: /*
002:
003: Derby - Class org.apache.derby.impl.store.access.conglomerate.ConglomerateUtil
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.store.access.conglomerate;
023:
024: import org.apache.derby.iapi.reference.Property;
025:
026: import org.apache.derby.iapi.services.sanity.SanityManager;
027: import org.apache.derby.iapi.services.io.Formatable;
028: import org.apache.derby.iapi.services.io.FormatIdUtil;
029:
030: import org.apache.derby.iapi.error.StandardException;
031:
032: import org.apache.derby.iapi.store.access.Qualifier;
033: import org.apache.derby.iapi.store.access.RowUtil;
034: import org.apache.derby.iapi.store.access.TransactionController;
035: import org.apache.derby.iapi.store.raw.ContainerHandle;
036: import org.apache.derby.iapi.store.raw.FetchDescriptor;
037: import org.apache.derby.iapi.store.raw.Page;
038: import org.apache.derby.iapi.store.raw.RawStoreFactory;
039: import org.apache.derby.iapi.store.raw.RecordHandle;
040:
041: import org.apache.derby.iapi.types.DataValueDescriptor;
042:
043: import org.apache.derby.iapi.services.io.FormatableBitSet;
044:
045: import java.io.IOException;
046: import java.io.ObjectInput;
047: import java.io.ObjectOutput;
048:
049: import java.util.Hashtable;
050: import java.util.Properties;
051:
052: /**
053: * Static utility routine package for all Conglomerates.
054: * <p>
055: * A collection of static utility routines that are shared by multiple
056: * Conglomerate implementations.
057: * <p>
058: **/
059: public final class ConglomerateUtil {
060:
061: /* Public Methods of This class: (arranged Alphabetically ) */
062:
063: /**
064: * Create a list of all the properties that Access wants to export
065: * through the getInternalTablePropertySet() call.
066: * <p>
067: * This utility routine creates a list of properties that are shared by
068: * all conglomerates. This list contains the following:
069: *
070: * derby.storage.initialPages
071: * derby.storage.minimumRecordSize
072: * derby.storage.pageReservedSpace
073: * derby.storage.pageSize
074: * derby.storage.reusableRecordId
075: *
076: * <p>
077: *
078: * @return The Property set filled in.
079: *
080: * @param prop If non-null the property set to fill in.
081: **/
082: public static Properties createRawStorePropertySet(Properties prop) {
083: prop = createUserRawStorePropertySet(prop);
084:
085: prop.put(RawStoreFactory.PAGE_REUSABLE_RECORD_ID, "");
086:
087: return (prop);
088: }
089:
090: /**
091: * Create a list of all the properties that Access wants to export
092: * through the getInternalTablePropertySet() call.
093: * <p>
094: * This utility routine creates a list of properties that are shared by
095: * all conglomerates. This list contains the following:
096: *
097: * derby.storage.initialPages
098: * derby.storage.minimumRecordSize
099: * derby.storage.pageReservedSpace
100: * derby.storage.pageSize
101: *
102: * <p>
103: *
104: * @return The Property set filled in.
105: *
106: * @param prop If non-null the property set to fill in.
107: **/
108: public static Properties createUserRawStorePropertySet(
109: Properties prop) {
110: if (prop == null)
111: prop = new Properties();
112:
113: prop.put(Property.PAGE_SIZE_PARAMETER, "");
114: prop.put(RawStoreFactory.MINIMUM_RECORD_SIZE_PARAMETER, "");
115: prop.put(RawStoreFactory.PAGE_RESERVED_SPACE_PARAMETER, "");
116: prop.put(RawStoreFactory.CONTAINER_INITIAL_PAGES, "");
117:
118: return (prop);
119: }
120:
121: /**
122: * Given an array of objects, return an array of format id's.
123: * <p>
124: *
125: * @return An array of format id's describing the input array of objects.
126: *
127: * @param template a row.
128: *
129: **/
130: public static int[] createFormatIds(DataValueDescriptor[] template) {
131:
132: // get format id's from each column in template
133: // conglomerate state.
134:
135: int[] format_ids = new int[template.length];
136:
137: for (int i = 0; i < template.length; i++) {
138: if (SanityManager.DEBUG) {
139: if (template[i] == null) {
140: SanityManager
141: .THROWASSERT("row template is null for "
142: + "column[" + i + "].");
143: }
144: if (!(template[i] instanceof Formatable)) {
145: SanityManager
146: .THROWASSERT("row template is not formatable "
147: + "column["
148: + i
149: + "]. Type is "
150: + template[i].getClass().getName());
151: }
152: }
153:
154: format_ids[i] = ((Formatable) template[i])
155: .getTypeFormatId();
156: }
157:
158: return (format_ids);
159: }
160:
161: /**
162: * Read a format id array in from a stream.
163: * <p>
164: *
165: * @return A new array of format id's.
166: *
167: * @param num The number of format ids to read.
168: * @param in The stream to read the array of format id's from.
169: *
170: * @exception IOException Thown on read error.
171: **/
172: public static int[] readFormatIdArray(int num, ObjectInput in)
173: throws IOException {
174: // read in the array of format id's
175:
176: int[] format_ids = new int[num];
177: for (int i = 0; i < num; i++) {
178: format_ids[i] = FormatIdUtil.readFormatIdInteger(in);
179: }
180:
181: return (format_ids);
182: }
183:
184: /**
185: * Write a format id array to a stream.
186: * <p>
187: *
188: * @param format_id_array The number of format ids to read.
189: * @param out The stream to write the array of format id's to.
190: *
191: * @exception IOException Thown on write error.
192: **/
193: public static void writeFormatIdArray(int[] format_id_array,
194: ObjectOutput out) throws IOException {
195: for (int i = 0; i < format_id_array.length; i++) {
196: FormatIdUtil.writeFormatIdInteger(out, format_id_array[i]);
197: }
198: }
199:
200: /**
201: ** Format a page of data, as access see's it.
202: **/
203:
204: public static String debugPage(Page page, int start_slot,
205: boolean full_rh, DataValueDescriptor[] template) {
206: if (SanityManager.DEBUG) {
207: StringBuffer string = new StringBuffer(4096);
208:
209: string.append("PAGE:(");
210: string.append(page.getPageNumber());
211: string
212: .append(")------------------------------------------:\n");
213:
214: try {
215: if (page != null) {
216: int numrows = page.recordCount();
217:
218: for (int slot_no = start_slot; slot_no < numrows; slot_no++) {
219: RecordHandle rh = page.fetchFromSlot(
220: (RecordHandle) null, slot_no, template,
221: (FetchDescriptor) null, true);
222:
223: // pre-pend either "D:" if deleted, or " :" if not.
224: string
225: .append(page.isDeletedAtSlot(slot_no) ? "D:"
226: : " :");
227:
228: // row[slot,id]:
229: string.append("row[");
230: string.append(slot_no);
231: string.append("](id:");
232: string.append(rh.getId());
233: string.append("):\t");
234:
235: // long record handle:
236: // Record id=78 Page(31,Container(0, 919707766934))
237: if (full_rh) {
238: string.append("[");
239: string.append(rh.toString());
240: string.append("]:");
241: }
242:
243: // row:
244: string.append(RowUtil.toString(template));
245: string.append("\n");
246: }
247:
248: // string.append(page.toString());
249: }
250: } catch (Throwable t) {
251: string
252: .append("Error encountered while building string");
253: }
254:
255: return (string.toString());
256: } else {
257: return (null);
258: }
259: }
260: }
|