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.AtomicLong;
24  
25  
26  /**
27   * Store in memory the information associated with a BTree. <br>
28   * A BTree 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   * | nextBtreeHeader    | 8 bytes     |
38   * +--------------------+-------------+
39   * | pageSize           | 4 bytes     |
40   * +--------------------+-------------+
41   * | name               | 4 bytes + N |
42   * +--------------------+-------------+
43   * | keySerializeFQCN   | 4 bytes + N |
44   * +--------------------+-------------+
45   * | valueSerializeFQCN | 4 bytes + N |
46   * +--------------------+-------------+
47   * </pre>
48   * Each BtreeHeader will be written starting on a new page.
49   * 
50   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
51   */
52  public class BTreeHeader
53  {
54      /** The current revision */
55      private AtomicLong revision = new AtomicLong( 0L );
56  
57      /** The number of elements in this BTree */
58      private AtomicLong nbElems = new AtomicLong( 0L );
59  
60      /** The offset of the BTree RootPage */
61      private long rootPageOffset;
62  
63      /** The offset of the next BTree */
64      private long nextBTreeOffset;
65  
66      /** The number of elements in a page for this BTree */
67      private int pageSize;
68  
69      /** The BTree name */
70      private String name;
71  
72      /** The FQCN of the Key serializer */
73      private String keySerializerFQCN;
74  
75      /** The FQCN of the Value serializer */
76      private String valueSerializerFQCN;
77  
78      // Those are data which aren't serialized : they are in memory only */
79      /** The position in the file */
80      private long btreeOffset;
81  
82      /** The existing versions */
83      private long[] versions;
84  
85      private int allowDuplicates = 0;
86  
87  
88      /**
89       * Creates a BTreeHeader instance
90       */
91      public BTreeHeader()
92      {
93      }
94  
95  
96      /**
97       * @return the name
98       */
99      public String getName()
100     {
101         return name;
102     }
103 
104 
105     /**
106      * @param name the name to set
107      */
108     public void setName( String name )
109     {
110         this.name = name;
111     }
112 
113 
114     /**
115      * @return the versions
116      */
117     public long[] getVersions()
118     {
119         return versions;
120     }
121 
122 
123     /**
124      * @param versions the versions to set
125      */
126     public void setVersions( long[] versions )
127     {
128         this.versions = versions;
129     }
130 
131 
132     /**
133      * @return the btreeOffset
134      */
135     public long getBTreeOffset()
136     {
137         return btreeOffset;
138     }
139 
140 
141     /**
142      * @param btreeOffset the btreeOffset to set
143      */
144     public void setBTreeOffset( long btreeOffset )
145     {
146         this.btreeOffset = btreeOffset;
147     }
148 
149 
150     /**
151      * @return the rootPageOffset
152      */
153     public long getRootPageOffset()
154     {
155         return rootPageOffset;
156     }
157 
158 
159     /**
160      * @param rootPageOffset the rootPageOffset to set
161      */
162     public void setRootPageOffset( long rootPageOffset )
163     {
164         this.rootPageOffset = rootPageOffset;
165     }
166 
167 
168     /**
169      * @return the revision
170      */
171     public long getRevision()
172     {
173         return revision.get();
174     }
175 
176 
177     /**
178      * @param revision the revision to set
179      */
180     public void setRevision( long revision )
181     {
182         this.revision.set( revision );
183     }
184 
185 
186     /**
187      * Increment the revision
188      * 
189      * @return the new revision
190      */
191     public long incrementRevision()
192     {
193         return revision.incrementAndGet();
194     }
195 
196 
197     /**
198      * @return the nbElems
199      */
200     public long getNbElems()
201     {
202         return nbElems.get();
203     }
204 
205 
206     /**
207      * Increment the number of elements
208      */
209     public void incrementNbElems()
210     {
211         nbElems.incrementAndGet();
212     }
213 
214 
215     /**
216      * Decrement the number of elements
217      */
218     public void decrementNbElems()
219     {
220         nbElems.decrementAndGet();
221     }
222 
223 
224     /**
225      * @param nbElems the nbElems to set
226      */
227     public void setNbElems( long nbElems )
228     {
229         this.nbElems.set( nbElems );
230     }
231 
232 
233     /**
234      * @return the nextBTreeOffset
235      */
236     public long getNextBTreeOffset()
237     {
238         return nextBTreeOffset;
239     }
240 
241 
242     /**
243      * @param nextBtreeOffset the nextBtreeOffset to set
244      */
245     public void setNextBTreeOffset( long nextBTreeOffset )
246     {
247         this.nextBTreeOffset = nextBTreeOffset;
248     }
249 
250 
251     /**
252      * @return the pageSize
253      */
254     public int getPageSize()
255     {
256         return pageSize;
257     }
258 
259 
260     /**
261      * @param pageSize the pageSize to set
262      */
263     public void setPageSize( int pageSize )
264     {
265         this.pageSize = pageSize;
266     }
267 
268 
269     /**
270      * @return the keySerializerFQCN
271      */
272     public String getKeySerializerFQCN()
273     {
274         return keySerializerFQCN;
275     }
276 
277 
278     /**
279      * @param keySerializerFQCN the keySerializerFQCN to set
280      */
281     public void setKeySerializerFQCN( String keySerializerFQCN )
282     {
283         this.keySerializerFQCN = keySerializerFQCN;
284     }
285 
286 
287     /**
288      * @return the valueSerializerFQCN
289      */
290     public String getValueSerializerFQCN()
291     {
292         return valueSerializerFQCN;
293     }
294 
295 
296     /**
297      * @param valueSerializerFQCN the valueSerializerFQCN to set
298      */
299     public void setValueSerializerFQCN( String valueSerializerFQCN )
300     {
301         this.valueSerializerFQCN = valueSerializerFQCN;
302     }
303 
304 
305     public boolean isAllowDuplicates()
306     {
307         return ( allowDuplicates == 1 );
308     }
309 
310 
311     public void setAllowDuplicates( boolean allowDuplicates )
312     {
313         this.allowDuplicates = ( allowDuplicates ? 1 : 0 );
314     }
315 
316 
317     /**
318      * @see Object#toString()
319      */
320     public String toString()
321     {
322         StringBuilder sb = new StringBuilder();
323 
324         sb.append( "Btree '" ).append( name ).append( "'" );
325         sb.append( ", revision[" ).append( revision ).append( "]" );
326         sb.append( ", btreeOffset[" ).append( btreeOffset ).append( "]" );
327         sb.append( ", rootPageOffset[" ).append( rootPageOffset ).append( "]" );
328         sb.append( ", nextBTree[" ).append( nextBTreeOffset ).append( "]" );
329         sb.append( ", nbElems[" ).append( nbElems ).append( "]" );
330         sb.append( ", pageSize[" ).append( pageSize ).append( "]" );
331         sb.append( ", hasDuplicates[" ).append( isAllowDuplicates() ).append( "]" );
332         sb.append( "{\n" );
333         sb.append( "    Key serializer   : " ).append( keySerializerFQCN ).append( "\n" );
334         sb.append( "    Value serializer : " ).append( valueSerializerFQCN ).append( "\n" );
335         sb.append( "}\n" );
336 
337         if ( ( versions != null ) && ( versions.length != 0 ) )
338         {
339             sb.append( "Versions : \n" );
340             sb.append( "{\n" );
341 
342             boolean isFirst = true;
343 
344             for ( long version : versions )
345             {
346                 if ( isFirst )
347                 {
348                     isFirst = false;
349                 }
350                 else
351                 {
352                     sb.append( ",\n" );
353                 }
354 
355                 sb.append( "    " ).append( version );
356             }
357 
358             sb.append( "}\n" );
359         }
360 
361         return sb.toString();
362     }
363 }