KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > ConvertUtils


1 /**
2  * com.mckoi.database.ConvertUtils 04 Oct 2001
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.io.*;
28 import java.util.ArrayList JavaDoc;
29 import com.mckoi.debug.DebugLogger;
30 import com.mckoi.util.ByteArrayUtil;
31 import com.mckoi.util.IntegerListInterface;
32
33 /**
34  * Various static database convertion tools for converting for upgrading
35  * parts of the database.
36  *
37  * @author Tobias Downer
38  */

39
40 class ConvertUtils {
41
42
43
44   /**
45    * Upgrades an .ijf index file to an .iid IndexStore. With version 0.92
46    * of the database we introduced a specialized scalable IndexStore for
47    * storing all indexing information.
48    * <p>
49    * Returns an list of MasterTableJournal that contains any journal entries
50    * that are pending to be made to the table.
51    */

52   static ArrayList JavaDoc convertIndexFiles1(File original_ijf,
53                IndexStore new_store, DataTableDef table_def,
54                DebugLogger logger) throws IOException {
55
56     int column_count = table_def.columnCount();
57
58     // Open the old ijf file
59
FixedSizeDataStore ijf =
60                      new FixedSizeDataStore(original_ijf, -1, false, logger);
61     ijf.open(false);
62
63     int block_size = 1024;
64     if (table_def.getTableName().getSchema().equals("SYS_INFO")) {
65       block_size = 128;
66     }
67
68     // Create and initialize the new index store
69
new_store.create(block_size);
70     new_store.init();
71     new_store.addIndexLists(column_count + 1, (byte) 1);
72     new_store.flush();
73     IndexSet index_set = new_store.getSnapshotIndexSet();
74
75     // Load the index header.
76
int header_size = 8 + 4 + 4 + (column_count * 4);
77     byte[] index_header_data = new byte[header_size];
78     byte[] reserved_header = new byte[64];
79     ijf.readReservedBuffer(reserved_header, 0, 64);
80     // Get the current 'unique_id' value.
81
long unique_id = ByteArrayUtil.getLong(reserved_header, 8);
82     int cur_header_sector = ByteArrayUtil.getInt(reserved_header, 0);
83     ijf.readAcross(cur_header_sector,
84                    index_header_data, 0, index_header_data.length);
85     // 'index_header_data' will now contain the header format.
86

87     // ---
88

89     // Convert the master index first,
90
// Where is the information in the header file?
91
int mast_index_sector = ByteArrayUtil.getInt(index_header_data, 8);
92     InputStream sin = ijf.getSectorInputStream(mast_index_sector);
93     DataInputStream din = new DataInputStream(sin);
94
95     int ver = din.readInt(); // The version.
96
if (ver != 1) {
97       throw new IOException("Unrecognised master index list version.");
98     }
99
100     // The master index is always at 0.
101
IntegerListInterface master_index = index_set.getIndex(0);
102     int entries_count = din.readInt();
103     int previous = -1;
104     for (int i = 0; i < entries_count; ++i) {
105       int entry = din.readInt();
106       if (entry == previous) {
107         throw new IOException("Master index format corrupt - double entry.");
108       }
109       else if (entry < previous) {
110         throw new IOException("Master index format corrupt - not sorted.");
111       }
112       master_index.add(entry);
113     }
114
115     // Close the stream
116
din.close();
117
118     // ---
119

120     // Any journal modifications
121
// Where is the information in the header file?
122
int journal_sector = ByteArrayUtil.getInt(index_header_data, 12);
123     sin = ijf.getSectorInputStream(journal_sector);
124     din = new DataInputStream(sin);
125
126     ver = din.readInt(); // The version.
127
if (ver != 1) {
128       throw new Error JavaDoc("Unrecognised journals list version.");
129     }
130
131     ArrayList JavaDoc transaction_mod_list = new ArrayList JavaDoc();
132     int num_journals = din.readInt();
133     for (int i = 0; i < num_journals; ++i) {
134       MasterTableJournal journal = new MasterTableJournal();
135       journal.readFrom(din);
136       transaction_mod_list.add(journal);
137     }
138
139     // Close the stream
140
din.close();
141
142     // ---
143

144     // Convert the indices for each column
145
// This is the new made up list of indices
146
IntegerListInterface[] column_indices =
147                                       new IntegerListInterface[column_count];
148
149     // For each column
150
for (int column = 0; column < column_count; ++column) {
151       // First check this is an indexable type.
152
if (table_def.columnAt(column).isIndexableType()) {
153         // Where is the information in the header file?
154
int scheme_sector = ByteArrayUtil.getInt(index_header_data,
155                                                  16 + (column * 4));
156
157         sin = ijf.getSectorInputStream(scheme_sector);
158         din = new DataInputStream(sin);
159
160         // Read the type of scheme for this column (1=Insert, 2=Blind).
161
byte t = (byte) din.read();
162
163         if (t == 1) {
164           // The index list for the given column
165
IntegerListInterface col_index = index_set.getIndex(column + 1);
166           column_indices[column] = col_index;
167
168           // Read from the input and output to the list.
169
int vec_size = din.readInt();
170           for (int i = 0; i < vec_size; ++i) {
171             int row = din.readInt();
172             col_index.add(row);
173           }
174
175         }
176         else {
177           // Ignore otherwise
178
}
179
180         // Close the stream
181
din.close();
182
183       } // If column is indexable
184

185     } // for each column
186

187     // ---
188

189     // Commit the new index store changes
190
new_store.commitIndexSet(index_set);
191     // Dispose of the set
192
index_set.dispose();
193     // Set the unique id
194
new_store.setUniqueID(unique_id);
195     // Flush the changes and synchronize with the file system.
196
new_store.flush();
197     new_store.hardSynch();
198
199     // Close and delete the old ijf file
200
ijf.close();
201     ijf.delete();
202
203     // Return the list of MasterTableJournal
204
return transaction_mod_list;
205
206   }
207
208
209 }
210
Popular Tags