1 24 25 package com.mckoi.database; 26 27 import com.mckoi.util.IntegerVector; 28 import com.mckoi.util.IndexComparator; 29 import com.mckoi.util.BlockIntegerList; 30 import com.mckoi.debug.DebugLogger; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.OutputStream ; 34 35 53 54 public abstract class SelectableScheme { 55 56 59 protected static final BlockIntegerList EMPTY_LIST; 60 protected static final BlockIntegerList ONE_LIST; 61 62 static { 63 EMPTY_LIST = new BlockIntegerList(); 64 EMPTY_LIST.setImmutable(); 65 ONE_LIST = new BlockIntegerList(); 66 ONE_LIST.add(0); 67 ONE_LIST.setImmutable(); 68 } 69 70 73 private final TableDataSource table; 74 75 78 private final int column; 79 80 83 private boolean immutable = false; 84 85 88 public SelectableScheme(TableDataSource table, int column) { 89 this.table = table; 90 this.column = column; 91 } 92 93 96 protected final TableDataSource getTable() { 97 return table; 98 } 99 100 103 protected final TransactionSystem getSystem() { 104 return table.getSystem(); 105 } 106 107 110 protected final DebugLogger Debug() { 111 return getSystem().Debug(); 112 } 113 114 117 protected final int getColumn() { 118 return column; 119 } 120 121 124 protected final TObject getCellContents(int row) { 125 return table.getCellContents(column, row); 126 } 127 128 131 public final void setImmutable() { 132 immutable = true; 133 } 134 135 138 public final boolean isImmutable() { 139 return immutable; 140 } 141 142 145 public String toString() { 146 String table_name; 148 if (table instanceof DefaultDataTable) { 149 table_name = ((DefaultDataTable) table).getTableName().toString(); 150 } 151 else { 152 table_name = "VirtualTable"; 153 } 154 155 StringBuffer buf = new StringBuffer (); 156 buf.append("[ SelectableScheme "); 157 buf.append(super.toString()); 158 buf.append(" for table: "); 159 buf.append(table_name); 160 buf.append("]"); 161 162 return new String (buf); 163 } 164 165 168 public abstract void writeTo(OutputStream out) throws IOException ; 169 170 174 public abstract void readFrom(InputStream in) throws IOException ; 175 176 190 public abstract SelectableScheme copy(TableDataSource table, 191 boolean immutable); 192 193 196 public abstract void dispose(); 197 198 199 204 208 abstract void insert(int row); 209 210 214 abstract void remove(int row); 215 216 228 public BlockIntegerList internalOrderIndexSet(final IntegerVector row_set) { 229 int row_set_length = row_set.size(); 231 232 if (row_set_length == 0) { 235 return EMPTY_LIST; 236 } 237 else if (row_set_length == 1) { 238 return ONE_LIST; 239 } 240 241 BlockIntegerList new_set = new BlockIntegerList(); 244 245 if (row_set_length <= 250000) { 246 final TObject[] subset_list = new TObject[row_set_length]; 251 for (int i = 0; i < row_set_length; ++i) { 252 subset_list[i] = getCellContents(row_set.intAt(i)); 253 } 254 255 IndexComparator comparator = new IndexComparator() { 257 public int compare(int index, Object val) { 258 TObject cell = subset_list[index]; 259 return cell.compareTo((TObject) val); 260 } 261 public int compare(int index1, int index2) { 262 throw new Error ("Shouldn't be called!"); 263 } 264 }; 265 266 for (int i = 0; i < row_set_length; ++i) { 268 TObject cell = subset_list[i]; 269 new_set.insertSort(cell, i, comparator); 270 } 271 272 } 273 else { 274 276 IndexComparator comparator = new IndexComparator() { 278 public int compare(int index, Object val) { 279 TObject cell = getCellContents(row_set.intAt(index)); 280 return cell.compareTo((TObject) val); 281 } 282 public int compare(int index1, int index2) { 283 throw new Error ("Shouldn't be called!"); 284 } 285 }; 286 287 for (int i = 0; i < row_set_length; ++i) { 289 TObject cell = getCellContents(row_set.intAt(i)); 290 new_set.insertSort(cell, i, comparator); 291 } 292 293 } 294 295 return new_set; 296 297 } 298 299 307 public SelectableScheme getSubsetScheme(Table subset_table, 308 int subset_column) { 309 310 IntegerVector row_set = new IntegerVector(subset_table.getRowCount()); 312 RowEnumeration e = subset_table.rowEnumeration(); 313 while (e.hasMoreRows()) { 314 row_set.addInt(e.nextRowIndex()); 315 } 316 subset_table.setToRowTableDomain(subset_column, row_set, getTable()); 317 318 BlockIntegerList new_set = internalOrderIndexSet(row_set); 321 322 if (new_set.size() != row_set.size()) { 324 throw new RuntimeException ("Internal sort error in finding sub-set."); 325 } 326 327 InsertSearch is = new InsertSearch(subset_table, subset_column, new_set); 330 is.RECORD_UID = false; 332 return is; 333 334 } 335 336 343 public IntegerVector selectAll() { 344 return selectRange(new SelectableRange( 345 SelectableRange.FIRST_VALUE, SelectableRange.FIRST_IN_SET, 346 SelectableRange.LAST_VALUE, SelectableRange.LAST_IN_SET)); 347 } 348 349 public IntegerVector selectFirst() { 350 return selectRange(new SelectableRange( 353 SelectableRange.FIRST_VALUE, SelectableRange.FIRST_IN_SET, 354 SelectableRange.LAST_VALUE, SelectableRange.FIRST_IN_SET)); 355 } 356 357 public IntegerVector selectNotFirst() { 358 return selectRange(new SelectableRange( 361 SelectableRange.AFTER_LAST_VALUE, SelectableRange.FIRST_IN_SET, 362 SelectableRange.LAST_VALUE, SelectableRange.LAST_IN_SET)); 363 } 364 365 public IntegerVector selectLast() { 366 return selectRange(new SelectableRange( 367 SelectableRange.FIRST_VALUE, SelectableRange.LAST_IN_SET, 368 SelectableRange.LAST_VALUE, SelectableRange.LAST_IN_SET)); 369 } 370 371 public IntegerVector selectNotLast() { 372 return selectRange(new SelectableRange( 373 SelectableRange.FIRST_VALUE, SelectableRange.FIRST_IN_SET, 374 SelectableRange.BEFORE_FIRST_VALUE, SelectableRange.LAST_IN_SET)); 375 } 376 377 380 public IntegerVector selectAllNonNull() { 381 return selectRange(new SelectableRange( 382 SelectableRange.AFTER_LAST_VALUE, TObject.nullVal(), 383 SelectableRange.LAST_VALUE, SelectableRange.LAST_IN_SET)); 384 } 385 386 public IntegerVector selectEqual(TObject ob) { 387 if (ob.isNull()) { 388 return new IntegerVector(0); 389 } 390 return selectRange(new SelectableRange( 391 SelectableRange.FIRST_VALUE, ob, 392 SelectableRange.LAST_VALUE, ob)); 393 } 394 395 public IntegerVector selectNotEqual(TObject ob) { 396 if (ob.isNull()) { 397 return new IntegerVector(0); 398 } 399 return selectRange(new SelectableRange[] { 400 new SelectableRange( 401 SelectableRange.AFTER_LAST_VALUE, TObject.nullVal(), 402 SelectableRange.BEFORE_FIRST_VALUE, ob) 403 , new SelectableRange( 404 SelectableRange.AFTER_LAST_VALUE, ob, 405 SelectableRange.LAST_VALUE, SelectableRange.LAST_IN_SET) 406 }); 407 } 408 409 public IntegerVector selectGreater(TObject ob) { 410 if (ob.isNull()) { 411 return new IntegerVector(0); 412 } 413 return selectRange(new SelectableRange( 414 SelectableRange.AFTER_LAST_VALUE, ob, 415 SelectableRange.LAST_VALUE, SelectableRange.LAST_IN_SET)); 416 } 417 418 public IntegerVector selectLess(TObject ob) { 419 if (ob.isNull()) { 420 return new IntegerVector(0); 421 } 422 return selectRange(new SelectableRange( 423 SelectableRange.AFTER_LAST_VALUE, TObject.nullVal(), 424 SelectableRange.BEFORE_FIRST_VALUE, ob)); 425 } 426 427 public IntegerVector selectGreaterOrEqual(TObject ob) { 428 if (ob.isNull()) { 429 return new IntegerVector(0); 430 } 431 return selectRange(new SelectableRange( 432 SelectableRange.FIRST_VALUE, ob, 433 SelectableRange.LAST_VALUE, SelectableRange.LAST_IN_SET)); 434 } 435 436 public IntegerVector selectLessOrEqual(TObject ob) { 437 if (ob.isNull()) { 438 return new IntegerVector(0); 439 } 440 return selectRange(new SelectableRange( 441 SelectableRange.AFTER_LAST_VALUE, TObject.nullVal(), 442 SelectableRange.LAST_VALUE, ob)); 443 } 444 445 public IntegerVector selectBetween(TObject ob1, TObject ob2) { 449 if (ob1.isNull() || ob2.isNull()) { 450 return new IntegerVector(0); 451 } 452 return selectRange(new SelectableRange( 453 SelectableRange.FIRST_VALUE, ob1, 454 SelectableRange.BEFORE_FIRST_VALUE, ob2)); 455 } 456 457 464 abstract IntegerVector selectRange(SelectableRange range); 465 466 478 abstract IntegerVector selectRange(SelectableRange[] ranges); 479 480 } 481 | Popular Tags |