1 21 22 package org.apache.derby.catalog.types; 23 24 import org.apache.derby.catalog.IndexDescriptor; 25 26 import org.apache.derby.iapi.services.io.Formatable; 27 import org.apache.derby.iapi.services.io.StoredFormatIds; 28 29 import org.apache.derby.iapi.reference.SQLState; 30 31 import org.apache.derby.iapi.services.io.FormatableHashtable; 32 import org.apache.derby.iapi.services.io.FormatableIntHolder; 33 import org.apache.derby.iapi.sql.dictionary.IndexRowGenerator; 34 35 import java.io.ObjectInput ; 36 import java.io.ObjectOutput ; 37 import java.io.IOException ; 38 39 40 public class IndexDescriptorImpl implements IndexDescriptor, Formatable 41 { 42 55 56 private boolean isUnique; 57 private int[] baseColumnPositions; 58 private boolean[] isAscending; 59 private int numberOfOrderedColumns; 60 private String indexType; 61 62 77 public IndexDescriptorImpl(String indexType, 78 boolean isUnique, 79 int[] baseColumnPositions, 80 boolean[] isAscending, 81 int numberOfOrderedColumns) 82 { 83 this.indexType = indexType; 84 this.isUnique = isUnique; 85 this.baseColumnPositions = baseColumnPositions; 86 this.isAscending = isAscending; 87 this.numberOfOrderedColumns = numberOfOrderedColumns; 88 } 89 90 91 public IndexDescriptorImpl() 92 { 93 } 94 95 96 public boolean isUnique() 97 { 98 return isUnique; 99 } 100 101 102 public int[] baseColumnPositions() 103 { 104 return baseColumnPositions; 105 } 106 107 108 public Integer getKeyColumnPosition(Integer heapColumnPosition) 109 { 110 return new Integer (getKeyColumnPosition(heapColumnPosition.intValue())); 111 } 112 113 114 public int getKeyColumnPosition(int heapColumnPosition) 115 { 116 117 int keyPosition = 0; 118 119 for (int index = 0; index < baseColumnPositions.length; index++) 120 { 121 122 if (baseColumnPositions[index] == heapColumnPosition) 123 { 124 keyPosition = index + 1; 125 break; 126 } 127 } 128 129 return keyPosition; 130 } 131 132 133 public int numberOfOrderedColumns() 134 { 135 return numberOfOrderedColumns; 136 } 137 138 139 public String indexType() 140 { 141 return indexType; 142 } 143 144 145 public boolean isAscending(Integer keyColumnPosition) 146 { 147 int i = keyColumnPosition.intValue() - 1; 148 if (i < 0 || i >= baseColumnPositions.length) 149 return false; 150 return isAscending[i]; 151 } 152 153 154 public boolean isDescending(Integer keyColumnPosition) 155 { 156 int i = keyColumnPosition.intValue() - 1; 157 if (i < 0 || i >= baseColumnPositions.length) 158 return false; 159 return ! isAscending[i]; 160 } 161 162 163 public boolean[] isAscending() 164 { 165 return isAscending; 166 } 167 168 169 public void setBaseColumnPositions(int[] baseColumnPositions) 170 { 171 this.baseColumnPositions = baseColumnPositions; 172 } 173 174 175 public void setIsAscending(boolean[] isAscending) 176 { 177 this.isAscending = isAscending; 178 } 179 180 181 public void setNumberOfOrderedColumns(int numberOfOrderedColumns) 182 { 183 this.numberOfOrderedColumns = numberOfOrderedColumns; 184 } 185 186 public String toString() 187 { 188 String uniqueness; 189 String cols; 190 191 StringBuffer sb = new StringBuffer (60); 192 193 if (isUnique) 194 sb.append("UNIQUE "); 195 196 sb.append(indexType); 197 198 sb.append(" ("); 199 200 201 for (int i = 0; i < baseColumnPositions.length; i++) 202 { 203 if (i > 0) 204 sb.append(", "); 205 sb.append(baseColumnPositions[i]); 206 if (! isAscending[i]) 207 sb.append(" DESC"); 208 } 209 210 sb.append(")"); 211 212 return sb.toString(); 213 } 214 215 216 217 222 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 223 { 224 FormatableHashtable fh = (FormatableHashtable)in.readObject(); 225 isUnique = fh.getBoolean("isUnique"); 226 int bcpLength = fh.getInt("keyLength"); 227 baseColumnPositions = new int[bcpLength]; 228 isAscending = new boolean[bcpLength]; 229 for (int i = 0; i < bcpLength; i++) 230 { 231 baseColumnPositions[i] = fh.getInt("bcp" + i); 232 isAscending[i] = fh.getBoolean("isAsc" + i); 233 } 234 numberOfOrderedColumns = fh.getInt("orderedColumns"); 235 indexType = (String )fh.get("indexType"); 236 } 237 238 243 public void writeExternal(ObjectOutput out) throws IOException 244 { 245 FormatableHashtable fh = new FormatableHashtable(); 246 fh.putBoolean("isUnique", isUnique); 247 fh.putInt("keyLength", baseColumnPositions.length); 248 for (int i = 0; i < baseColumnPositions.length; i++) 249 { 250 fh.putInt("bcp" + i, baseColumnPositions[i]); 251 fh.putBoolean("isAsc" + i, isAscending[i]); 252 } 253 fh.putInt("orderedColumns", numberOfOrderedColumns); 254 fh.put("indexType", indexType); 255 out.writeObject(fh); 256 } 257 258 259 public int getTypeFormatId() 260 { 261 return StoredFormatIds.INDEX_DESCRIPTOR_IMPL_V02_ID; 262 } 263 264 271 272 public boolean equals(Object other) 273 { 274 275 boolean retval = false; 276 277 278 if (other instanceof IndexDescriptorImpl) 279 { 280 IndexDescriptorImpl id = (IndexDescriptorImpl) other; 281 282 286 if ((id.isUnique == this.isUnique) && 287 (id.baseColumnPositions.length == 288 this.baseColumnPositions.length) && 289 (id.numberOfOrderedColumns == this.numberOfOrderedColumns) && 290 (id.indexType.equals(this.indexType))) 291 { 292 296 retval = true; 297 298 for (int i = 0; i < this.baseColumnPositions.length; i++) 299 { 300 301 if ((id.baseColumnPositions[i] != 302 this.baseColumnPositions[i]) || (id.isAscending[i] != 303 this.isAscending[i])) 304 { 305 retval = false; 306 break; 307 } 308 } 309 } 310 } 311 312 return retval; 313 } 314 315 318 public int hashCode() 319 { 320 int retval; 321 322 retval = isUnique ? 1 : 2; 323 retval *= numberOfOrderedColumns; 324 for (int i = 0; i < baseColumnPositions.length; i++) 325 { 326 retval *= baseColumnPositions[i]; 327 } 328 retval *= indexType.hashCode(); 329 330 return retval; 331 } 332 } 333 | Popular Tags |