1 21 22 package org.apache.derby.iapi.sql.dictionary; 23 24 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList; 25 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 26 27 import org.apache.derby.iapi.sql.execute.ExecutionContext; 28 import org.apache.derby.iapi.sql.execute.ExecIndexRow; 29 import org.apache.derby.iapi.sql.execute.ExecRow; 30 import org.apache.derby.iapi.sql.execute.ExecutionFactory; 31 32 import org.apache.derby.iapi.types.RowLocation; 33 import org.apache.derby.iapi.types.DataTypeDescriptor; 34 35 import org.apache.derby.iapi.services.io.Formatable; 36 import org.apache.derby.iapi.services.io.FormatIdUtil; 37 import org.apache.derby.iapi.services.io.StoredFormatIds; 38 39 import org.apache.derby.iapi.services.sanity.SanityManager; 40 41 import org.apache.derby.iapi.services.context.ContextService; 42 43 import org.apache.derby.iapi.error.StandardException; 44 45 import org.apache.derby.catalog.IndexDescriptor; 46 import org.apache.derby.catalog.types.IndexDescriptorImpl; 47 48 import java.io.ObjectInput ; 49 import java.io.ObjectOutput ; 50 import java.io.IOException ; 51 import org.apache.derby.iapi.services.io.FormatableBitSet; 52 53 57 public class IndexRowGenerator implements IndexDescriptor, Formatable 58 { 59 IndexDescriptor id; 60 private ExecutionFactory ef; 61 62 77 public IndexRowGenerator(String indexType, 78 boolean isUnique, 79 int[] baseColumnPositions, 80 boolean[] isAscending, 81 int numberOfOrderedColumns) 82 { 83 id = new IndexDescriptorImpl(indexType, 84 isUnique, 85 baseColumnPositions, 86 isAscending, 87 numberOfOrderedColumns); 88 89 if (SanityManager.DEBUG) 90 { 91 SanityManager.ASSERT(baseColumnPositions != null, 92 "baseColumnPositions are null"); 93 } 94 } 95 96 101 public IndexRowGenerator(IndexDescriptor indexDescriptor) 102 { 103 id = indexDescriptor; 104 } 105 106 111 public ExecIndexRow getIndexRowTemplate() 112 { 113 return getExecutionFactory().getIndexableRow( 114 id.baseColumnPositions().length + 1); 115 } 116 117 137 public void getIndexRow(ExecRow baseRow, 138 RowLocation rowLocation, 139 ExecIndexRow indexRow, 140 FormatableBitSet bitSet) 141 throws StandardException 142 { 143 147 int[] baseColumnPositions = id.baseColumnPositions(); 148 int colCount = baseColumnPositions.length; 149 150 if (bitSet == null) 151 { 152 156 for (int i = 0; i < colCount ; i++) 157 { 158 indexRow.setColumn(i + 1, 159 baseRow.getColumn(baseColumnPositions[i])); 160 } 161 } 162 else 163 { 164 if (SanityManager.DEBUG) 165 { 166 SanityManager.ASSERT(!bitSet.get(0), "element zero of the bitSet passed into getIndexRow() is not false, bitSet should be 1 based"); 167 } 168 169 173 for (int i = 0; i < colCount; i++) 174 { 175 int fullColumnNumber = baseColumnPositions[i]; 176 int partialColumnNumber = 0; 177 for (int index = 1; index <= fullColumnNumber; index++) 178 { 179 if (bitSet.get(index)) 180 { 181 partialColumnNumber++; 182 } 183 } 184 indexRow.setColumn(i + 1, 185 baseRow.getColumn(partialColumnNumber)); 186 } 187 } 188 189 190 indexRow.setColumn(colCount + 1, rowLocation); 191 } 192 193 202 public ExecIndexRow getNullIndexRow(ColumnDescriptorList columnList, 203 RowLocation rowLocation) 204 throws StandardException 205 { 206 int[] baseColumnPositions = id.baseColumnPositions(); 207 int i; 208 ExecIndexRow indexRow = getIndexRowTemplate(); 209 210 for (i = 0; i < baseColumnPositions.length; i++) 211 { 212 DataTypeDescriptor dtd = 213 columnList.elementAt(baseColumnPositions[i] - 1).getType(); 214 indexRow.setColumn(i + 1, dtd.getNull()); 215 } 216 217 indexRow.setColumn(i + 1, rowLocation); 218 return indexRow; 219 } 220 221 230 public boolean indexChanged(int[] changedColumnIds) 231 { 232 int[] baseColumnPositions = id.baseColumnPositions(); 233 234 for (int ix = 0; ix < changedColumnIds.length; ix++) 235 { 236 for (int iy = 0; iy < baseColumnPositions.length; iy++) 237 { 238 if (changedColumnIds[ix] == baseColumnPositions[iy]) 239 return true; 240 } 241 } 242 return false; 243 } 244 245 246 249 public IndexDescriptor getIndexDescriptor() 250 { 251 return id; 252 } 253 254 255 public IndexRowGenerator() 256 { 257 } 258 259 260 public boolean isUnique() 261 { 262 return id.isUnique(); 263 } 264 265 266 public int[] baseColumnPositions() 267 { 268 return id.baseColumnPositions(); 269 } 270 271 272 public Integer getKeyColumnPosition(Integer heapColumnPosition) 273 { 274 return id.getKeyColumnPosition(heapColumnPosition); 275 } 276 277 278 public int getKeyColumnPosition(int heapColumnPosition) 279 { 280 return id.getKeyColumnPosition(heapColumnPosition); 281 } 282 283 284 public int numberOfOrderedColumns() 285 { 286 return id.numberOfOrderedColumns(); 287 } 288 289 290 public String indexType() 291 { 292 return id.indexType(); 293 } 294 295 public String toString() 296 { 297 return id.toString(); 298 } 299 300 301 public boolean isAscending(Integer keyColumnPosition) 302 { 303 return id.isAscending(keyColumnPosition); 304 } 305 306 307 public boolean isDescending(Integer keyColumnPosition) 308 { 309 return id.isDescending(keyColumnPosition); 310 } 311 312 313 public boolean[] isAscending() 314 { 315 return id.isAscending(); 316 } 317 318 319 public void setBaseColumnPositions(int[] baseColumnPositions) 320 { 321 id.setBaseColumnPositions(baseColumnPositions); 322 } 323 324 325 public void setIsAscending(boolean[] isAscending) 326 { 327 id.setIsAscending(isAscending); 328 } 329 330 331 public void setNumberOfOrderedColumns(int numberOfOrderedColumns) 332 { 333 id.setNumberOfOrderedColumns(numberOfOrderedColumns); 334 } 335 336 343 344 public boolean equals(Object other) 345 { 346 return id.equals(other); 347 } 348 349 352 public int hashCode() 353 { 354 return id.hashCode(); 355 } 356 357 private ExecutionFactory getExecutionFactory() 358 { 359 if (ef == null) 360 { 361 ExecutionContext ec; 362 363 ec = (ExecutionContext) 364 ContextService.getContext(ExecutionContext.CONTEXT_ID); 365 ef = ec.getExecutionFactory(); 366 } 367 return ef; 368 } 369 370 376 382 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 383 { 384 id = (IndexDescriptor)in.readObject(); 385 } 386 387 391 public void writeExternal(ObjectOutput out) throws IOException 392 { 393 out.writeObject(id); 394 } 395 396 397 public int getTypeFormatId() 398 { 399 return StoredFormatIds.INDEX_ROW_GENERATOR_V01_ID; 400 } 401 402 } 403 | Popular Tags |