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 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 InMemoryBTreeConfiguration<K, V>
36  {
37      /** Number of entries in each Page. */
38      private int pageSize = InMemoryBTree.DEFAULT_PAGE_SIZE;
39  
40      /** The size of the buffer used to write data in disk */
41      private int writeBufferSize = InMemoryBTree.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 = InMemoryBTree.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 = InMemoryBTree.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      /** the type of BTree */
88      private BTreeTypeEnum type;
89  
90  
91      /**
92       * @return the pageSize
93       */
94      public int getPageSize()
95      {
96          return pageSize;
97      }
98  
99  
100     /**
101      * @param pageSize the pageSize to set
102      */
103     public void setPageSize( int pageSize )
104     {
105         this.pageSize = pageSize;
106     }
107 
108 
109     /**
110      * @return the key serializer
111      */
112     public ElementSerializer<K> getKeySerializer()
113     {
114         return keySerializer;
115     }
116 
117 
118     /**
119      * @return the value serializer
120      */
121     public ElementSerializer<V> getValueSerializer()
122     {
123         return valueSerializer;
124     }
125 
126 
127     /**
128      * @param keySerializer the key serializer to set
129      * @param valueSerializer the value serializer to set
130      */
131     public void setSerializers( ElementSerializer<K> keySerializer, ElementSerializer<V> valueSerializer )
132     {
133         this.keySerializer = keySerializer;
134         this.valueSerializer = valueSerializer;
135     }
136 
137 
138     /**
139      * @param serializer the key serializer to set
140      */
141     public void setKeySerializer( ElementSerializer<K> keySerializer )
142     {
143         this.keySerializer = keySerializer;
144     }
145 
146 
147     /**
148      * @param serializer the key serializer to set
149      */
150     public void setValueSerializer( ElementSerializer<V> valueSerializer )
151     {
152         this.valueSerializer = valueSerializer;
153     }
154 
155 
156     /**
157      * @return the readTimeOut
158      */
159     public long getReadTimeOut()
160     {
161         return readTimeOut;
162     }
163 
164 
165     /**
166      * @param readTimeOut the readTimeOut to set
167      */
168     public void setReadTimeOut( long readTimeOut )
169     {
170         this.readTimeOut = readTimeOut;
171     }
172 
173 
174     /**
175      * @return the journalSize
176      */
177     public long getJournalSize()
178     {
179         return journalSize;
180     }
181 
182 
183     /**
184      * @param journalSize the journalSize to set
185      */
186     public void setJournalSize( long journalSize )
187     {
188         this.journalSize = journalSize;
189     }
190 
191 
192     /**
193      * @return the checkPointDelay
194      */
195     public long getCheckPointDelay()
196     {
197         return checkPointDelay;
198     }
199 
200 
201     /**
202      * @param checkPointDelay the checkPointDelay to set
203      */
204     public void setCheckPointDelay( long checkPointDelay )
205     {
206         this.checkPointDelay = checkPointDelay;
207     }
208 
209 
210     /**
211      * @return the filePath
212      */
213     public String getFilePath()
214     {
215         return filePath;
216     }
217 
218 
219     /**
220      * @param filePath the filePath to set
221      */
222     public void setFilePath( String filePath )
223     {
224         this.filePath = filePath;
225     }
226 
227 
228     /**
229      * @return the journal name
230      */
231     public String getJournalName()
232     {
233         return journalName;
234     }
235 
236 
237     /**
238      * @param journalName the journal name to set
239      */
240     public void setJournalName( String journalName )
241     {
242         this.journalName = journalName;
243     }
244 
245 
246     /**
247      * @return the writeBufferSize
248      */
249     public int getWriteBufferSize()
250     {
251         return writeBufferSize;
252     }
253 
254 
255     /**
256      * @param writeBufferSize the writeBufferSize to set
257      */
258     public void setWriteBufferSize( int writeBufferSize )
259     {
260         this.writeBufferSize = writeBufferSize;
261     }
262 
263 
264     /**
265      * @return the name
266      */
267     public String getName()
268     {
269         return name;
270     }
271 
272 
273     /**
274      * @param name the name to set
275      */
276     public void setName( String name )
277     {
278         this.name = name.trim();
279     }
280 
281 
282     /**
283      * @return true if duplicate key support is enabled
284      */
285     public boolean isAllowDuplicates()
286     {
287         return allowDuplicates;
288     }
289 
290 
291     /**
292      * enable duplicate key support
293      * 
294      * @param allowDuplicates
295      * @throws IllegalStateException if the btree was already initialized or when tried to turn off duplicate suport on
296      *                               an existing btree containing duplicate keys
297      */
298     public void setAllowDuplicates( boolean allowDuplicates )
299     {
300         this.allowDuplicates = allowDuplicates;
301     }
302 
303 
304     /**
305      * @return the type of BTree
306      */
307     public BTreeTypeEnum getType()
308     {
309         return type;
310     }
311 
312 
313     /**
314      * Sets the type of the BTree
315      * 
316      * @param type the type of the tree
317      */
318     public void setType( BTreeTypeEnum type )
319     {
320         this.type = type;
321     }
322 }