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