KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datasystem > persistentsystem > DatabaseUserTableIterator


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 JavaDoc;
10 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
11 import com.daffodilwoods.daffodildb.utils.BufferRange;
12
13
14 /**
15  *
16  * <p>Title: DatabaseUserTableIterator</p>
17  * <p>Description: Objective of DatabaseUserTableIterator is to perform operations on table.When more than
18  * one user wants to work on the table then each one is provided with the iterator .Every user can peform
19  * read and write operations on table through their respective iterators.
20  */

21 public class DatabaseUserTableIterator implements _TableIterator,_UserTableOperations{
22
23     /**
24      * DatabaseUserTable table on which Iterator navigates.
25      */

26    private DatabaseUserTable table;
27
28     /**
29      * CurrentKey maintains the current pointer of Iterator on table (i.e. Table Key)
30      *
31      */

32     private Object JavaDoc currentKey;
33
34     /**
35      * To get CurrentCluster
36      */

37
38    private ClusterIterator clusterIterator;
39
40     /**
41      * status = -1 ::::::: Invalid state before First
42      * status = 0 ::::::: Valid state
43      * status = 1 ::::::: Invalid state after Last
44      */

45
46    private int status;
47
48     /**
49      * fieldbases for current row.
50      */

51
52     public DatabaseUserTableIterator(DatabaseUserTable table0, ClusterIterator clusterIterator0) throws DException{
53         table = table0;
54         clusterIterator = clusterIterator0;
55         status = -1;
56     }
57
58     /**
59      *
60      * updates current key of Iterator on first valid Record of table if it exists otherwise null
61
62      * for first valid record we get current cluster and check its all record for its validity
63      * if any first valid record found than current key is updated with tableKey of that record
64      * and while we traversed all records of a cluster than key is not valid exception throughed
65      * and we catch it and get next cluster from cluster iterator and if it doesn't exists than we
66      * set status -1 and return false otherwise this cluster is again searched for first valid record.
67      * @return true if FirstKey exists otherwise false
68      */

69
70     public boolean first() throws DException {
71         if(clusterIterator.first()){
72                    short recordNumber = 1;
73                    Cluster cluster = clusterIterator.getCurrentCluster();
74                    WeakReference JavaDoc wk = new WeakReference JavaDoc(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 JavaDoc(cluster) ;
91                            }
92                        }
93                    }
94                }
95                return false;
96     }
97
98     /**
99      * updates current key of Iterator on LastRecord of table if it exists otherwise null
100      * for last valid record we get last current and check its all record for its validity
101      * if any first valid record found from last than current key is updated with tableKey of that record
102      * and while we traversed all records of a cluster than key is not valid exception throughed
103      * and we catch it and get previouse cluster from cluster iterator and if it doesn't exists than we
104      * set status -1 and return false otherwise this cluster is again searched for first valid record from last.
105
106      * @return true if LastKey exists otherwise false
107      */

108
109     public boolean last() throws DException {
110                if(clusterIterator.last()){
111                    Cluster cluster = clusterIterator.getCurrentCluster();
112                    WeakReference JavaDoc wk = new WeakReference JavaDoc(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 JavaDoc(cluster) ;
129                                recordNumber = cluster.actualRecordCount;
130                            }
131                        }
132                    }
133                }
134                return false;
135     }
136
137     /**
138      * updates current key of Iterator on nextKey of currentKey If Exists otherwise null
139      *
140      * working same as of first method.
141      * @return true if NextKey exists otherwise false
142      */

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 JavaDoc wk = Key.cluster.get() == null ? new WeakReference JavaDoc(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 JavaDoc(cluster);
167                          }
168
169                    }
170                }
171     }
172
173
174     /**
175      * updates current key of Iterator on PreviousKey of currentKey If Exists otherwise null
176      *
177      * working same as last method.
178      * @return true if PreviousKey exists otherwise false
179      */

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 JavaDoc wk = Key.cluster.get() == null ? new WeakReference JavaDoc(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 JavaDoc(cluster);
208                     }
209                 }
210             }
211     }
212
213     /**
214      * Returns the currentPointer of Iterator ( i.e. table key )
215      *
216      * @return currentPointer of Iterator
217      */

218
219     public Object JavaDoc getKey() throws DException {
220             return currentKey;
221     }
222
223     /**
224      * moves the current pointer of iterator on given key and also moves clusterIterator on given key
225      *
226      * @param key Table key on which iterator has to move
227      *
228      * @throws DException
229      */

230
231     public void move(Object JavaDoc key) throws DException {
232                currentKey = key;
233                clusterIterator.move(key);
234                status = 0;
235     }
236
237     /**
238      * returns the values of the desired columns at the current position from the table.
239      *
240      * @param columns columns whose corresponding values are required
241      *
242      * @return values of desired Columns
243      * @throws DException If iterator is on invalid State
244      */

245
246     public Object JavaDoc getColumnValues(int[] columns) throws DException {
247         if(status != 0)
248             throw new DException("DSE2019",new Object JavaDoc[] {new Integer JavaDoc(status)});
249         return table.getColumnObjects(currentKey,columns);
250     }
251
252     /**
253      * inserts Values in table and updates Current Key
254      *
255      * @param user to Perform write operation
256      * @param value values which has to insert
257      *
258      */

259
260     public void insert(_DatabaseUser user, Object JavaDoc value) throws DException {
261         currentKey = table.insert(user,value);
262         status = 0;
263         clusterIterator.move(currentKey);
264     }
265
266     /**
267      * updates Record at Currentkey
268      *
269      * @param user to Perform write operation
270      * @param columns columns whose values has to update
271      * @param value new values of columns
272      */

273
274     public void update(_DatabaseUser user, int[] columns, Object JavaDoc[] value) throws DException {
275         Object JavaDoc[] values = (Object JavaDoc[])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     /**
283      * update Records at CurrentKey
284      *
285      * @param user to Perform write operation
286      * @param value new values of record
287      */

288
289     public void update(_DatabaseUser user, Object JavaDoc value) throws DException {
290         currentKey = table.update(user,currentKey,value);
291         status = 0;
292     }
293
294     /**
295      * deletes Record at current key
296      *
297      * @param user to Perform write operations
298      */

299
300     public void delete(_DatabaseUser user) throws DException {
301         currentKey = table.delete(user,currentKey);
302         status = 0;
303     }
304
305     /**
306      * Checks whether current key is valid or not
307      *
308      * @throws DException if current key is inValid or deleted
309      */

310
311
312     /**
313      * To get record of current key seted.
314      * @throws DException
315      * @return _Record
316      */

317     public _Record getRecord() throws DException {
318         Record record = new Record(table.getTable().getTableCharacteristics(),null);
319         record.setObject((Object JavaDoc[])getColumnValues());
320         return record;
321     }
322     /**
323      * Sets parameter status and key
324      * @param status0 int - 0 -> valid -1 -> before first 1 -> after last
325      * @param key Object - tableKey to be seted.
326      * @param flag boolean - toReturn
327      * @return boolean
328      */

329     private boolean setKeyStatus(int status0,Object JavaDoc key,boolean flag) {
330         status = status0;
331         currentKey = key;
332         return flag;
333     }
334     /**
335      * Used for any verifing any unexpected results .
336      * @throws DException
337      * @return Object[] - returns all records of databaseUserTable.
338      */

339     public Object JavaDoc[] 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     /**
358      * returns the value of the desired column at the current position from the table.
359      *
360      * @param column column whose corresponding value is required
361      *
362      * @return value of desired Column
363      * @throws DException If iterator is on invalid State
364      */

365
366
367     public Object JavaDoc getColumnValues(int parm1) throws DException {
368         if(status != 0)
369             throw new DException("DSE2019",new Object JavaDoc[] {new Integer JavaDoc(status)});
370         return table.getColumnObjects(currentKey,parm1);
371     }
372
373     /**
374      * returns the values at the current position from the table.
375      *
376      * @return values at the current position from the table.
377      * @throws DException If iterator is on invalid State
378      */

379
380
381     public Object JavaDoc getColumnValues() throws DException {
382         if(status != 0)
383             throw new DException("DSE2019",new Object JavaDoc[] {new Integer JavaDoc(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 JavaDoc[] vals = columns == null ? (Object JavaDoc[])getTableCharacteristics().getObject(array)
397                                         : (Object JavaDoc[])getTableCharacteristics().getObject(columns,array);
398     }
399     public byte[] getByteKey() throws DException{
400       throw new java.lang.UnsupportedOperationException JavaDoc(
401         "Method getByteKey() not yet implemented."); }
402
403    public void moveByteKey(byte[] key) throws DException{
404      throw new java.lang.UnsupportedOperationException JavaDoc(
405         "Method moveByteKey() not yet implemented.");
406        }
407        /**
408         * Used in test cases of data system TestUpdateDeletedClusterInformation
409         * @param flag boolean
410         * @throws DException
411         * @return Object[]
412         */

413        public Object JavaDoc[] 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