|                                                                                                              1
 24
 25  package com.mckoi.database;
 26
 27  import com.mckoi.util.IntegerVector;
 28  import com.mckoi.util.BlockIntegerList;
 29  import com.mckoi.util.IntegerListInterface;
 30  import com.mckoi.util.IndexComparator;
 31  import com.mckoi.util.IntegerIterator;
 32  import java.util.Comparator
  ; 33  import java.util.Arrays
  ; 34  import java.io.*;
 35
 36
 53
 54  public final class InsertSearch extends CollatedBaseSearch {
 55
 56
 60    private IntegerListInterface set_list;
 61
 62
 67    boolean RECORD_UID;
 68
 69
 73    private IndexComparator set_comparator;
 74
 75
 76
 78
 82    private int DEBUG_immutable_set_size;
 83
 84
 85
 86
 87
 90    public InsertSearch(TableDataSource table, int column) {
 91      super(table, column);
 92      set_list = new BlockIntegerList();
 93
 94              setupComparator();
 97    }
 98
 99
 104   public InsertSearch(TableDataSource table, int column, IntegerVector vec) {
 105     this(table, column);
 106     for (int i = 0; i < vec.size(); ++i) {
 107       set_list.add(vec.intAt(i));
 108     }
 109
 110             checkSchemeSorted();
 113
 114   }
 115
 116
 121   InsertSearch(TableDataSource table, int column, IntegerListInterface list) {
 122     this(table, column);
 123     this.set_list = list;
 124
 125             checkSchemeSorted();
 128
 129   }
 130
 131
 135   private InsertSearch(TableDataSource table, InsertSearch from,
 136                        boolean immutable) {
 137     super(table, from.getColumn());
 138
 139     if (immutable) {
 140       setImmutable();
 141     }
 142
 143     if (immutable) {
 144             set_list = from.set_list;
 146       DEBUG_immutable_set_size = set_list.size();
 147     }
 148     else {
 149       set_list = new BlockIntegerList(from.set_list);
 150     }
 151
 152         RECORD_UID = from.RECORD_UID;
 154
 155             setupComparator();
 158
 159   }
 160
 161
 165   private void setupComparator() {
 166     set_comparator = new IndexComparator() {
 167
 168       private int internalCompare(int index, TObject cell2) {
 169         TObject cell1 = getCellContents(index);
 170         return cell1.compareTo(cell2);
 171       }
 172
 173       public int compare(int index, Object
  val) { 174         return internalCompare(index, (TObject) val);
 175       }
 176       public int compare(int index1, int index2) {
 177         TObject cell = getCellContents(index2);
 178         return internalCompare(index1, cell);
 179       }
 180     };
 181   }
 182
 183
 184
 185
 186
 187
 188
 193   public void insert(int row) {
 194     if (isImmutable()) {
 195       throw new Error
  ("Tried to change an immutable scheme."); 196     }
 197
 198     final TObject cell = getCellContents(row);
 199     set_list.insertSort(cell, row, set_comparator);
 200
 201   }
 202
 203
 208   public void remove(int row) {
 209     if (isImmutable()) {
 210       throw new Error
  ("Tried to change an immutable scheme."); 211     }
 212
 213     TObject cell = getCellContents(row);
 214     int removed = set_list.removeSort(cell, row, set_comparator);
 215
 216     if (removed != row) {
 217       throw new Error
  ("Removed value different than row asked to remove.  " + 218                       "To remove: " + row + "  Removed: " + removed);
 219     }
 220
 221   }
 222
 223
 234   private final IndexComparator safeSetComparator() {
 235       return set_comparator;
 237   }
 239
 240
 244   public void readFrom(InputStream in) throws IOException {
 245     if (set_list.size() != 0) {
 246       throw new RuntimeException
  ("Error reading scheme, already a set in the Scheme"); 247     }
 248     DataInputStream din = new DataInputStream(in);
 249     int vec_size = din.readInt();
 250
 251     int row_count = getTable().getRowCount();
 252         if (row_count != vec_size) {
 254       throw new IOException(
 255          "Different table row count to indices in scheme. " +
 256          "table=" + row_count +
 257          ", vec_size=" + vec_size);
 258     }
 259
 260     for (int i = 0; i < vec_size; ++i) {
 261       int row = din.readInt();
 262       if (row < 0) {         set_list = new BlockIntegerList();
 264         throw new IOException("Scheme contains out of table bounds index.");
 265       }
 266       set_list.add(row);
 267     }
 268
 269     getSystem().stats().add(vec_size, "{session} InsertSearch.read_indices");
 270
 271             checkSchemeSorted();
 274   }
 275
 276
 279   public void writeTo(OutputStream out) throws IOException {
 280     DataOutputStream dout = new DataOutputStream(out);
 281     int list_size = set_list.size();
 282     dout.writeInt(list_size);
 283
 284     IntegerIterator i = set_list.iterator(0, list_size - 1);
 285     while (i.hasNext()) {
 286       dout.writeInt(i.next());
 287     }
 288   }
 289
 290
 296   public SelectableScheme copy(TableDataSource table, boolean immutable) {
 297             if (isImmutable()) {
 300       if (DEBUG_immutable_set_size != set_list.size()) {
 301         throw new Error
  ("Assert failed: " + 302                         "Immutable set size is different from when created.");
 303       }
 304     }
 305
 306             return new InsertSearch(table, this, immutable);
 309   }
 310
 311
 314   public void dispose() {
 315         set_list = null;
 317     set_comparator = null;
 318   }
 319
 320
 326   private void checkSchemeSorted() {
 327   }
 347
 348
 350   protected int searchFirst(TObject val) {
 351     return set_list.searchFirst(val, safeSetComparator());
 352   }
 353
 354   protected int searchLast(TObject val) {
 355     return set_list.searchLast(val, safeSetComparator());
 356   }
 357
 358   protected int setSize() {
 359     return set_list.size();
 360   }
 361
 362   protected TObject firstInCollationOrder() {
 363     return getCellContents(set_list.get(0));
 364   }
 365
 366   protected TObject lastInCollationOrder() {
 367     return getCellContents(set_list.get(setSize() - 1));
 368   }
 369
 370   protected IntegerVector addRangeToSet(int start, int end,
 371                                         IntegerVector ivec) {
 372     if (ivec == null) {
 373       ivec = new IntegerVector((end - start) + 2);
 374     }
 375     IntegerIterator i = set_list.iterator(start, end);
 376     while (i.hasNext()) {
 377       ivec.addInt(i.next());
 378     }
 379     return ivec;
 380   }
 381
 382
 385
 386   public IntegerVector selectAll() {
 387     IntegerVector ivec = new IntegerVector(set_list);
 388     return ivec;
 389   }
 390
 391 }
 392
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |