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