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.database.utility.*; 7 import com.daffodilwoods.daffodildb.utils.GetByteComparator; 8 9 19 20 public abstract class RankAbstractNavigation extends FTException { 21 22 28 public abstract boolean alignLocationForward() throws DException; 29 public abstract boolean alignLocationBackward() throws DException; 30 31 40 41 private boolean alignFullTextAndLocationsForward() throws DException { 42 if (!alignFullTextForward()) { 43 return false; 44 } 45 return alignLocationForward() ? true : next(); 46 } 47 48 57 59 68 private boolean alignFullTextAndLocationsBackward() throws DException { 69 if (!alignFullTextBackward()) 70 return false; 71 return alignLocationBackward() ? true : previous(); 72 } 73 74 83 84 public static boolean testing; 85 86 public boolean first() throws DException { 87 testing = true; 88 for(int i = 0 ; i < fullTextIndex.length ; i++) 89 if(!fullTextIndex[i].first()) { 90 return false; 91 } 92 boolean b = alignFullTextAndLocationsForward(); 93 return b; 94 } 95 96 public boolean next() throws DException { 97 for(int i = 0 ; i < fullTextIndex.length ; i++) 98 if(!fullTextIndex[i].next()) { 99 return false; 100 } 101 else { 102 } 103 boolean b = alignFullTextAndLocationsForward(); 104 return b; 105 } 106 107 public boolean last() throws DException { 108 testing = true; 109 for(int i = 0 ; i < fullTextIndex.length ; i++) 110 if(!fullTextIndex[i].last()) { 111 return false; 112 } 113 return alignFullTextAndLocationsBackward(); 114 } 115 116 public boolean previous() throws DException { 117 for(int i = 0 ; i < fullTextIndex.length ; i++) 118 if(!fullTextIndex[i].previous()) 119 return false; 120 return alignFullTextAndLocationsBackward(); 121 } 122 123 124 134 public boolean alignFullTextForward() throws DException { 135 int maxIndex = getMaxIndex(); 136 return (maxIndex == -1) ? true : alignForward(maxIndex); 137 } 138 139 146 private int getMaxIndex() throws DException { 147 Object value = fullTextIndex[0].getColumnValues(documentIdColumn); 148 Object maxValue = value; 149 int maxIndex = 0; 150 boolean haveSameDocumentId = true; 151 for(int i = 1; i < fullTextIndex.length; i++){ 152 Object nValue = fullTextIndex[i].getColumnValues(documentIdColumn); 153 int cmp = compare(value,nValue); 154 if( cmp < 0){ 155 maxIndex = i; 156 maxValue = nValue; 157 } 158 else if ( cmp > 0) 159 haveSameDocumentId = false; 160 } 161 if(haveSameDocumentId && maxValue == value) 162 return -1; 163 return maxIndex; 164 } 165 166 private int getMinIndex() throws DException { 167 Object value = fullTextIndex[0].getColumnValues(documentIdColumn); 168 Object maxValue = value; 169 int maxIndex = 0; 170 boolean haveSameDocumentId = true; 171 for(int i = 1; i < fullTextIndex.length; i++){ 172 Object nValue = fullTextIndex[i].getColumnValues(documentIdColumn); 173 int cmp = compare(value,nValue); 174 if( cmp > 0){ 175 maxIndex = i; 176 maxValue = nValue; 177 } 178 else if ( cmp < 0) 179 haveSameDocumentId = false; 180 } 181 if(haveSameDocumentId && maxValue == value) 182 return -1; 183 return maxIndex; 184 } 185 186 195 private boolean alignForward(int index) throws DException { 196 Object value = fullTextIndex[index].getColumnValues(documentIdColumn); 197 while(true){ 198 for(int i = 0; i < fullTextIndex.length; i++){ 199 if(compare(value,fullTextIndex[i].getColumnValues(documentIdColumn)) == 0) 200 continue; 201 if(!fullTextIndex[i].seekFromTopRelative(new Object [] {keyWords[i], value} )) 202 return false; 203 Object nValue = fullTextIndex[i].getColumnValues(documentIdColumn); 204 value = compare(nValue,value) > 0 ? nValue : value; 205 } 206 boolean b = haveSameDocumentId(); 207 if(b) 208 return true; 209 } 210 } 211 212 222 public boolean alignFullTextBackward() throws DException { 223 int maxIndex = getMinIndex(); 224 return (maxIndex == -1) ? true : alignBackward(maxIndex); 225 } 226 227 236 private boolean alignBackward(int index) throws DException { 237 Object value = fullTextIndex[index].getColumnValues(documentIdColumn); 238 while(true){ 239 for(int i = 0; i < fullTextIndex.length; i++){ 240 if(compare(value,fullTextIndex[i].getColumnValues(documentIdColumn)) == 0) 241 continue; 242 if(!fullTextIndex[i].seekFromBottomRelative(new Object [] {keyWords[i], value} )) 243 return false; 244 Object nValue = fullTextIndex[i].getColumnValues(documentIdColumn); 245 value = compare(nValue,value) < 0 ? nValue : value; 246 } 247 boolean b = haveSameDocumentId(); 248 if(b) 249 return true; 250 } 251 } 252 253 263 public Object [] setValues() throws DException { 264 int length = fullTextIndex.length; 265 Object [] pkValues = new Object [length]; 266 for(int i = 0; i < length; i++){ 267 pkValues[i] = fullTextIndex[i].getColumnValues(pkColumn); 268 locationIndex[i].setConditionVariableValue(new _Reference[]{fpkReference[i]} , new Object [] {pkValues[i]}, 0); 269 } 270 return pkValues; 271 } 272 273 281 public FieldBase[] fields(_Reference[] references) throws com.daffodilwoods.database.resource.DException { 282 FieldBase[] fbsToRet = new FieldBase[references.length]; 283 for (int i = 0; i < references.length; i++) { 284 fbsToRet[i] = field(references[i]); 285 } 286 return fbsToRet; 287 } 288 289 public FieldBase[] fields(int[] columns) throws com.daffodilwoods.database.resource.DException { 290 return (FieldBase[]) getColumnValues(columns); 291 } 292 293 private boolean haveSameDocumentId() throws DException { 294 Object value = fullTextIndex[0].getColumnValues(documentIdColumn); 295 for (int i = 1; i < fullTextIndex.length; i++) { 296 Object nValue = fullTextIndex[i].getColumnValues(documentIdColumn); 297 if ( compare(value , nValue) != 0) 298 return false; 299 } 300 return true; 301 } 302 303 public int compare(Object ob1, Object ob2 ) throws DException{ 304 return GetByteComparator.ftComparator.compare(ob1,ob2); 305 } 306 public Object getColumnValues() throws DException { 307 return fullTextIndex[0].getColumnValues(); 308 } 309 310 public Object getColumnValues(int[] columns) throws DException { 311 return fullTextIndex[0].getColumnValues(columns); 312 } 313 314 public Object getColumnValues(_Reference[] references) throws DException { 315 Object [] fbsToRet = new Object [references.length]; 316 for (int i = 0; i < references.length; i++) { 317 fbsToRet[i] = getColumnValues(references[i]); 318 } 319 return fbsToRet; 320 } 321 322 public Object getColumnValues(_Reference reference) throws DException { 323 return fullTextIndex[0].getColumnValues(reference); 324 } 325 326 public FieldBase field(_Reference reference) throws com.daffodilwoods.database.resource.DException { 327 return fullTextIndex[0].field(reference); 328 } 329 } 330 | Popular Tags |