1 21 22 package org.apache.derby.iapi.store.access; 23 24 import org.apache.derby.iapi.services.monitor.Monitor; 25 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 28 import org.apache.derby.iapi.error.StandardException; 29 import org.apache.derby.iapi.services.io.Storable; 30 import org.apache.derby.iapi.types.DataValueDescriptor; 31 import org.apache.derby.iapi.services.io.FormatableBitSet; 32 import org.apache.derby.iapi.services.loader.InstanceGetter; 33 34 import org.apache.derby.iapi.store.raw.FetchDescriptor; 35 36 import java.lang.reflect.InvocationTargetException ; 37 38 import java.util.Enumeration ; 39 import java.util.Hashtable ; 40 import java.util.Vector ; 41 42 98 public class RowUtil 99 { 100 private RowUtil() {} 101 102 106 public static final DataValueDescriptor[] EMPTY_ROW = 107 new DataValueDescriptor[0]; 108 109 113 public static final FormatableBitSet EMPTY_ROW_BITSET = 114 new FormatableBitSet(0); 115 116 120 public static final FetchDescriptor EMPTY_ROW_FETCH_DESCRIPTOR = 121 new FetchDescriptor(0); 122 123 public static final FetchDescriptor[] ROWUTIL_FETCH_DESCRIPTOR_CONSTANTS = 124 {EMPTY_ROW_FETCH_DESCRIPTOR, 125 new FetchDescriptor(1, 1), 126 new FetchDescriptor(2, 2), 127 new FetchDescriptor(3, 3), 128 new FetchDescriptor(4, 4), 129 new FetchDescriptor(5, 5), 130 new FetchDescriptor(6, 6), 131 new FetchDescriptor(7, 7)}; 132 133 134 144 public static DataValueDescriptor getColumn( 145 DataValueDescriptor[] row, 146 FormatableBitSet columnList, 147 int columnId) 148 { 149 150 if (columnList == null) 151 return columnId < row.length ? row[columnId] : null; 152 153 154 if (!(columnList.getLength() > columnId && columnList.isSet(columnId))) 155 return null; 156 157 return columnId < row.length ? row[columnId] : null; 158 159 } 160 161 public static Object getColumn( 162 Object [] row, 163 FormatableBitSet columnList, 164 int columnId) 165 { 166 167 if (columnList == null) 168 return columnId < row.length ? row[columnId] : null; 169 170 171 if (!(columnList.getLength() > columnId && columnList.isSet(columnId))) 172 return null; 173 174 return columnId < row.length ? row[columnId] : null; 175 176 } 177 178 184 public static FormatableBitSet getQualifierBitSet(Qualifier[][] qualifiers) 185 { 186 FormatableBitSet qualifierColumnList = new FormatableBitSet(); 187 188 if (qualifiers != null) 189 { 190 for (int i = 0; i < qualifiers.length; i++) 191 { 192 for (int j = 0; j < qualifiers[i].length; j++) 193 { 194 int colId = qualifiers[i][j].getColumnId(); 195 196 qualifierColumnList.grow(colId+1); 198 qualifierColumnList.set(colId); 199 } 200 } 201 } 202 203 return qualifierColumnList; 204 } 205 206 222 public static int getNumberOfColumns( 223 int maxColumnNumber, 224 FormatableBitSet columnList) 225 { 226 if (SanityManager.DEBUG) 227 SanityManager.ASSERT(columnList != null); 228 229 int max_col_number = columnList.getLength(); 230 231 if (maxColumnNumber > 0 && maxColumnNumber < max_col_number) 232 max_col_number = maxColumnNumber; 233 234 int ret_num_cols = 0; 235 236 for (int i = 0; i < max_col_number; i++) 237 { 238 if (columnList.isSet(i)) 239 ret_num_cols++; 240 } 241 242 return(ret_num_cols); 243 } 244 245 251 public static boolean isRowEmpty( 252 DataValueDescriptor[] row) 253 { 254 255 if (row == null) 256 return true; 257 258 if (row.length == 0) 259 return true; 260 261 return false; 262 } 263 264 268 public static int columnOutOfRange( 269 DataValueDescriptor[] row, 270 FormatableBitSet columnList, 271 int maxColumns) 272 { 273 274 if (columnList == null) { 275 if (row.length > maxColumns) 276 return maxColumns; 277 278 return -1; 279 } 280 281 int size = columnList.getLength(); 282 for (int i = maxColumns; i < size; i++) { 283 if (columnList.isSet(i)) 284 return i; 285 } 286 287 return -1; 288 } 289 290 294 public static int nextColumn( 295 Object [] row, 296 FormatableBitSet columnList, 297 int startColumn) 298 { 299 300 if (columnList != null) { 301 302 int size = columnList.getLength(); 303 304 for (; startColumn < size; startColumn++) { 305 if (columnList.isSet(startColumn)) { 306 return startColumn; 307 } 308 } 309 310 return -1; 311 } 312 313 if (row == null) 314 return -1; 315 316 return startColumn < row.length ? startColumn : -1; 317 } 318 319 325 public static final FetchDescriptor getFetchDescriptorConstant( 326 int single_column_number) 327 { 328 if (single_column_number < ROWUTIL_FETCH_DESCRIPTOR_CONSTANTS.length) 329 { 330 return(ROWUTIL_FETCH_DESCRIPTOR_CONSTANTS[single_column_number]); 331 } 332 else 333 { 334 return( 335 new FetchDescriptor( 336 single_column_number, single_column_number)); 337 } 338 } 339 340 344 345 361 public static InstanceGetter[] newClassInfoTemplate( 362 FormatableBitSet column_list, 363 int[] format_ids) 364 throws StandardException 365 { 366 int num_cols = format_ids.length; 367 InstanceGetter[] ret_row = new InstanceGetter[num_cols]; 368 369 int column_listSize = 370 (column_list == null) ? 0 : column_list.getLength(); 371 372 for (int i = 0; i < num_cols; i++) 373 { 374 if ((column_list != null) && 376 !((column_listSize > i) && 377 (column_list.isSet(i)))) 378 { 379 } 381 else 382 { 383 385 387 ret_row[i] = Monitor.classFromIdentifier(format_ids[i]); 388 } 389 } 390 391 return(ret_row); 392 } 393 394 395 private static void newRowFromClassInfoTemplateError() 396 { 397 if (SanityManager.DEBUG) 398 SanityManager.THROWASSERT( 399 "unexpected error in newRowFromClassInfoTemplate()"); 400 } 401 402 421 public static DataValueDescriptor[] newRowFromClassInfoTemplate( 422 InstanceGetter[] classinfo_template) 423 throws StandardException 424 { 425 426 DataValueDescriptor[] columns = 427 new DataValueDescriptor[classinfo_template.length]; 428 429 try 430 { 431 for (int column_index = classinfo_template.length; 432 column_index-- > 0;) 433 { 434 if (classinfo_template[column_index] != null) 435 { 436 columns[column_index] = (DataValueDescriptor) 439 classinfo_template[column_index].getNewInstance(); 440 } 441 } 442 } 443 catch (InstantiationException ie) 444 { 445 newRowFromClassInfoTemplateError(); 446 } 447 catch (IllegalAccessException iae) 448 { 449 newRowFromClassInfoTemplateError(); 450 } 451 catch (InvocationTargetException ite) 452 { 453 newRowFromClassInfoTemplateError(); 454 } 455 456 return columns; 457 } 458 459 460 470 public static String toString(Object [] row) 471 { 472 if (SanityManager.DEBUG) 473 { 474 475 String str = new String (); 476 477 if (row != null) 478 { 479 if (row.length == 0) 480 { 481 str = "empty row"; 482 } 483 else 484 { 485 for (int i = 0; i < row.length; i++) 486 str += "col[" + i + "]=" + row[i]; 487 } 488 } 489 else 490 { 491 str = "row is null"; 492 } 493 494 return(str); 495 } 496 else 497 { 498 return(null); 499 } 500 } 501 502 510 511 public static String toString(Hashtable hash_table) 513 { 514 if (SanityManager.DEBUG) 515 { 516 String str = new String (); 517 518 Object row_or_vector; 519 520 for (Enumeration e = hash_table.elements(); e.hasMoreElements();) 521 { 522 row_or_vector = e.nextElement(); 523 524 if (row_or_vector instanceof Object []) 525 { 526 str += RowUtil.toString((Object []) row_or_vector); 528 str += "\n"; 529 } 530 else if (row_or_vector instanceof Vector ) 531 { 532 Vector vec = (Vector ) row_or_vector; 534 535 for (int i = 0; i < vec.size(); i++) 536 { 537 str += 538 "vec[" + i + "]:" + 539 RowUtil.toString((Object []) vec.elementAt(i)); 540 541 str += "\n"; 542 } 543 } 544 else 545 { 546 str += "BAD ENTRY\n"; 547 } 548 } 549 return(str); 550 } 551 else 552 { 553 return(null); 554 } 555 } 556 557 588 public static final boolean qualifyRow( 589 Object [] row, 590 Qualifier[][] qual_list) 591 throws StandardException 592 { 593 boolean row_qualifies = true; 594 595 if (SanityManager.DEBUG) 596 { 597 SanityManager.ASSERT(row != null); 598 } 599 600 602 if (SanityManager.DEBUG) 603 { 604 SanityManager.ASSERT(qual_list != null); 606 SanityManager.ASSERT(qual_list.length > 0); 607 } 608 609 for (int i = 0; i < qual_list[0].length; i++) 610 { 611 613 row_qualifies = false; 614 615 617 Qualifier q = qual_list[0][i]; 618 619 DataValueDescriptor columnValue = 622 (DataValueDescriptor) row[q.getColumnId()]; 623 624 row_qualifies = 625 columnValue.compare( 626 q.getOperator(), 627 q.getOrderable(), 628 q.getOrderedNulls(), 629 q.getUnknownRV()); 630 631 if (q.negateCompareResult()) 632 row_qualifies = !row_qualifies; 633 634 if (!row_qualifies) 636 return(false); 637 } 638 639 641 for (int and_idx = 1; and_idx < qual_list.length; and_idx++) 642 { 643 645 row_qualifies = false; 646 647 if (SanityManager.DEBUG) 648 { 649 SanityManager.ASSERT(qual_list[and_idx].length > 0); 651 } 652 653 for (int or_idx = 0; or_idx < qual_list[and_idx].length; or_idx++) 654 { 655 Qualifier q = qual_list[and_idx][or_idx]; 657 int col_id = q.getColumnId(); 658 659 if (SanityManager.DEBUG) 660 { 661 SanityManager.ASSERT( 662 (col_id < row.length), 663 "Qualifier is referencing a column not in the row."); 664 } 665 666 DataValueDescriptor columnValue = 669 (DataValueDescriptor) row[q.getColumnId()]; 670 671 if (SanityManager.DEBUG) 672 { 673 if (columnValue == null) 674 SanityManager.THROWASSERT( 675 "1:row = " + RowUtil.toString(row) + 676 "row.length = " + row.length + 677 ";q.getColumnId() = " + q.getColumnId()); 678 } 679 680 row_qualifies = 683 columnValue.compare( 684 q.getOperator(), 685 q.getOrderable(), 686 q.getOrderedNulls(), 687 q.getUnknownRV()); 688 689 if (q.negateCompareResult()) 690 row_qualifies = !row_qualifies; 691 692 694 696 if (row_qualifies) 699 break; 700 701 } 702 703 if (!row_qualifies) 706 break; 707 } 708 709 return(row_qualifies); 710 } 711 712 } 713 | Popular Tags |