KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem;
2
3 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs;
4 import com.daffodilwoods.database.resource.*;
5 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
6
7 /**
8  * <p>Title: ClusterIterator</p>
9  * <p>Description: It is used for the navigation on different clusters related to a particular table.
10  * It is a iterator Specific class.
11  */

12
13 public class ClusterIterator {
14
15     /**
16      * current cluster of iterator
17      */

18
19    private Cluster currentCluster;
20
21     /**
22      * Table to get cluster
23      */

24
25    private PersistentTable table;
26
27     public ClusterIterator(PersistentTable table0) {
28         table = table0;
29     }
30
31     /**
32      * sets the current cluster of iterator on first cluster of table
33      * @returns true if first cluster of table exists
34      */

35
36      boolean first()throws DException {
37        currentCluster = table.getFirstCluster();
38         while (currentCluster!= null && currentCluster.activeRecordCount == 0) {
39            currentCluster = table.getNextCluster(currentCluster, null);
40          }
41             return (currentCluster) != null;
42      }
43     /**
44      * sets the current cluster of iterator on next cluster of current cluster
45      * if next cluster exists
46      *
47      * @return true if next cluster of current cluster exists
48      */

49
50     boolean next() throws DException {
51       try {
52         currentCluster = table.getNextCluster(currentCluster, null);
53            while (currentCluster!=null && currentCluster.activeRecordCount == 0) {
54              currentCluster = table.getNextCluster(currentCluster, null);
55           }
56              return (currentCluster) != null;
57       }
58       catch (NullPointerException JavaDoc ne) {
59         currentCluster = table.getClusterForRead(currentCluster.
60                                                  clusterCharacteristics);
61         return (currentCluster = table.getNextCluster(currentCluster, null)) != null;
62       }
63     }
64
65     /**
66      * sets the current cluster of iterator on last cluster of table
67      * @returns true if last cluster of table exists
68      */

69
70
71     boolean last() throws DException {
72       currentCluster = table.getLastCluster(null);
73         while (currentCluster!= null && currentCluster.activeRecordCount == 0) {
74           currentCluster = table.getPreviousCluster(currentCluster, null);
75         }
76           return (currentCluster) != null;
77     }
78
79     /**
80      * sets the current cluster of iterator on previous cluster of current cluster if previous cluster
81      * exists
82      * @return true if previous cluster of current cluster exists
83      */

84
85     boolean previous() throws DException {
86       try {
87         currentCluster = table.getPreviousCluster(currentCluster, null);
88          while (currentCluster!= null && currentCluster.activeRecordCount == 0) {
89             currentCluster = table.getPreviousCluster(currentCluster, null);
90           }
91            return (currentCluster) != null;
92       }
93       catch (NullPointerException JavaDoc ne) {
94         currentCluster = table.getClusterForRead(currentCluster.
95                                                  clusterCharacteristics);
96         return (currentCluster = table.getPreviousCluster(currentCluster, null)) != null;
97       }
98     }
99
100     /**
101      * returns current cluster of Iterator
102      *
103      * @return current cluster of Iterator
104      */

105
106      Cluster getCurrentCluster(){
107                return currentCluster;
108     }
109
110
111     /**
112      * moves iterator on cluster of given key
113      * @param key tablekey at which cluster iterator hase to move
114      */

115
116      void move(Object JavaDoc key) throws DException{
117                 TableKey tk = (TableKey) key;
118                 if (currentCluster != null &&
119                     currentCluster.clusterCharacteristics.getStartAddress() ==
120                     tk.getStartAddress()) {
121                   return;
122                 }
123                 Cluster cls = (Cluster) tk.cluster.get();
124                 if (cls == null) {
125                   cls = table.getClusterForRead(new ClusterCharacteristics(tk.getStartAddress(),false));
126                 }
127                 currentCluster = cls;
128     }
129
130     public int getNumberOfClustersUsed() throws DException {
131       int count = 0;
132       if (first()) {
133         do {
134           count++;
135         }
136         while (next());
137       }
138       return count;
139     }
140
141
142 }
143
Popular Tags