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         copy.nbUsers.set( 0 );
128 
129         return copy;
130     }
131 
132 
133     /**
134      * @param btreeOffset the B-tree header Offset to set
135      */
136     /* no qualifier */void setBTreeHeaderOffset( long btreeHeaderOffset )
137     {
138         this.btreeHeaderOffset = btreeHeaderOffset;
139     }
140 
141 
142     /**
143      * @return the rootPageOffset
144      */
145     public long getRootPageOffset()
146     {
147         return rootPageOffset;
148     }
149 
150 
151     /**
152      * @param rootPageOffset the rootPageOffset to set
153      */
154     /* no qualifier */void setRootPageOffset( long rootPageOffset )
155     {
156         this.rootPageOffset = rootPageOffset;
157     }
158 
159 
160     /**
161      * @return the revision
162      */
163     public long getRevision()
164     {
165         return revision;
166     }
167 
168 
169     /**
170      * @param revision the revision to set
171      */
172     /* no qualifier */void setRevision( long revision )
173     {
174         this.revision = revision;
175     }
176 
177 
178     /**
179      * @return the nbElems
180      */
181     public long getNbElems()
182     {
183         return nbElems;
184     }
185 
186 
187     /**
188      * @param nbElems the nbElems to set
189      */
190     /* no qualifier */void setNbElems( long nbElems )
191     {
192         this.nbElems = nbElems;
193     }
194 
195 
196     /**
197      * Increment the number of elements
198      */
199     /* no qualifier */void incrementNbElems()
200     {
201         nbElems++;
202     }
203 
204 
205     /**
206      * Decrement the number of elements
207      */
208     /* no qualifier */void decrementNbElems()
209     {
210         nbElems--;
211     }
212 
213 
214     /**
215      * @return the rootPage
216      */
217     /* no qualifier */ Page<K, V> getRootPage()
218     {
219         return rootPage;
220     }
221 
222 
223     /**
224      * @param rootPage the rootPage to set
225      */
226     /* no qualifier */ void setRootPage( Page<K, V> rootPage )
227     {
228         this.rootPage = rootPage;
229         this.rootPageOffset = ((AbstractPage<K, V>)rootPage).getOffset();
230     }
231 
232 
233     /**
234      * @return the nbUsers
235      */
236     /* no qualifier */ int getNbUsers()
237     {
238         return nbUsers.get();
239     }
240 
241 
242     /**
243      * Increment the number of users
244      */
245     /* no qualifier */ void incrementNbUsers()
246     {
247         nbUsers.incrementAndGet();
248     }
249 
250 
251     /**
252      * Decrement the number of users
253      */
254     /* no qualifier */ void decrementNbUsers()
255     {
256         nbUsers.decrementAndGet();
257     }
258 
259 
260     /**
261      * @return the btree
262      */
263     /* no qualifier */ BTree<K, V> getBtree()
264     {
265         return btree;
266     }
267 
268 
269     /**
270      * @param btree the btree to set
271      */
272     /* no qualifier */ void setBtree( BTree<K, V> btree )
273     {
274         this.btree = btree;
275     }
276 
277 
278     /**
279      * @see Object#toString()
280      */
281     public String toString()
282     {
283         StringBuilder sb = new StringBuilder();
284 
285         sb.append( "B-treeHeader " );
286         sb.append( ", offset[0x" ).append( Long.toHexString( btreeHeaderOffset ) ).append( "]" );
287         sb.append( ", name[" ).append( btree.getName() ).append( "]" );
288         sb.append( ", revision[" ).append( revision ).append( "]" );
289         sb.append( ", btreeInfoOffset[0x" ).append( Long.toHexString( ((PersistedBTree<K, V>)btree).getBtreeInfoOffset() ) ).append( "]" );
290         sb.append( ", rootPageOffset[0x" ).append( Long.toHexString( rootPageOffset ) ).append( "]" );
291         sb.append( ", nbElems[" ).append( nbElems ).append( "]" );
292         sb.append( ", nbUsers[" ).append( nbUsers.get() ).append( "]" );
293 
294         return sb.toString();
295     }
296 }