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.memory;
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 if ( parentPos.dupsContainer == null )
53 {
54 Leaf leaf = ( Leaf ) ( parentPos.page );
55 MultipleMemoryHolder mvHolder = ( MultipleMemoryHolder ) leaf.values[parentPos.pos];
56 if( !mvHolder.isSingleValue() )
57 {
58 BTree dupsContainer = ( BTree ) mvHolder.getValue( btree );
59 parentPos.dupsContainer = dupsContainer;
60 }
61 }
62 }
63
64
65 /**
66 * Sets the multi-value container(a.k.a dupsContainer) of the key at the given position
67 * and resets the 'dupsPos' to zero. This is mostly used by Cursor while navigating using
68 * next()
69 *
70 * @param parentPos the parent position object
71 * @param btree the BTree
72 */
73 public static void changeNextDupsContainer( ParentPos parentPos, BTree btree ) throws IOException
74 {
75 if ( !btree.isAllowDuplicates() )
76 {
77 return;
78 }
79
80 if ( parentPos.pos < parentPos.page.getNbElems() )
81 {
82 Leaf leaf = ( Leaf ) ( parentPos.page );
83 MultipleMemoryHolder mvHolder = ( MultipleMemoryHolder ) leaf.values[parentPos.pos];
84 if( !mvHolder.isSingleValue() )
85 {
86 BTree dupsContainer = ( BTree ) mvHolder.getValue( btree );
87 parentPos.dupsContainer = dupsContainer;
88 parentPos.dupPos = 0;
89 }
90 }
91 }
92
93
94 /**
95 * Sets the multi-value container(a.k.a dupsContainer) of the key at the index below the given position( i.e pos - 1)
96 * and resets the 'dupsPos' to the number of elements present in the multi-value container.
97 * This is used by Cursor while navigating using prev()
98 *
99 * @param parentPos the parent position object
100 * @param btree the BTree
101 */
102 public static void changePrevDupsContainer( ParentPos parentPos, BTree btree ) throws IOException
103 {
104 if ( !btree.isAllowDuplicates() )
105 {
106 return;
107 }
108
109 int index = parentPos.pos - 1;
110 if ( index >= 0 )
111 {
112 Leaf leaf = ( Leaf ) ( parentPos.page );
113 MultipleMemoryHolder mvHolder = ( MultipleMemoryHolder ) leaf.values[index];
114 if( !mvHolder.isSingleValue() )
115 {
116 BTree dupsContainer = ( BTree ) mvHolder.getValue( btree );
117 parentPos.dupsContainer = dupsContainer;
118 parentPos.dupPos = ( int ) parentPos.dupsContainer.getNbElems();
119 }
120 else
121 {
122 parentPos.dupsContainer = null;
123 parentPos.dupPos = -1;
124 }
125 }
126 }
127
128
129 /**
130 * Same as @see #changePrevDupsContainer(ParentPos, BTree) but with a different name
131 * to make it sound semantically right when used inside {@link BTreeFactory#getPathToRightMostLeaf(BTree)}
132 */
133 public static void setLastDupsContainer( ParentPos parentPos, BTree btree ) throws IOException
134 {
135 changePrevDupsContainer( parentPos, btree );
136 }
137 }