1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table; 2 3 import com.daffodilwoods.daffodildb.server.sql99.utils.BaseSingleIterator; 4 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._Iterator; 5 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference; 6 import com.daffodilwoods.daffodildb.server.serversystem._ServerSession; 7 import com.daffodilwoods.daffodildb.utils.field.FieldBase; 8 import com.daffodilwoods.database.resource.DException; 9 import com.daffodilwoods.daffodildb.utils.comparator.SuperComparator; 10 import java.util.Arrays ; 11 import java.util.Comparator ; 12 import com.daffodilwoods.daffodildb.server.datasystem.interfaces. 13 _IndexIteratorInfo; 14 import com.daffodilwoods.daffodildb.utils.GetByteComparator; 15 import com.daffodilwoods.daffodildb.server.sql99.common.ColumnDetails; 16 import com.daffodilwoods.daffodildb.server.datasystem.interfaces. 17 _TableOperations; 18 import com.daffodilwoods.daffodildb.server.datasystem.interfaces. 19 _UserTableOperations; 20 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._DatabaseUser; 21 import com.daffodilwoods.daffodildb.server.sql99.expression. 22 booleanvalueexpression.predicates.invaluelist; 23 import com.daffodilwoods.daffodildb.server.sql99.utils.VariableValues; 24 import java.util.ArrayList ; 25 import com.daffodilwoods.daffodildb.utils.FieldUtility; 26 import com.daffodilwoods.daffodildb.server.sql99.utils.ConditionVariableValue; 27 import com.daffodilwoods.daffodildb.server.sql99.common.TableDetails; 28 import java.text.Collator ; 29 import com.daffodilwoods.database.utility.P; 30 import com.daffodilwoods.daffodildb.server.sql99.utils._IndexPredicate; 31 32 40 41 public class MultiValueIndexedFilterIterator 42 extends BaseSingleIterator 43 implements _TableOperations, _UserTableOperations, _IndexIteratorInfo { 44 protected Multicomparator comparator; 45 protected FieldBase[] list; 46 private invaluelist inlist; 47 private int state = INVALIDSTATE; 48 protected _Reference column; 49 private int pos; 50 private VariableValues vv; 51 _ServerSession session; 52 53 public MultiValueIndexedFilterIterator(_Iterator iterator, FieldBase[] list0, 54 _ServerSession serverSession, 55 ColumnDetails column0, 56 invaluelist inlist0) throws DException { 57 super(iterator); 58 list = list0; 59 comparator = new Multicomparator(GetByteComparator.getComparator(column0. 60 getDatatype(), true, 61 column0.getTableDetails().getColumnCharacteristics().getCollator())); 62 column = column0; 63 inlist = inlist0; 64 session = serverSession; 65 vv = new VariableValues(inlist.getReferences(new TableDetails[] {column0. 66 getTable()}), serverSession); 67 } 68 69 public boolean first() throws DException { 70 if (list.length == 0) { 71 state = AFTERLAST; 72 return false; 73 } 74 pos = 0; 75 return (state = iterator.first() && list[pos] != null 76 && alignForward() ? VALIDSTATE : AFTERLAST) != AFTERLAST; 77 } 78 79 public boolean last() throws DException { 80 if (list.length == 0) { 81 state = BEFOREFIRST; 82 return false; 83 } 84 pos = list.length - 1; 85 return (state = iterator.last() && list[pos] != null 86 && alignBackward() ? VALIDSTATE : BEFOREFIRST) != BEFOREFIRST; 87 } 88 89 public boolean next() throws DException { 90 switch (state) { 91 case INVALIDSTATE: 92 throw new DException("DSE4116", null); 93 case BEFOREFIRST: 94 return first(); 95 case AFTERLAST: 96 return false; 97 default: 98 if (list.length == 0) { 99 state = AFTERLAST; 100 return false; 101 } 102 103 if (!iterator.next() || list[pos] == null) { 104 state = AFTERLAST; 105 return false; 106 } 107 else { 108 if (!alignForward()) { 109 state = AFTERLAST; 110 return false; 111 } 112 return true; 113 } 114 } 115 } 116 117 public boolean previous() throws DException { 118 switch (state) { 119 case INVALIDSTATE: 120 throw new DException("DSE4117", null); 121 case AFTERLAST: 122 return last(); 123 case BEFOREFIRST: 124 return false; 125 default: 126 if (list.length == 0) { 127 state = BEFOREFIRST; 128 return false; 129 } 130 131 if (!iterator.previous() || list[pos] == null) { 132 state = BEFOREFIRST; 133 return false; 134 } 135 else { 136 if (!alignBackward()) { 137 state = BEFOREFIRST; 138 return false; 139 } 140 return true; 141 } 142 } 143 } 144 145 private boolean alignForward() throws DException { 146 int cmp = 0; 147 while ( (cmp = compare(iterator.getColumnValues(column), list[pos])) != 0) { 148 if (cmp > 0) { 149 if (!alignArray()) 150 return false; 151 } 152 else { 153 if (!alignIterator()) 154 return false; 155 } 156 } 157 return true; 158 } 159 160 161 private boolean alignBackward() throws DException { 162 int cmp = 0; 163 while ( (cmp = compare(iterator.getColumnValues(column), list[pos])) != 0) { 164 if (cmp > 0) { 165 if (!alignIteratorBackward()) 166 return false; 167 } 168 else { 169 if (!alignArrayBackward()) 170 return false; 171 } 172 } 173 return true; 174 } 175 176 private boolean alignArrayBackward() throws DException { 177 int a = Arrays.binarySearch(list, iterator.getColumnValues(column), 178 comparator); 179 if ( a == -1) { 180 return false; 181 } 182 if (a >= 0) { 183 pos = a; 184 return true; 185 } 186 else { 187 189 pos = -a - 2; 190 return true; 191 } 192 } 193 194 195 private boolean alignArray() throws DException { 196 int a = Arrays.binarySearch(list, iterator.getColumnValues(column), 197 comparator); 198 200 206 if ( -a > list.length) { 207 return false; 208 } 209 if (a >= 0) { 210 pos = a; 211 return true; 212 } 213 else { 214 pos = -a - 1; return true; 216 } 217 } 218 219 220 private boolean alignIteratorBackward() throws DException { 221 return iterator.seekFromBottomRelative(new Object [] {list[pos]}); 222 } 223 224 private boolean alignIterator() throws DException { 225 return iterator.seekFromTopRelative(new Object [] {list[pos]}); 226 } 227 228 protected int compare(Object object1, Object object2) throws DException { 229 int cmp = comparator.compare(object1, object2); 230 return cmp; 231 } 232 233 public void update(_DatabaseUser parm1, Object parm2) throws DException { 234 ( (_UserTableOperations) iterator).update(parm1, parm2); 235 } 236 237 public void update(_DatabaseUser parm1, int[] parm2, Object [] parm3) throws 238 DException { 239 ( (_UserTableOperations) iterator).update(parm1, parm2, parm3); 240 } 241 242 public void update(Object parm1) throws DException { 243 ( (_TableOperations) iterator).update(parm1); 244 } 245 246 public void insert(_DatabaseUser parm1, Object parm2) throws DException { 247 ( (_UserTableOperations) iterator).insert(parm1, parm2); 248 } 249 250 public void insert(Object parm1) throws DException { 251 ( (_TableOperations) iterator).insert(parm1); 252 } 253 254 public void update(int[] parm1, Object [] parm2) throws DException { 255 ( (_TableOperations) iterator).update(parm1, parm2); 256 } 257 258 public void delete() throws DException { 259 ( (_TableOperations) iterator).delete(); 260 } 261 262 public void delete(_DatabaseUser parm1) throws DException { 263 ( (_UserTableOperations) iterator).delete(parm1); 264 } 265 266 public SuperComparator getComparator() { 267 return ( (_IndexIteratorInfo) iterator).getComparator(); 268 } 269 270 public void ensureRecordInMemory() throws com.daffodilwoods.database.resource. 271 DException { 272 ( (_IndexIteratorInfo) iterator).ensureRecordInMemory(); 273 } 274 275 public Object getActualKey() throws com.daffodilwoods.database.resource. 276 DException { 277 return ( (_IndexIteratorInfo) iterator).getActualKey(); 278 } 279 280 public int getBtreeIndex() throws com.daffodilwoods.database.resource. 281 DException { 282 return ( (_IndexIteratorInfo) iterator).getBtreeIndex(); 283 } 284 285 public SuperComparator getObjectComparator() throws DException { 286 return ( (_IndexIteratorInfo) iterator).getObjectComparator(); 287 } 288 289 public Object getPhysicalAddress() throws com.daffodilwoods.database.resource. 290 DException { 291 return ( (_IndexIteratorInfo) iterator).getPhysicalAddress(); 292 } 293 294 public boolean locateKey(Object parm1, boolean parm2) throws DException { 295 int a1 = Math.abs(Arrays.binarySearch(list, iterator.getColumnValues(column), 296 comparator)); 297 if ( ( (_IndexIteratorInfo) iterator).locateKey(parm1, parm2)) 298 return a1 < 0 ? 299 parm2 ? next() : previous() : 300 true; 301 return false; 302 } 303 304 public void moveOnActualKey(Object parm1) throws DException { 305 ( (_IndexIteratorInfo) iterator).moveOnActualKey(parm1); 306 } 307 308 public boolean seekKeyAddress(Object parm1) throws com.daffodilwoods.database. 309 resource.DException { 310 throw new java.lang.UnsupportedOperationException ( 311 "Method seekKeyAddress() not yet implemented."); 312 } 313 314 public void setConditionVariableValue(_Reference[] parm1, Object [] parm2, 315 int parm3) throws com.daffodilwoods. 316 database.resource.DException { 317 vv.setConditionVariableValue(parm1, parm2, parm3); 318 super.setConditionVariableValue(parm1, parm2, parm3); 319 getValues(); 320 } 321 322 private void getValues() throws DException { 323 324 Object ob1 = inlist.run(vv); 325 Object [] obj = ob1 instanceof Object [] ? (Object []) ob1 : new Object [] { 326 ob1}; 327 328 ArrayList values = new ArrayList (); 329 330 for (int i = 0; i < obj.length; i++) { 331 if (!values.contains(obj[i])) 332 values.add(obj[i]); 333 } 334 Collator collator = ( (ColumnDetails) column).getTableDetails(). 335 getColumnCharacteristics().getCollator(); 336 int type = 0; 337 FieldBase fb = null; 338 for (int i = values.size(); i-- > 0; ) { 339 try { 340 Object obj1 = values.get(i); 341 fb= (FieldBase) ((obj1 instanceof Object []) ? ((Object [])obj1)[0] :obj1); 342 type = fb.getDatatype(); 343 if (type == -1) 344 fb.setDatatype(type); 345 346 type = fb.getDatatype(); 347 348 if (type != column.getDatatype()) { 349 350 if (GetByteComparator.getAppropriateComparator(column.getDatatype(), 351 type, session, 352 collator).compare(FieldUtility. 353 convertToAppropriateType(fb, column.getDatatype(), 354 column.getSize(), collator), fb) != 0) { 355 values.remove(i); 356 } 357 else { 358 values.set(i, 359 FieldUtility.convertToAppropriateType(fb, 360 column.getDatatype(), 361 column.getSize(), collator)); 362 } 363 } 364 } 365 catch (DException ex) { 366 367 values.remove(i); 368 } 369 } 370 list = (FieldBase[]) values.toArray(new FieldBase[0]); 371 Arrays.sort(list, comparator); 372 } 373 374 private void getValuesOLD() throws DException { 375 376 Object ob1 = inlist.run(vv); 377 Object [] obj = ob1 instanceof Object [] ? (Object []) ob1 : new Object [] { 378 ob1}; 379 380 ArrayList values = new ArrayList (); 381 382 for (int i = 0; i < obj.length; i++) { 383 if (!values.contains(obj[i])) 384 values.add(obj[i]); 385 386 } 387 398 Collator collator = ( (ColumnDetails) column).getTableDetails(). 399 getColumnCharacteristics().getCollator(); 400 for (int i = values.size(); i-- > 0; ) { 401 try { 402 values.set(i, FieldUtility.convertToAppropriateType( (FieldBase) values. 403 get(i), column.getDatatype(), column.getSize(), collator)); 404 } 405 catch (DException ex) { 406 407 values.remove(i); 408 } 409 } 410 list = (FieldBase[]) values.toArray(new FieldBase[0]); 411 Arrays.sort(list, comparator); 412 } 413 414 public boolean seek(Object parm1) throws DException { 415 Object obj = parm1 instanceof Object [] ? ( (Object []) parm1)[0] : parm1; 416 417 int a = Arrays.binarySearch(list, obj, comparator); 418 if (a < 0) 419 return false; 420 else { 421 if (iterator.seek(parm1)) { 422 state = VALIDSTATE; 423 pos = a; 424 return true; 425 } 426 return false; 427 } 428 } 429 430 public boolean seekFromBottom(_IndexPredicate[] parm1) throws DException { 431 int a = Arrays.binarySearch(list, parm1, comparator); 432 if (a < 0) { 433 return false; 434 } 435 else { 436 if (iterator.seekFromBottom(new _IndexPredicate[] {parm1[0]})) { 437 state = VALIDSTATE; 438 pos = a; 439 return true; 440 } 441 return false; 442 } 443 } 444 445 public boolean seekFromBottomRelative(Object parm1) throws DException { 446 Object obj = parm1 instanceof Object [] ? ( (Object []) parm1)[0] : parm1; 447 int a = Arrays.binarySearch(list, parm1, comparator); 448 if (a < 0) 449 return false; 450 else { 451 if (iterator.seekFromBottomRelative(obj)) { 452 state = VALIDSTATE; 453 pos = a; 454 return true; 455 } 456 if (!alignBackward()) { 457 state = BEFOREFIRST; 458 return false; 459 } 460 state=VALIDSTATE; 461 return true; 462 } 463 } 464 465 public boolean seekFromTop(_IndexPredicate[] parm1) throws DException { 466 int a = Arrays.binarySearch(list, parm1, comparator); 467 if (a < 0) { 468 return false; 469 } 470 else { 471 if (iterator.seekFromTop(new _IndexPredicate[] {parm1[0]})) { 472 state = VALIDSTATE; 473 pos = a; 474 return true; 475 } 476 return false; 477 } 478 } 479 480 public boolean seekFromTopRelative(Object parm1) throws DException { 481 Object obj = parm1 instanceof Object [] ? ( (Object []) parm1)[0] : parm1; 482 int a = Arrays.binarySearch(list, parm1, comparator); 483 if (a < 0) 484 return false; 485 else { 486 if (iterator.seekFromTopRelative(obj)) { 487 state = VALIDSTATE; 488 pos = a; 489 return true; 490 } 491 if (!alignForward()) { 492 state = AFTERLAST; 493 return false; 494 } 495 state = VALIDSTATE; 496 return true; 497 } 498 } 499 public void deleteBlobClobRecord(_DatabaseUser user) throws DException { 500 ( (_TableOperations) iterator).deleteBlobClobRecord(user) ; 501 } 502 503 } 504 | Popular Tags |