KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.TableDescriptions 28 Jul 2000
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.*;
29
30 /**
31  * An object that is a key part of Database. This object maintains a list
32  * of descriptions of all tables in the database. The list contains
33  * information about the columns in the table and any other misc table options.
34  *
35  * @author Tobias Downer
36  */

37
38 public final class TableDescriptions {
39
40   /**
41    * The filename of the file that describes every table in the database.
42    */

43   private static final String JavaDoc TABLE_DESC_FILE = "MckoiDB.desc";
44
45   /**
46    * The File that contains the table descriptions list.
47    */

48   private File table_desc_file;
49
50   /**
51    * The File we use to temporary store the table descriptions as we save
52    * them.
53    */

54   private File temp_desc_file;
55
56   /**
57    * The backup file for table descriptions.
58    */

59   private File backup_file;
60
61   /**
62    * A hash table that maps from table name to the DataTableDef object that
63    * describes the table.
64    */

65   private HashMap table_descriptions;
66
67   /**
68    * Constructs this object with the database in the given directory.
69    */

70   public TableDescriptions(File database_path) {
71     table_desc_file = new File(database_path, TABLE_DESC_FILE);
72     temp_desc_file = new File(database_path, TABLE_DESC_FILE + ".temp");
73     backup_file = new File(database_path, TABLE_DESC_FILE + ".bak");
74     clear();
75   }
76
77   /**
78    * Returns true if the table descriptions file exists.
79    */

80   public boolean exists() {
81     return table_desc_file.exists() && !table_desc_file.isDirectory();
82   }
83
84   /**
85    * Load the entire list of table descriptions for this database.
86    */

87   public void load() throws IOException {
88
89     // Does the table description file exist?
90
if (table_desc_file.exists()) {
91       // The file exists so load up the table descriptions and put each table
92
// in the table_descriptions map.
93
DataInputStream din = new DataInputStream(
94              new BufferedInputStream(new FileInputStream(table_desc_file)));
95
96       int ver = din.readInt();
97       int table_count = din.readInt();
98       for (int i = 0; i < table_count; ++i) {
99         DataTableDef table_desc = DataTableDef.read(din);
100         String JavaDoc name = table_desc.getName();
101         table_descriptions.put(name, table_desc);
102       }
103
104       din.close();
105     }
106
107   }
108
109   /**
110    * Updates the table description file in the database. The table description
111    * file describes every table in the database. It is loaded when the
112    * database is initialized and refreshed whenever a table alteration occurs
113    * or the database is shut down.
114    */

115   public void save() throws IOException {
116
117     DataOutputStream dout = new DataOutputStream(
118             new BufferedOutputStream(new FileOutputStream(temp_desc_file)));
119
120     dout.writeInt(1);
121     String JavaDoc[] table_list = getTableList();
122     dout.writeInt(table_list.length);
123     for (int i = 0; i < table_list.length; ++i) {
124       // Write the DataTableDef for this table
125
((DataTableDef) table_descriptions.get(table_list[i])).write(dout);
126     }
127
128     dout.flush();
129     dout.close();
130
131     // Delete the current backup file and rename the temp file to the official
132
// file.
133
// Cycle through the backups...
134

135     backup_file.delete();
136     table_desc_file.renameTo(backup_file);
137     temp_desc_file.renameTo(table_desc_file);
138
139   }
140
141   /**
142    * Adds a new DataTableDef object to the list of tables in the database.
143    */

144   void add(DataTableDef table) throws IOException {
145     table_descriptions.put(table.getName(), table);
146   }
147
148   /**
149    * Removes a DataTableDef object from the list with the given name.
150    */

151   void remove(String JavaDoc name) throws IOException {
152     table_descriptions.remove(name);
153   }
154
155   /**
156    * Returns a list of table name's sorted in alphebetical order.
157    */

158   public String JavaDoc[] getTableList() {
159     Set keys = table_descriptions.keySet();
160     String JavaDoc[] all_keys = (String JavaDoc[]) keys.toArray(new String JavaDoc[keys.size()]);
161     Arrays.sort(all_keys);
162     return all_keys;
163   }
164
165   /**
166    * Clears this object completely.
167    */

168   void clear() {
169     table_descriptions = new HashMap(150, 0.50f);
170   }
171
172   /**
173    * Returns the DataTableDef object for the table with the given name. The
174    * description must have been loaded before this method is called. Returns
175    * null if the table was not found.
176    */

177   public DataTableDef getDef(String JavaDoc table_name) {
178     return (DataTableDef) table_descriptions.get(table_name);
179   }
180
181
182 }
183
Popular Tags