1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table; 2 3 import com.daffodilwoods.daffodildb.client.*; 4 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record; 5 import com.daffodilwoods.daffodildb.server.sql99.common.*; 6 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*; 7 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 8 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 9 import com.daffodilwoods.daffodildb.utils.field.*; 10 import com.daffodilwoods.database.resource.*; 11 import com.daffodilwoods.database.sqlinitiator.*; 12 import com.daffodilwoods.database.utility.P; 13 14 25 public class TopIterator extends BaseSingleIterator { 26 27 30 int countLimit; 31 34 long position; 35 36 43 public TopIterator(_Iterator iterator0, FieldBase countLimit0) throws DException { 44 super(iterator0); 45 countLimit = countLimit0.getObject().hashCode(); 46 if (countLimit <= 0) { 47 throw new DException("DSE3552", null); 48 } 49 position = 0; 50 } 51 52 59 public boolean first() throws DException { 60 boolean flag = iterator.first(); 61 position = flag ? 1 : 0; 62 return flag; 63 } 64 65 78 public boolean last() throws DException { 79 boolean flag = true; 80 for (long i = position; i < countLimit && flag; i++) { 81 flag = iterator.next(); 82 } 83 84 85 if(!flag) 86 iterator.previous(); 87 88 position = countLimit; 89 return flag; 90 } 91 92 103 public boolean next() throws DException { 104 return position < countLimit 105 ? iterator.next() ? position++ > -1 : false 106 : false; 107 } 108 109 115 public boolean previous() throws DException { 116 if( position == countLimit ) 117 return true; 118 119 boolean flag = iterator.previous(); 120 position = flag ? (position - 1) : 0; 121 return flag; 122 } 123 124 132 public void move(Object key) throws DException { 133 Object oldKey = iterator.getKey(); 134 int tempPosition = 0; 135 boolean match = false; 136 if (iterator.first()) { 137 do { 138 if (iterator.getKey().equals(key)) { 139 match = true; 140 } 141 tempPosition++; 142 } while (!match && tempPosition < countLimit && iterator.next()); 143 } 144 if (match) { 145 position = tempPosition; 146 } else { 147 iterator.move(oldKey); 148 } 149 } 150 151 158 159 public Object getColumnValues(int[] columns) throws DException { 160 if (position < 1 && position > countLimit) { 161 throw new DException("DSE3553", null); 162 } 163 return iterator.getColumnValues(columns); 164 } 165 166 public Object getColumnValues() throws DException { 167 if (position < 1 && position > countLimit) { 168 throw new DException("DSE3553", null); 169 } 170 return iterator.getColumnValues(); 171 } 172 173 public Object getColumnValues(_Reference[] reference) throws DException { 174 if (position < 1 && position > countLimit) { 175 throw new DException("DSE3553", null); 176 } 177 return iterator.getColumnValues(reference); 178 } 179 180 public Object getColumnValues(_Reference reference) throws DException { 181 if (position < 1 && position > countLimit) { 182 throw new DException("DSE3553", null); 183 } 184 return iterator.getColumnValues(reference); 185 } 186 187 public _Record getRecord() throws DException { 188 if (position < 1 && position > countLimit) { 189 throw new DException("DSE3553", null); 190 } 191 return iterator.getRecord(); 192 } 193 194 199 public _ExecutionPlan getExecutionPlan() throws DException { 200 _ExecutionPlan plan = iterator.getExecutionPlan(); 201 _ExecutionPlan cplans[] = plan == null ? null : new _ExecutionPlan[] {plan}; 202 return new ExecutionPlan("TopIterator", cplans, null, null, null); 203 } 204 205 210 public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException { 211 int length = 1; 212 ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[length]; 213 cplans[0] = iterator.getExecutionPlanForBrowser(); 214 return new ExecutionPlanForBrowser("Top " + countLimit, "Top Iterator", cplans, null, null, null); 215 } 216 217 221 222 public _OrderCount getOrderCounts() throws DException { 223 return iterator.getOrderCounts(); 224 } 225 226 public TableDetails[] getTableDetails() throws DException { 227 return iterator.getTableDetails(); 228 } 229 230 public _Iterator getBaseIterator(ColumnDetails column) throws DException { 231 232 return this; 233 } 234 235 236 public void setKeyCount(Object [][] tableAndKeyCount) throws DException { 237 iterator.setKeyCount(tableAndKeyCount); 238 } 239 240 public void setConditionVariableValue(_Reference[] references, Object [] values, int priority) throws DException { 241 if(underlyingRef!=null){ 242 references = GeneralPurposeStaticClass.getJointReferences(references,underlyingRef); 243 values = GeneralPurposeStaticClass.getJointValues(this, values,underlyingRef.length); 244 } 245 246 iterator.setConditionVariableValue(references, values, priority); 247 } 248 249 public _KeyColumnInformation[] getKeyColumnInformations() throws DException { 250 return iterator.getKeyColumnInformations(); 251 } 252 253 public Object [] getUniqueColumnReference() throws DException { 254 return iterator.getUniqueColumnReference(); 255 } 256 257 public _Order getDefaultOrder() throws DException { 258 return iterator.getDefaultOrder(); 259 } 260 261 public boolean seekFromTop(_IndexPredicate[] condition) throws DException { 262 return iterator.seekFromTop(condition); 263 } 264 265 public boolean seekFromTopRelative(Object indexKey) throws DException { 266 return iterator.seekFromTopRelative(indexKey); 267 } 268 269 public boolean seekFromBottom(_IndexPredicate[] condition) throws DException { 270 return iterator.seekFromBottom(condition); 271 } 272 273 public boolean seekFromBottomRelative(Object indexKey) throws DException { 274 return iterator.seekFromBottomRelative(indexKey); 275 } 276 277 public boolean seek(Object indexKey) throws DException { 278 return iterator.seek(indexKey); 279 } 280 281 282 public Object getKey() throws DException { 283 return iterator.getKey(); 284 } 285 } 286 | Popular Tags |