1 24 25 package com.mckoi.database; 26 27 import java.io.*; 28 import java.util.*; 29 30 37 38 public final class TableDescriptions { 39 40 43 private static final String TABLE_DESC_FILE = "MckoiDB.desc"; 44 45 48 private File table_desc_file; 49 50 54 private File temp_desc_file; 55 56 59 private File backup_file; 60 61 65 private HashMap table_descriptions; 66 67 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 80 public boolean exists() { 81 return table_desc_file.exists() && !table_desc_file.isDirectory(); 82 } 83 84 87 public void load() throws IOException { 88 89 if (table_desc_file.exists()) { 91 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 name = table_desc.getName(); 101 table_descriptions.put(name, table_desc); 102 } 103 104 din.close(); 105 } 106 107 } 108 109 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 [] table_list = getTableList(); 122 dout.writeInt(table_list.length); 123 for (int i = 0; i < table_list.length; ++i) { 124 ((DataTableDef) table_descriptions.get(table_list[i])).write(dout); 126 } 127 128 dout.flush(); 129 dout.close(); 130 131 135 backup_file.delete(); 136 table_desc_file.renameTo(backup_file); 137 temp_desc_file.renameTo(table_desc_file); 138 139 } 140 141 144 void add(DataTableDef table) throws IOException { 145 table_descriptions.put(table.getName(), table); 146 } 147 148 151 void remove(String name) throws IOException { 152 table_descriptions.remove(name); 153 } 154 155 158 public String [] getTableList() { 159 Set keys = table_descriptions.keySet(); 160 String [] all_keys = (String []) keys.toArray(new String [keys.size()]); 161 Arrays.sort(all_keys); 162 return all_keys; 163 } 164 165 168 void clear() { 169 table_descriptions = new HashMap(150, 0.50f); 170 } 171 172 177 public DataTableDef getDef(String table_name) { 178 return (DataTableDef) table_descriptions.get(table_name); 179 } 180 181 182 } 183 | Popular Tags |