1 24 25 package com.mckoi.database; 26 27 import java.io.IOException ; 28 import java.io.DataInput ; 29 import java.io.DataOutput ; 30 import java.util.ArrayList ; 31 32 37 38 public class DataIndexSetDef { 39 40 43 private TableName table_name; 44 45 48 private ArrayList index_list; 49 50 53 private boolean immutable; 54 55 58 public DataIndexSetDef(TableName table_name) { 59 this.table_name = table_name; 60 index_list = new ArrayList (); 61 immutable = false; 62 } 63 64 public DataIndexSetDef(DataIndexSetDef def) { 65 this.table_name = def.table_name; 66 index_list = new ArrayList (); 67 for (int i = 0; i < def.indexCount(); ++i) { 68 index_list.add(new DataIndexDef(def.indexAt(i))); 69 } 70 immutable = false; 71 } 72 73 76 public void setImmutable() { 77 this.immutable = true; 78 } 79 80 83 public void addDataIndexDef(DataIndexDef def) { 84 if (!immutable) { 85 index_list.add(def); 86 } 87 else { 88 throw new RuntimeException ("Tried to add index to immutable def."); 89 } 90 } 91 92 95 public void removeDataIndexDef(int i) { 96 if (!immutable) { 97 index_list.remove(i); 98 } 99 else { 100 throw new RuntimeException ("Tried to add index to immutable def."); 101 } 102 } 103 104 107 public int indexCount() { 108 return index_list.size(); 109 } 110 111 114 public DataIndexDef indexAt(int i) { 115 return (DataIndexDef) index_list.get(i); 116 } 117 118 122 public int findIndexWithName(String index_name) { 123 int sz = indexCount(); 124 for (int i = 0; i < sz; ++i) { 125 if (indexAt(i).getName().equals(index_name)) { 126 return i; 127 } 128 } 129 return -1; 130 } 131 132 136 public int findIndexForColumns(String [] cols) { 137 int sz = indexCount(); 138 for (int i = 0; i < sz; ++i) { 139 String [] t_cols = indexAt(i).getColumnNames(); 140 if (t_cols.length == cols.length) { 141 boolean passed = true; 142 for (int n = 0; n < t_cols.length && passed; ++n) { 143 if (!t_cols[n].equals(cols[n])) { 144 passed = false; 145 } 146 } 147 if (passed) { 148 return i; 149 } 150 } 151 } 152 return -1; 153 } 154 155 159 public DataIndexDef indexWithName(String index_name) { 160 int i = findIndexWithName(index_name); 161 if (i != -1) { 162 return indexAt(i); 163 } 164 else { 165 return null; 166 } 167 } 168 169 174 public String resolveIndexName(String index_name, boolean ignore_case) 175 throws DatabaseException { 176 int sz = indexCount(); 177 String found = null; 178 for (int i = 0; i < sz; ++i) { 179 boolean passed; 180 String cur_index_name = indexAt(i).getName(); 181 if (ignore_case) { 182 passed = cur_index_name.equalsIgnoreCase(index_name); 183 } 184 else { 185 passed = cur_index_name.equals(index_name); 186 } 187 if (passed) { 188 if (found != null) { 189 throw new DatabaseException("Ambigious index name '" + 190 index_name + "'"); 191 } 192 found = cur_index_name; 193 } 194 } 195 if (found == null) { 196 throw new DatabaseException("Index '" + index_name + "' not found."); 197 } 198 return found; 199 } 200 201 204 public void write(DataOutput dout) throws IOException { 205 dout.writeInt(1); 206 dout.writeUTF(table_name.getSchema()); 207 dout.writeUTF(table_name.getName()); 208 dout.writeInt(index_list.size()); 209 for (int i = 0; i < index_list.size(); ++i) { 210 ((DataIndexDef) index_list.get(i)).write(dout); 211 } 212 } 213 214 217 public static DataIndexSetDef read(DataInput din) throws IOException { 218 int version = din.readInt(); 219 if (version != 1) { 220 throw new IOException ("Don't understand version."); 221 } 222 String schema = din.readUTF(); 223 String name = din.readUTF(); 224 int sz = din.readInt(); 225 DataIndexSetDef index_set = 226 new DataIndexSetDef(new TableName(schema, name)); 227 for (int i = 0; i < sz; ++i) { 228 index_set.addDataIndexDef(DataIndexDef.read(din)); 229 } 230 231 return index_set; 232 } 233 234 235 } 236 237 | Popular Tags |