001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020
021package org.apache.directory.mavibot.btree;
022
023
024/**
025 * A class to store informations on a level. We have to keep :
026 * <ul>
027 * <li>The number of elements to store in this level</li>
028 * <li>A flag that tells if it's a leaf or a node level</li>
029 * <li>The number of pages necessary to store all the elements in a level</li>
030 * <li>The number of elements we can store in a complete page (we may have one or two 
031 * incomplete pages at the end)</li>
032 * <li>A flag that tells if we have some incomplete page at the end</li>
033 * </ul>
034 *
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public class LevelInfo<K, V>
038{
039    /** The level number */
040    private int levelNumber;
041
042    /** Nb of elements for this level */
043    private int nbElems;
044
045    /** The number of pages in this level */
046    private int nbPages;
047
048    /** Nb of elements before we reach an incomplete page */
049    private int nbElemsLimit;
050
051    /** A flag that tells if the level contains nodes or leaves */
052    private boolean isNode;
053
054    /** The current page which contains the data until we move it to the resulting BTree */
055    private Page<K, V> currentPage;
056
057    /** The current position in the currentPage */
058    private int currentPos;
059
060    /** The number of already added elements for this level */
061    private int nbAddedElems;
062
063
064    /**
065     * @return the levelNumber
066     */
067    public int getLevelNumber()
068    {
069        return levelNumber;
070    }
071
072
073    /**
074     * @param levelNumber the levelNumber to set
075     */
076    public void setLevelNumber( int levelNumber )
077    {
078        this.levelNumber = levelNumber;
079    }
080
081
082    /**
083     * @return the nbElems
084     */
085    public int getNbElems()
086    {
087        return nbElems;
088    }
089
090
091    /**
092     * @param nbElems the nbElems to set
093     */
094    public void setNbElems( int nbElems )
095    {
096        this.nbElems = nbElems;
097    }
098
099
100    /**
101     * @return the nbPages
102     */
103    public int getNbPages()
104    {
105        return nbPages;
106    }
107
108
109    /**
110     * @param nbPages the nbPages to set
111     */
112    public void setNbPages( int nbPages )
113    {
114        this.nbPages = nbPages;
115    }
116
117
118    /**
119     * Increment the number of pages
120     */
121    public void incNbPages()
122    {
123        this.nbPages++;
124    }
125
126
127    /**
128     * @return the nbElemsLimit
129     */
130    public int getNbElemsLimit()
131    {
132        return nbElemsLimit;
133    }
134
135
136    /**
137     * @param nbElemsLimit the nbElemsLimit to set
138     */
139    public void setNbElemsLimit( int nbElemsLimit )
140    {
141        this.nbElemsLimit = nbElemsLimit;
142    }
143
144
145    /**
146     * @return the isNode
147     */
148    public boolean isNode()
149    {
150        return isNode;
151    }
152
153
154    /**
155     * @param isNode the isNode to set
156     */
157    public void setType( boolean isNode )
158    {
159        this.isNode = isNode;
160    }
161
162
163    /**
164     * @return the currentPage
165     */
166    public Page<K, V> getCurrentPage()
167    {
168        return currentPage;
169    }
170
171
172    /**
173     * @param currentPage the currentPage to set
174     */
175    public void setCurrentPage( Page<K, V> currentPage )
176    {
177        this.currentPage = currentPage;
178    }
179
180
181    /**
182     * @return the currentPos
183     */
184    public int getCurrentPos()
185    {
186        return currentPos;
187    }
188
189
190    /**
191     * @param currentPos the currentPos to set
192     */
193    public void setCurrentPos( int currentPos )
194    {
195        this.currentPos = currentPos;
196    }
197
198
199    /**
200     * Increment the current position
201     */
202    public void incCurrentPos()
203    {
204        this.currentPos++;
205    }
206
207
208    /**
209     * @return the nbAddedElems
210     */
211    public int getNbAddedElems()
212    {
213        return nbAddedElems;
214    }
215
216
217    /**
218     * @param nbAddedElems the nbAddedElems to set
219     */
220    public void setNbAddedElems( int nbAddedElems )
221    {
222        this.nbAddedElems = nbAddedElems;
223    }
224
225
226    /**
227     * Increment the number of added elements
228     */
229    public void incNbAddedElems()
230    {
231        this.nbAddedElems++;
232    }
233
234
235    /** @see Object#toString() */
236    public String toString()
237    {
238        StringBuilder sb = new StringBuilder();
239
240        if ( isNode )
241        {
242            sb.append( "NodeLevel[" );
243            sb.append( levelNumber );
244            sb.append( "] :" );
245        }
246        else
247        {
248            sb.append( "LeafLevel:" );
249        }
250
251        sb.append( "\n    nbElems           = " ).append( nbElems );
252        sb.append( "\n    nbPages           = " ).append( nbPages );
253        sb.append( "\n    nbElemsLimit      = " ).append( nbElemsLimit );
254        sb.append( "\n    nbAddedElems      = " ).append( nbAddedElems );
255        sb.append( "\n    currentPos        = " ).append( currentPos );
256        sb.append( "\n    currentPage" );
257        sb.append( "\n        nbKeys : " ).append( currentPage.getNbElems() );
258
259        return sb.toString();
260    }
261}