View Javadoc

1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  
21  package org.apache.directory.mavibot.btree;
22  
23  
24  import java.io.IOException;
25  
26  
27  /**
28   * A class containing utility methods to be used internally. 
29   *
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   */
32  @SuppressWarnings("all")
33  /* No qualifier */class InternalUtil
34  {
35  
36      /**
37       * Sets the multi-value container(a.k.a dupsContainer) of the key at the given position.
38       * 
39       * This method will not update the existing value of 'dupsPos'. To change this value
40       * use {@link #changeNextDupsContainer(ParentPos, BTree)} or {@link #changePrevDupsContainer(ParentPos, BTree)}
41       *  
42       * @param parentPos the parent position object
43       * @param btree the BTree
44       */
45      public static void setDupsContainer( ParentPos parentPos, BTree btree )
46      {
47          if ( !btree.isAllowDuplicates() )
48          {
49              return;
50          }
51  
52          try
53          {
54              if ( parentPos.dupsContainer == null )
55              {
56                  Leaf leaf = ( Leaf ) ( parentPos.page );
57                  BTree dupsContainer = ( BTree ) leaf.values[parentPos.pos].getValue( btree );
58                  parentPos.dupsContainer = dupsContainer;
59              }
60          }
61          catch ( IOException e )
62          {
63              throw new RuntimeException( e );
64          }
65      }
66  
67  
68      /**
69       * Sets the multi-value container(a.k.a dupsContainer) of the key at the given position
70       * and resets the 'dupsPos' to zero. This is mostly used by Cursor while navigating using
71       * next() 
72       *
73       * @param parentPos the parent position object
74       * @param btree the BTree
75       */
76      public static void changeNextDupsContainer( ParentPos parentPos, BTree btree ) throws IOException
77      {
78          if ( !btree.isAllowDuplicates() )
79          {
80              return;
81          }
82  
83          if ( parentPos.pos < parentPos.page.getNbElems() )
84          {
85              Leaf leaf = ( Leaf ) ( parentPos.page );
86              BTree dupsContainer = ( BTree ) leaf.values[parentPos.pos].getValue( btree );
87              parentPos.dupsContainer = dupsContainer;
88              parentPos.dupPos = 0;
89          }
90      }
91  
92  
93      /**
94       * Sets the multi-value container(a.k.a dupsContainer) of the key at the index below the given position( i.e pos - 1)
95       * and resets the 'dupsPos' to the number of elements present in the multi-value container.
96       * This is used by Cursor while navigating using prev() 
97       *
98       * @param parentPos the parent position object
99       * @param btree the BTree
100      */
101     public static void changePrevDupsContainer( ParentPos parentPos, BTree btree ) throws IOException
102     {
103         if ( !btree.isAllowDuplicates() )
104         {
105             return;
106         }
107 
108         int index = parentPos.pos - 1;
109         if ( index >= 0 )
110         {
111             Leaf leaf = ( Leaf ) ( parentPos.page );
112             BTree dupsContainer = ( BTree ) leaf.values[index].getValue( btree );
113             parentPos.dupsContainer = dupsContainer;
114             parentPos.dupPos = ( int ) parentPos.dupsContainer.getNbElems();
115         }
116     }
117 
118 
119     /**
120      * Same as @see #changePrevDupsContainer(ParentPos, BTree) but with a different name
121      * to make it sound semantically right when used inside {@link BTreeFactory#getPathToRightMostLeaf(BTree)} 
122      */
123     public static void setLastDupsContainer( ParentPos parentPos, BTree btree ) throws IOException
124     {
125         changePrevDupsContainer( parentPos, btree );
126     }
127 }