KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > access > btree > BranchRow


1 /*
2
3    Derby - Class org.apache.derby.impl.store.access.btree.BranchRow
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.store.access.btree;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25 import org.apache.derby.iapi.services.io.Storable;
26
27 import org.apache.derby.iapi.error.StandardException;
28
29 import org.apache.derby.iapi.store.access.RowUtil;
30
31 import org.apache.derby.iapi.store.raw.ContainerHandle;
32
33 import org.apache.derby.iapi.types.DataValueDescriptor;
34
35 import org.apache.derby.iapi.types.SQLLongint;
36 import org.apache.derby.iapi.services.io.FormatableBitSet;
37
38
39 /**
40  * Implements row which is stored in the branch pages of a btree. A non-suffix
41  * compressed branch row contains all of the columns of the leaf rows of a btree
42  * and contains an additional field at the end. The extra field of a branch row
43  * in a branch page at level N, is the child page field pointing the page at
44  * level N-1 which has keys which follow or equal the branch row entry.
45  *
46  * There are 3 ways to use this class to produce a branch row:
47  * createEmptyTemplate()
48  * creates a empty row template
49  * createBranchRowFromOldBranchRow()
50  * creates a new row with reference to an old branch row.
51  * createBranchRowFromOldLeafRow()
52  * creates a new row with reference to an old leaf row.
53  */

54
55 public class BranchRow
56 {
57     /* a dummy page number value (should not be compressable) */
58     public static final long DUMMY_PAGE_NUMBER = 0xffffffffffffffffL;
59
60     /**
61      * The branch child page pointer. All keys that Follow or equal the
62      * key in this row can be found by following the child page pointer.
63      * A reference to this object will be placed in the last slot of branchrow,
64      * and this class expects that no-one will replace that reference.
65      */

66     // private SQLLongint child_page = null;
67

68     /**
69      * The array of object to be used as the row.
70      */

71     private DataValueDescriptor[] branchrow = null;
72
73     /*
74     ** Constructors of BranchRow
75     */

76
77     /**
78     Constuctor for creating an "empty" BranchRow template, suitable for reading
79     in a branchRow from disk.
80     **/

81     private BranchRow()
82     {
83     }
84
85     private BranchRow(BTree btree)
86         throws StandardException
87     {
88         SQLLongint child_page =
89             new SQLLongint(ContainerHandle.INVALID_PAGE_NUMBER);
90
91         branchrow = btree.createBranchTemplate(child_page);
92
93         if (SanityManager.DEBUG)
94         {
95             SanityManager.ASSERT(
96                 child_page == ((SQLLongint) branchrow[branchrow.length - 1]));
97         }
98     }
99
100     /*
101     ** The following methods implement the BranchRow Private interface.
102     */

103
104     /**
105      * Accessor for the child page field of the branch row.
106      *
107      * @return The child page object.
108      **/

109     private SQLLongint getChildPage()
110     {
111         // last column of branch row should be the child page pointer.
112
if (SanityManager.DEBUG)
113         {
114             SanityManager.ASSERT(branchrow != null);
115             SanityManager.ASSERT(
116                 branchrow[branchrow.length - 1] instanceof SQLLongint);
117         }
118
119         return((SQLLongint) branchrow[branchrow.length - 1]);
120     }
121
122     /*
123     ** The following methods implement the BranchRow Public interface.
124     */

125
126     /**
127      * Create an empty branch row template suitable for reading branch rows in
128      * from disk. This routine will create newly allocated "empty" objects for
129      * every column in the template row.
130      *
131      * @exception StandardException Standard exception policy.
132      */

133     public static BranchRow createEmptyTemplate(BTree btree)
134         throws StandardException
135     {
136         BranchRow newbranch = new BranchRow(btree);
137
138         return(new BranchRow(btree));
139     }
140
141     /**
142      * Create a new branch row, given a old branch row and a new child page.
143      * Used by BranchControlRow to manufacture new branch rows when splitting
144      * or growing the tree.
145      *
146      * There is no way to "copy" values of a template row, so this class just
147      * stores a reference to each of the columns of the Indexable row passed
148      * in. This is ok as all
149      * usages of this class when instantiated this way, have an old branch row
150      * from which they are creating a new branch row with the same key values,
151      * and a different child page number.
152      *
153      * WARNING - this branch row is only valid while the old branch row is
154      * valid, as it contains references to the columns of the old branch row.
155      * So use of the row should only provide read-only access to the objects
156      * of the old branch row which are referenced.
157      */

158     public BranchRow createBranchRowFromOldBranchRow(long childpageno)
159     {
160         BranchRow newbranch = new BranchRow();
161
162         /* create new object array, and shallow copy all object references
163          * from old branch row to new branch row.
164          */

165
166         newbranch.branchrow = new DataValueDescriptor[this.branchrow.length];
167         System.arraycopy(
168             this.branchrow, 0, newbranch.branchrow, 0,
169             newbranch.branchrow.length - 1);
170
171         /* now create a different child page pointer object and place it as
172          * last column in the new branch row.
173          */

174         newbranch.branchrow[newbranch.branchrow.length - 1] =
175             new SQLLongint(childpageno);
176
177         return(newbranch);
178     }
179
180     /**
181      * Create a new branch row, given a old leaf row and a new child page.
182      * Used by LeafControlRow to manufacture new branch rows when splitting
183      * or growing the tree.
184      *
185      * There is no way to "copy" values of a template row, so this class just
186      * stores a referece to the Indexable row passed in. This is ok as all
187      * usages of this class when instantiated this way, have an old leaf row
188      * from which they are creating a new branch row with the same key values,
189      * and a different child page number.
190      *
191      * WARNING - this branch row is only valid while the old leaf row is
192      * valid, as it contains references to the columns of the old leaf row.
193      * So use of the row should only provide read-only access to the objects
194      * of the old leaf row which are referenced.
195      */

196     public static BranchRow createBranchRowFromOldLeafRow(
197     DataValueDescriptor[] leafrow,
198     long childpageno)
199     {
200         BranchRow newbranch = new BranchRow();
201
202         /* create new object array for the row, and copy all object references
203          * from old leaf row to new branch row.
204          */

205         newbranch.branchrow = new DataValueDescriptor[leafrow.length + 1];
206
207         System.arraycopy(leafrow, 0, newbranch.branchrow, 0, leafrow.length);
208
209         /* now create a different child page pointer object and place it as
210          * last column in the new branch row.
211          */

212         newbranch.branchrow[newbranch.branchrow.length - 1] =
213             new SQLLongint(childpageno);
214
215         return(newbranch);
216     }
217
218     /**
219      * Return the branch row.
220      * <p>
221      * Return the DataValueDescriptor array that represents the branch row,
222      * for use in raw store calls to fetch, insert, and update.
223      * <p>
224      *
225      * @return The branch row object array.
226      **/

227     protected DataValueDescriptor[] getRow()
228     {
229         return(this.branchrow);
230     }
231
232     /**
233      * Set the page number field of the branch row to a new value.
234      *
235      * @param page_number the new page number.
236      **/

237     protected void setPageNumber(long page_number)
238     {
239         getChildPage().setValue(page_number);
240     }
241
242
243     public String JavaDoc toString()
244     {
245         if (SanityManager.DEBUG)
246         {
247             return(
248                 RowUtil.toString(branchrow) +
249                 "child page: (" + getChildPage() + ")");
250         }
251         else
252         {
253             return(null);
254         }
255     }
256 }
257
Popular Tags