1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem; 2 3 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 4 import com.daffodilwoods.daffodildb.server.datasystem.utility.*; 5 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 6 import com.daffodilwoods.database.resource.*; 7 import com.daffodilwoods.database.utility.*; 8 import java.util.*; 9 import java.lang.ref.WeakReference ; 10 import com.daffodilwoods.daffodildb.utils.field.FieldBase; 11 import com.daffodilwoods.daffodildb.utils.BufferRange; 12 13 14 21 public class DatabaseUserTableIterator implements _TableIterator,_UserTableOperations{ 22 23 26 private DatabaseUserTable table; 27 28 32 private Object currentKey; 33 34 37 38 private ClusterIterator clusterIterator; 39 40 45 46 private int status; 47 48 51 52 public DatabaseUserTableIterator(DatabaseUserTable table0, ClusterIterator clusterIterator0) throws DException{ 53 table = table0; 54 clusterIterator = clusterIterator0; 55 status = -1; 56 } 57 58 69 70 public boolean first() throws DException { 71 if(clusterIterator.first()){ 72 short recordNumber = 1; 73 Cluster cluster = clusterIterator.getCurrentCluster(); 74 WeakReference wk = new WeakReference (cluster) ; 75 while(true){ 76 int firstClusterAddress = cluster.getClusterCharacteristics().getStartAddress() ; 77 currentKey = new TableKey(wk,firstClusterAddress,recordNumber++); 78 try{ 79 table.checkValidity(currentKey); 80 status = 0; 81 return true; 82 } 83 catch(DException de){ 84 if(de.getDseCode().equalsIgnoreCase("DSE2002")){ 85 if(!clusterIterator.next()){ 86 return setKeyStatus(-1,null,false); 87 } 88 recordNumber = 1; 89 cluster = clusterIterator.getCurrentCluster(); 90 wk = new WeakReference (cluster) ; 91 } 92 } 93 } 94 } 95 return false; 96 } 97 98 108 109 public boolean last() throws DException { 110 if(clusterIterator.last()){ 111 Cluster cluster = clusterIterator.getCurrentCluster(); 112 WeakReference wk = new WeakReference (cluster) ; 113 short recordNumber = cluster.actualRecordCount; 114 while(true){ 115 int lastClusterAddress = cluster.clusterCharacteristics.getStartAddress() ; 116 currentKey = new TableKey(wk,lastClusterAddress,recordNumber--); 117 try{ 118 table.checkValidity(currentKey); 119 status = 0; 120 return true; 121 } 122 catch(DException de){ 123 if(de.getDseCode().equalsIgnoreCase("DSE2002")){ 124 if(!clusterIterator.previous()){ 125 return setKeyStatus(1,null,false); 126 } 127 cluster = clusterIterator.getCurrentCluster(); 128 wk = new WeakReference (cluster) ; 129 recordNumber = cluster.actualRecordCount; 130 } 131 } 132 } 133 } 134 return false; 135 } 136 137 143 144 public boolean next() throws DException { 145 if(status != 0) 146 return status == 1 ? false : first(); 147 TableKey Key = (TableKey)currentKey; 148 short recordNumber = (short)(Key.getRecordNumber() +(short)1); 149 Cluster cluster = clusterIterator.getCurrentCluster(); 150 WeakReference wk = Key.cluster.get() == null ? new WeakReference (cluster) : Key.cluster ; 151 while(true){ 152 int currentClusterAddress = cluster.clusterCharacteristics.getStartAddress() ; 153 currentKey = new TableKey(wk, currentClusterAddress,recordNumber++); 154 try{ 155 table.checkValidity(currentKey); 156 status = 0; 157 return true; 158 } 159 catch(DException de){ 160 if(de.getDseCode().equalsIgnoreCase("DSE2002")){ 161 if(!clusterIterator.next()){ 162 return setKeyStatus(1,null,false); 163 } 164 recordNumber = 1; 165 cluster = clusterIterator.getCurrentCluster() ; 166 wk = new WeakReference (cluster); 167 } 168 169 } 170 } 171 } 172 173 174 180 181 182 public boolean previous() throws DException { 183 if(status == -1) 184 return false; 185 if(status == 1) 186 return last(); 187 TableKey Key = (TableKey)currentKey; 188 short recordNumber = (short)(Key.getRecordNumber() -(short)1); 189 Cluster cluster = clusterIterator.getCurrentCluster(); 190 WeakReference wk = Key.cluster.get() == null ? new WeakReference (cluster) : Key.cluster ; 191 int currentClusterAddress = Key.getStartAddress() ; 192 while(true){ 193 currentKey = new TableKey(wk,currentClusterAddress,recordNumber--); 194 try{ 195 table.checkValidity(currentKey); 196 status = 0; 197 return true; 198 } 199 catch(DException de){ 200 if(de.getDseCode().equalsIgnoreCase("DSE2002")){ 201 if(!clusterIterator.previous()){ 202 return setKeyStatus(-1,null,false); 203 } 204 cluster = clusterIterator.getCurrentCluster(); 205 currentClusterAddress = cluster.clusterCharacteristics.getStartAddress(); 206 recordNumber = cluster.actualRecordCount; 207 wk = new WeakReference (cluster); 208 } 209 } 210 } 211 } 212 213 218 219 public Object getKey() throws DException { 220 return currentKey; 221 } 222 223 230 231 public void move(Object key) throws DException { 232 currentKey = key; 233 clusterIterator.move(key); 234 status = 0; 235 } 236 237 245 246 public Object getColumnValues(int[] columns) throws DException { 247 if(status != 0) 248 throw new DException("DSE2019",new Object [] {new Integer (status)}); 249 return table.getColumnObjects(currentKey,columns); 250 } 251 252 259 260 public void insert(_DatabaseUser user, Object value) throws DException { 261 currentKey = table.insert(user,value); 262 status = 0; 263 clusterIterator.move(currentKey); 264 } 265 266 273 274 public void update(_DatabaseUser user, int[] columns, Object [] value) throws DException { 275 Object [] values = (Object [])getColumnValues(); 276 for(int i = 0 ; i < columns.length ; i++) 277 values[columns[i]] = value[i]; 278 currentKey = table.update(user,currentKey,values); 279 status = 0; 280 } 281 282 288 289 public void update(_DatabaseUser user, Object value) throws DException { 290 currentKey = table.update(user,currentKey,value); 291 status = 0; 292 } 293 294 299 300 public void delete(_DatabaseUser user) throws DException { 301 currentKey = table.delete(user,currentKey); 302 status = 0; 303 } 304 305 310 311 312 317 public _Record getRecord() throws DException { 318 Record record = new Record(table.getTable().getTableCharacteristics(),null); 319 record.setObject((Object [])getColumnValues()); 320 return record; 321 } 322 329 private boolean setKeyStatus(int status0,Object key,boolean flag) { 330 status = status0; 331 currentKey = key; 332 return flag; 333 } 334 339 public Object [] show() throws DException{ 340 ArrayList list = new ArrayList(); 341 if(first()){ 342 _TableCharacteristics tc = table.getTable().getTableCharacteristics(); 343 do{ 344 BufferRange[] value = (BufferRange[])getColumnValues(); 345 list.add(value); 346 } while(next()); 347 }else 348 P.pln(" NO RECORD IN TABLE "); 349 return list.size() == 0 ? null : list.toArray(); 350 } 351 352 353 354 355 356 357 365 366 367 public Object getColumnValues(int parm1) throws DException { 368 if(status != 0) 369 throw new DException("DSE2019",new Object [] {new Integer (status)}); 370 return table.getColumnObjects(currentKey,parm1); 371 } 372 373 379 380 381 public Object getColumnValues() throws DException { 382 if(status != 0) 383 throw new DException("DSE2019",new Object [] {new Integer (status)}); 384 BufferRange[] buffers = (BufferRange[])table.getColumnObjects(currentKey); 385 return buffers; 386 } 387 388 389 390 391 392 public _TableCharacteristics getTableCharacteristics() throws DException{ 393 return table.getTableCharacteristics(); 394 } 395 public void print(int[] columns,BufferRange[] array ) throws DException{ 396 Object [] vals = columns == null ? (Object [])getTableCharacteristics().getObject(array) 397 : (Object [])getTableCharacteristics().getObject(columns,array); 398 } 399 public byte[] getByteKey() throws DException{ 400 throw new java.lang.UnsupportedOperationException ( 401 "Method getByteKey() not yet implemented."); } 402 403 public void moveByteKey(byte[] key) throws DException{ 404 throw new java.lang.UnsupportedOperationException ( 405 "Method moveByteKey() not yet implemented."); 406 } 407 413 public Object [] show(boolean flag) throws DException{ 414 ArrayList list = new ArrayList(); 415 if(first()){ 416 _TableCharacteristics tc = table.getTable().getTableCharacteristics(); 417 do{ 418 BufferRange[] value = (BufferRange[])getColumnValues(); 419 list.add(value); 420 } while(next()); 421 }else 422 P.pln(" NO RECORD IN TABLE "); 423 return list.size() == 0 ? null : list.toArray(); 424 } 425 426 427 } 428 | Popular Tags |