KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datasystem > btree > BTreeNavigator


1 package com.daffodilwoods.daffodildb.server.datasystem.btree;
2
3 import com.daffodilwoods.database.resource.DException;
4 import com.daffodilwoods.database.utility.P;
5 import com.daffodilwoods.daffodildb.utils.BufferRange;
6 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._Index;
7 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexKey;
8
9 /**
10  * BTreeNaviagtor is used to navigate and retrieve values from btree.
11  */

12
13 public abstract class BTreeNavigator {
14
15     /**
16      * current position of BTreeNavigator
17      */

18   protected _IndexKey currentKey = null;
19
20   protected _Index btree;
21
22   public int status = -1;
23
24   public BTreeNavigator(_Index btree0) {
25         btree = btree0;
26         currentKey = btree.keyInstance();
27   }
28
29   /**
30    * sets its current key as first key of Btree , if there is no data in btree then sets current key node
31    * null and position 0
32    * @return true if first key exists in Btree else false
33    */

34
35     public boolean first() throws DException{
36
37             boolean flag = btree.first(currentKey);
38             status = flag ? 0 : -1;
39             return flag;
40     }
41
42     /**
43      * sets its current key as next key of current key , if there is no next key in btree of current key
44      * then sets current key node null and position 0
45      * @return true if next key of current key exists in Btree else false
46      */

47
48     public boolean next() throws DException {
49       boolean flag = btree.next(currentKey);
50        status = flag ? 0 : 1;
51       return flag;
52     }
53
54     /**
55      * sets its current key as last key of Btree , if there is no data in btree then sets current key node
56      * null and position 0
57      * @return true if first key exists in Btree else false
58      */

59     public boolean last() throws DException{
60             boolean flag = btree.last(currentKey);
61             status = flag ? 0 : 1;
62             return flag;
63     }
64
65     /**
66      * sets its current key as previous key of current key , if there is no previous key in btree of
67      * current key then sets current key node null and position 0
68      *
69      * @return true if previous key of current key exists in Btree else false
70      */

71
72     public boolean previous() throws DException{
73             boolean flag = btree.previous(currentKey);
74             status = flag ? 0 : -1;
75             return flag;
76     }
77
78     public void move(Object JavaDoc key)throws DException{
79        currentKey = (_IndexKey)key;
80
81     }
82
83     /**
84      * returns key of BtreeElement at given position in given node, throws DException if element at given
85      * node and position does not exist
86      * @return key of BtreeElement at given position in given node
87      * @throws DException if current key is deleted
88      */

89
90
91     public abstract Object JavaDoc getKey()throws DException;
92
93     public abstract Object JavaDoc getValue()throws DException;
94
95
96
97    public abstract Object JavaDoc getCurrentKey() throws DException;
98
99    /**
100     * checks that btree contains given columnIndex, if this column exists in Btree then return value of
101     * given column index from btree otherwise Throws DException that value corresponding to given index
102     * can not be returned
103     *
104     * @param columnIndex whose corresponding value is required
105     * @return value corresponding to given columnIndex
106     */

107
108    public abstract Object JavaDoc getColumnValues(int columnIndex) throws DException;
109
110    /**
111     * checks that btree contains given columnIndexes, if all columns exist in Btree then returns values
112     * corresponding to given column indexes from btree otherwise Throws DException that value
113     * corresponding to given indexes can not be returned
114     *
115     * @param columnIndexes whose corresponding values are required
116     * @return values corresponding to given columnIndexes
117     */

118    public abstract Object JavaDoc getColumnValues(int[] columnIndexes) throws DException;
119
120
121   public void setNodeAndPosition(Object JavaDoc node, int position)throws DException{
122       currentKey.setNodeAndPosition(node,position);
123         status = (node == null || position == 0 ) ? -1 : 0;
124   }
125 }
126
Popular Tags