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  package org.apache.directory.mavibot.btree;
21  
22  import java.util.concurrent.atomic.AtomicInteger;
23  
24  
25  /**
26   * Store in memory the information associated with a B-tree. <br>
27   * A B-tree Header on disk contains the following elements :
28   * <pre>
29   * +--------------------+-------------+
30   * | revision           | 8 bytes     |
31   * +--------------------+-------------+
32   * | nbElems            | 8 bytes     |
33   * +--------------------+-------------+
34   * | rootPageOffset     | 8 bytes     |
35   * +--------------------+-------------+
36   * | BtreeOffset        | 8 bytes     |
37   * +--------------------+-------------+
38   * </pre>
39   * Each B-tree Header will be written starting on a new page.
40   * In memory, a B-tree Header store a bit more of information :
41   * <li>
42   * <ul>rootPage : the associated rootPage in memory</lu>
43   * <ul>nbUsers : the number of readThreads using this revision</lu>
44   * <ul>offset : the offset of this B-tre header</lu>
45   * </li>
46   *
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  /* No qualifier*/class BTreeHeader<K, V> implements Cloneable
50  {
51      /** The current revision */
52      private long revision = 0L;
53  
54      /** The number of elements in this B-tree */
55      private Long nbElems = 0L;
56  
57      /** The offset of the B-tree RootPage */
58      private long rootPageOffset;
59  
60      /** The position of the B-tree header in the file */
61      private long btreeHeaderOffset;
62  
63      // Those are data which aren't serialized : they are in memory only */
64      /** A Map containing the rootPage for this tree */
65      private Page<K, V> rootPage;
66  
67      /** The number of users for this BtreeHeader */
68      private AtomicInteger nbUsers = new AtomicInteger( 0 );
69  
70      /** The B-tree this header is associated with */
71      private BTree<K, V> btree;
72  
73  
74      /**
75       * Creates a BTreeHeader instance
76       */
77      public BTreeHeader()
78      {
79      }
80  
81  
82      /**
83       * @return the B-tree info Offset
84       */
85      public long getBTreeInfoOffset()
86      {
87          return ((PersistedBTree<K, V>)btree).getBtreeInfoOffset();
88      }
89  
90  
91      /**
92       * @return the B-tree header Offset
93       */
94      public long getBTreeHeaderOffset()
95      {
96          return btreeHeaderOffset;
97      }
98  
99  
100     public BTreeHeader<K, V> clone()
101     {
102         try
103         {
104             BTreeHeader<K, V> copy = (BTreeHeader<K, V>)super.clone();
105 
106             return copy;
107         }
108         catch ( CloneNotSupportedException cnse )
109         {
110             return null;
111         }
112     }
113 
114 
115     /**
116      * Copy the current B-tre header and return the copy
117      * @return
118      */
119     /* no qualifier */ BTreeHeader<K, V> copy()
120     {
121         BTreeHeader<K, V> copy = clone();
122 
123         // Clear the fields that should not be copied
124         copy.rootPage = null;
125         copy.rootPageOffset = -1L;
126         copy.btreeHeaderOffset = -1L;
127 
128         return copy;
129     }
130 
131 
132     /**
133      * @param btreeOffset the B-tree header Offset to set
134      */
135     /* no qualifier */void setBTreeHeaderOffset( long btreeHeaderOffset )
136     {
137         this.btreeHeaderOffset = btreeHeaderOffset;
138     }
139 
140 
141     /**
142      * @return the rootPageOffset
143      */
144     public long getRootPageOffset()
145     {
146         return rootPageOffset;
147     }
148 
149 
150     /**
151      * @param rootPageOffset the rootPageOffset to set
152      */
153     /* no qualifier */void setRootPageOffset( long rootPageOffset )
154     {
155         this.rootPageOffset = rootPageOffset;
156     }
157 
158 
159     /**
160      * @return the revision
161      */
162     public long getRevision()
163     {
164         return revision;
165     }
166 
167 
168     /**
169      * @param revision the revision to set
170      */
171     /* no qualifier */void setRevision( long revision )
172     {
173         this.revision = revision;
174     }
175 
176 
177     /**
178      * @return the nbElems
179      */
180     public long getNbElems()
181     {
182         return nbElems;
183     }
184 
185 
186     /**
187      * @param nbElems the nbElems to set
188      */
189     /* no qualifier */void setNbElems( long nbElems )
190     {
191         this.nbElems = nbElems;
192     }
193 
194 
195     /**
196      * Increment the number of elements
197      */
198     /* no qualifier */void incrementNbElems()
199     {
200         nbElems++;
201     }
202 
203 
204     /**
205      * Decrement the number of elements
206      */
207     /* no qualifier */void decrementNbElems()
208     {
209         nbElems--;
210     }
211 
212 
213     /**
214      * @return the rootPage
215      */
216     /* no qualifier */ Page<K, V> getRootPage()
217     {
218         return rootPage;
219     }
220 
221 
222     /**
223      * @param rootPage the rootPage to set
224      */
225     /* no qualifier */ void setRootPage( Page<K, V> rootPage )
226     {
227         this.rootPage = rootPage;
228         this.rootPageOffset = ((AbstractPage<K, V>)rootPage).getOffset();
229     }
230 
231 
232     /**
233      * @return the nbUsers
234      */
235     /* no qualifier */ int getNbUsers()
236     {
237         return nbUsers.get();
238     }
239 
240 
241     /**
242      * Increment the number of users
243      */
244     /* no qualifier */ void incrementNbUsers()
245     {
246         nbUsers.incrementAndGet();
247     }
248 
249 
250     /**
251      * Decrement the number of users
252      */
253     /* no qualifier */ void decrementNbUsers()
254     {
255         nbUsers.decrementAndGet();
256     }
257 
258 
259     /**
260      * @return the btree
261      */
262     /* no qualifier */ BTree<K, V> getBtree()
263     {
264         return btree;
265     }
266 
267 
268     /**
269      * @param btree the btree to set
270      */
271     /* no qualifier */ void setBtree( BTree<K, V> btree )
272     {
273         this.btree = btree;
274     }
275 
276 
277     /**
278      * @see Object#toString()
279      */
280     public String toString()
281     {
282         StringBuilder sb = new StringBuilder();
283 
284         sb.append( "B-treeHeader " );
285         sb.append( ", offset[0x" ).append( Long.toHexString( btreeHeaderOffset ) ).append( "]" );
286         sb.append( ", name[" ).append( btree.getName() ).append( "]" );
287         sb.append( ", revision[" ).append( revision ).append( "]" );
288         sb.append( ", btreeInfoOffset[0x" ).append( Long.toHexString( ((PersistedBTree<K, V>)btree).getBtreeInfoOffset() ) ).append( "]" );
289         sb.append( ", rootPageOffset[0x" ).append( Long.toHexString( rootPageOffset ) ).append( "]" );
290         sb.append( ", nbElems[" ).append( nbElems ).append( "]" );
291         sb.append( ", nbUsers[" ).append( nbUsers.get() ).append( "]" );
292 
293         return sb.toString();
294     }
295 }