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.managed;
21  
22  
23  import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
24  
25  
26  /**
27   * The B+Tree Configuration. This class can be used to store all the configurable
28   * parameters used by the BTree class
29   * 
30   * @param <K> The type for the keys
31   * @param <V> The type for the stored values
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class BTreeConfiguration<K, V>
36  {
37      /** Number of entries in each Page. */
38      private int pageSize = BTree.DEFAULT_PAGE_SIZE;
39  
40      /** The size of the buffer used to write data in disk */
41      private int writeBufferSize = BTree.DEFAULT_WRITE_BUFFER_SIZE;
42  
43      /** The Key and Value serializer used for this tree. If none is provided, 
44       * the BTree will deduce the serializer to use from the generic type, and
45       * use the default Java serialization  */
46      private ElementSerializer<K> keySerializer;
47      private ElementSerializer<V> valueSerializer;
48  
49      /** The BTree name */
50      private String name;
51  
52      /** The path where the BTree file will be stored. Default to the local 
53       * temporary directory.
54       */
55      private String filePath;
56  
57      /** 
58       * The maximum delay to wait before a revision is considered as unused.
59       * This delay is necessary so that a read that does not ends does not 
60       * hold a revision in memory forever.
61       * The default value is 10000 (10 seconds). If the value is 0 or below,
62       * the delay is considered as infinite
63       */
64      private long readTimeOut = BTree.DEFAULT_READ_TIMEOUT;
65  
66      /** The maximal size of the journal. When this size is reached, the tree is 
67       * flushed on disk.
68       * The default size is 10 Mb
69       */
70      private long journalSize = 10 * 1024 * 1024L;
71  
72      /**
73       * The journal's name. Default to "mavibot.log".
74       */
75      private String journalName = BTree.DEFAULT_JOURNAL;
76  
77      /** 
78       * The delay between two checkpoints. When we reach the maximum delay,
79       * the BTree is flushed on disk, but only if we have had some modifications.
80       * The default value is 60 seconds.
81       */
82      private long checkPointDelay = 60 * 1000L;
83  
84      /** Flag to enable duplicate key support */
85      private boolean allowDuplicates;
86      
87      /** A flag set when the BTree is a sub btree */
88      private boolean isSubBtree = false;
89  
90      /** The cache size, if it's <= 0, we don't have cache */
91      private int cacheSize;
92      
93      /** The inherited BTree if we create a sub BTree */
94      private BTree<?, V> parentBTree;
95      
96      /**
97       * @return the pageSize
98       */
99      public int getPageSize()
100     {
101         return pageSize;
102     }
103 
104 
105     /**
106      * @param pageSize the pageSize to set
107      */
108     public void setPageSize( int pageSize )
109     {
110         this.pageSize = pageSize;
111     }
112 
113 
114     /**
115      * @return the key serializer
116      */
117     public ElementSerializer<K> getKeySerializer()
118     {
119         return keySerializer;
120     }
121 
122 
123     /**
124      * @return the value serializer
125      */
126     public ElementSerializer<V> getValueSerializer()
127     {
128         return valueSerializer;
129     }
130 
131 
132     /**
133      * @param keySerializer the key serializer to set
134      * @param valueSerializer the value serializer to set
135      */
136     public void setSerializers( ElementSerializer<K> keySerializer, ElementSerializer<V> valueSerializer )
137     {
138         this.keySerializer = keySerializer;
139         this.valueSerializer = valueSerializer;
140     }
141 
142 
143     /**
144      * @param serializer the key serializer to set
145      */
146     public void setKeySerializer( ElementSerializer<K> keySerializer )
147     {
148         this.keySerializer = keySerializer;
149     }
150 
151 
152     /**
153      * @param serializer the key serializer to set
154      */
155     public void setValueSerializer( ElementSerializer<V> valueSerializer )
156     {
157         this.valueSerializer = valueSerializer;
158     }
159 
160 
161     /**
162      * @return the readTimeOut
163      */
164     public long getReadTimeOut()
165     {
166         return readTimeOut;
167     }
168 
169 
170     /**
171      * @param readTimeOut the readTimeOut to set
172      */
173     public void setReadTimeOut( long readTimeOut )
174     {
175         this.readTimeOut = readTimeOut;
176     }
177 
178 
179     /**
180      * @return the journalSize
181      */
182     public long getJournalSize()
183     {
184         return journalSize;
185     }
186 
187 
188     /**
189      * @param journalSize the journalSize to set
190      */
191     public void setJournalSize( long journalSize )
192     {
193         this.journalSize = journalSize;
194     }
195 
196 
197     /**
198      * @return the checkPointDelay
199      */
200     public long getCheckPointDelay()
201     {
202         return checkPointDelay;
203     }
204 
205 
206     /**
207      * @param checkPointDelay the checkPointDelay to set
208      */
209     public void setCheckPointDelay( long checkPointDelay )
210     {
211         this.checkPointDelay = checkPointDelay;
212     }
213 
214 
215     /**
216      * @return the filePath
217      */
218     public String getFilePath()
219     {
220         return filePath;
221     }
222 
223 
224     /**
225      * @param filePath the filePath to set
226      */
227     public void setFilePath( String filePath )
228     {
229         this.filePath = filePath;
230     }
231 
232 
233     /**
234      * @return the journal name
235      */
236     public String getJournalName()
237     {
238         return journalName;
239     }
240 
241 
242     /**
243      * @param journalName the journal name to set
244      */
245     public void setJournalName( String journalName )
246     {
247         this.journalName = journalName;
248     }
249 
250 
251     /**
252      * @return the writeBufferSize
253      */
254     public int getWriteBufferSize()
255     {
256         return writeBufferSize;
257     }
258 
259 
260     /**
261      * @param writeBufferSize the writeBufferSize to set
262      */
263     public void setWriteBufferSize( int writeBufferSize )
264     {
265         this.writeBufferSize = writeBufferSize;
266     }
267 
268 
269     /**
270      * @return the name
271      */
272     public String getName()
273     {
274         return name;
275     }
276 
277 
278     /**
279      * @param name the name to set
280      */
281     public void setName( String name )
282     {
283         this.name = name.trim();
284     }
285 
286 
287     /**
288      * @return true if duplicate key support is enabled
289      */
290     public boolean isAllowDuplicates()
291     {
292         return allowDuplicates;
293     }
294 
295 
296     /**
297      * enable duplicate key support
298      * 
299      * @param allowDuplicates
300      * @throws IllegalStateException if the btree was already initialized or when tried to turn off duplicate suport on
301      * an existing btree containing duplicate keys
302      */
303     public void setAllowDuplicates( boolean allowDuplicates )
304     {
305         this.allowDuplicates = allowDuplicates;
306     }
307 
308 
309     /**
310      * @return the cacheSize
311      */
312     public int getCacheSize()
313     {
314         return cacheSize;
315     }
316 
317 
318     /**
319      * @param cacheSize the cacheSize to set.
320      */
321     public void setCacheSize( int cacheSize )
322     {
323         this.cacheSize = cacheSize;
324     }
325 
326 
327     /**
328      * @return the cache
329      */
330     public BTree<?, V> getParentBTree()
331     {
332         return parentBTree;
333     }
334 
335 
336     /**
337      * @param cache the cache to set.
338      */
339     public void setParentBTree( BTree<?, V> parentBTree )
340     {
341         this.parentBTree = parentBTree;
342     }
343 
344 
345     /**
346      * @return True if this is a sub btree, false otherwise
347      */
348     public boolean isSubBtree()
349     {
350         return isSubBtree;
351     }
352 
353 
354     /**
355      * @param isSubBtree True if the BTree will be a sub btree, false otherwise
356      */
357     public void setSubBtree( boolean isSubBtree )
358     {
359         this.isSubBtree = isSubBtree;
360     }
361 }