| 1 package com.daffodilwoods.daffodildb.server.sql99.fulltext.dql.iterator; 2 3 import com.daffodilwoods.database.resource.DException; 4 import com.daffodilwoods.daffodildb.utils.field.FieldBase; 5 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference; 6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._Iterator; 7 import com.daffodilwoods.daffodildb.server.sql99.common.GeneralPurposeStaticClass; 8 import com.daffodilwoods.database.utility.*; 9 import com.daffodilwoods.daffodildb.utils.GetByteComparator; 10 import java.util.Arrays ; 11 12 23 public class AnyKeyWordsIterator extends AnyWordsAbstractIterator { 24 25 public AnyKeyWordsIterator(_Iterator[] fullTextIndex0,FieldBase[] keyWord0) throws DException { 26 fullTextIndex = fullTextIndex0; 27 pkColumn = GeneralPurposeStaticClass.getColumnDetails("pk"); 28 documentIdColumn = GeneralPurposeStaticClass.getColumnDetails("documentId"); 29 keyWords =keyWord0; 30 } 31 32 38 public boolean first() throws DException { 39 boolean allFalse = true; 40 for (int i = 0; i < fullTextIndex.length; i++) 41 if (fullTextIndex[i].first()) 42 allFalse = false; 43 direction = true; 44 if(allFalse) { 45 state = AFTERLAST; 46 return false; 47 } 48 initializeIteratorIndexWithSmallest(); 49 state = VALIDSTATE; 50 return true; 51 } 52 53 59 public boolean next() throws DException { 60 switch(state) { 61 case INVALIDSTATE : 62 throw new DException("DSE4116",null); 63 case BEFOREFIRST : 64 return first(); 65 case AFTERLAST : 66 return false; 67 } 68 boolean next = direction ? nextWithForwardDirection() : 69 nextWithOppositeDirection(); 70 state = next ? VALIDSTATE : AFTERLAST; 71 return next; 72 } 73 81 private boolean nextWithForwardDirection() throws DException { 82 boolean[] smallest = getSmallest(); 83 for (int i = 0; i < fullTextIndex.length; i++) 84 if (smallest[i]) 85 fullTextIndex[i].next(); 86 boolean allFalse = allFalse(); 87 if(allFalse) 88 return false; 89 initializeIteratorIndexWithSmallest(); 90 return true; 91 } 92 93 100 private boolean nextWithOppositeDirection() throws DException { 101 boolean allFalse = true; 102 for (int i = 0; i < fullTextIndex.length; i++) 103 if(fullTextIndex[i].next()) 104 allFalse = false; 105 if (allFalse) 106 return false; 107 if (allSameWithSmallest()) 108 return true; 109 boolean[] smallest = getSmallest(); 110 for (int i = 0; i < fullTextIndex.length; i++) 111 if (!smallest[i]) 112 fullTextIndex[i].previous(); 113 return true; 114 } 115 116 122 public boolean previous() throws DException { 123 switch (state) { 124 case INVALIDSTATE: 125 throw new DException("DSE4116", null); 126 case AFTERLAST: 127 return last(); 128 case BEFOREFIRST: 129 return false; 130 } 131 boolean prev = !direction ? previousWithBackwardDirection() 132 : previousWithOppositeDirection(); 133 state = prev ? VALIDSTATE : BEFOREFIRST; 134 return prev; 135 } 136 137 144 private boolean previousWithBackwardDirection() throws DException { 145 boolean[] largest = getLargest(); 146 for (int i = 0; i < fullTextIndex.length; i++){ 147 if (largest[i]) 148 fullTextIndex[i].previous(); 149 } 150 if(allFalse()) 151 return false; 152 initializeIteratorIndexWithLargest(); 153 return true; 154 } 155 156 163 private boolean previousWithOppositeDirection() throws DException { 164 boolean allFalse = true; 165 for (int i = 0; i < fullTextIndex.length; i++) 166 if(fullTextIndex[i].previous()) 167 allFalse = false; 168 if (allFalse) { 169 return false; 170 } 171 if (allSameWithLargest()) 172 return true; 173 boolean[] largest = getLargest(); 174 for (int i = 0; i < fullTextIndex.length; i++) 175 if (!largest[i]) 176 fullTextIndex[i].next(); 177 return true; 178 } 179 180 181 187 public boolean last() throws DException { 188 boolean allFalse = true; 189 for (int i = 0; i < fullTextIndex.length; i++) 190 if (fullTextIndex[i].last()) 191 allFalse = false; 192 direction = false; 193 if(allFalse) { 194 state = BEFOREFIRST; 195 return false; 196 } 197 initializeIteratorIndexWithLargest(); 198 state = VALIDSTATE; 199 return true; 200 } 201 202 208 private void initializeIteratorIndexWithSmallest() throws DException { 209 Object min = null; 210 for (int i = 0; i < fullTextIndex.length; i++) { 211 Object nValue = null; 212 try { 213 nValue = fullTextIndex[i].getColumnValues(documentIdColumn); 214 } 215 catch (DException ex) { 216 continue; 217 } 218 if (min == null) { 219 min = nValue; 220 iterIndex = i; 221 continue; 222 } 223 int cmp = compare(nValue, min); 224 if (cmp < 0) { 225 iterIndex = i; 226 min = nValue; 227 } 228 } 229 } 230 231 237 private void initializeIteratorIndexWithLargest() throws DException { 238 Object max = null; 239 for (int i = 0; i < fullTextIndex.length; i++) { 240 Object nValue = null; 241 try { 242 nValue = fullTextIndex[i].getColumnValues(documentIdColumn); 243 } 244 catch (DException ex) { 245 continue; 246 } 247 if (max == null) { 248 max = nValue; 249 iterIndex = i; 250 continue; 251 } 252 int cmp = compare(nValue, max); 253 if (cmp > 0) { 254 iterIndex = i; 255 max = nValue; 256 } 257 } 258 } 259 public void setSpecificUnderlyingReferences(_Reference[] specificUnderlyingReferences) throws DException{ 260 } 261 262 public String toString(){ 263 String str = "AnyKeyWordsIterator"; 264 str += "[FullTextIndex"+fullTextIndex[0] 265 +"]"; 266 return str; 267 } 268 } 269 | Popular Tags |