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 B-tree 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 PersistedBTreeConfiguration<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 B-tree 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 B-tree name */
50      private String name;
51  
52      /** The path where the B-tree 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 = PersistedBTree.DEFAULT_READ_TIMEOUT;
65  
66      /** Flag to enable duplicate key support */
67      private boolean allowDuplicates;
68  
69      /** The B-tree type */
70      private BTreeTypeEnum btreeType = BTreeTypeEnum.PERSISTED;
71  
72      /** The cache size, if it's <= 0, we don't have cache */
73      private int cacheSize;
74  
75      /** The inherited B-tree if we create a sub B-tree */
76      private BTree<?, V> parentBTree;
77  
78  
79      /**
80       * @return the pageSize
81       */
82      public int getPageSize()
83      {
84          return pageSize;
85      }
86  
87  
88      /**
89       * @param pageSize the pageSize to set
90       */
91      public void setPageSize( int pageSize )
92      {
93          this.pageSize = pageSize;
94      }
95  
96  
97      /**
98       * @return the key serializer
99       */
100     public ElementSerializer<K> getKeySerializer()
101     {
102         return keySerializer;
103     }
104 
105 
106     /**
107      * @return the value serializer
108      */
109     public ElementSerializer<V> getValueSerializer()
110     {
111         return valueSerializer;
112     }
113 
114 
115     /**
116      * @param keySerializer the key serializer to set
117      * @param valueSerializer the value serializer to set
118      */
119     public void setSerializers( ElementSerializer<K> keySerializer, ElementSerializer<V> valueSerializer )
120     {
121         this.keySerializer = keySerializer;
122         this.valueSerializer = valueSerializer;
123     }
124 
125 
126     /**
127      * @param serializer the key serializer to set
128      */
129     public void setKeySerializer( ElementSerializer<K> keySerializer )
130     {
131         this.keySerializer = keySerializer;
132     }
133 
134 
135     /**
136      * @param serializer the key serializer to set
137      */
138     public void setValueSerializer( ElementSerializer<V> valueSerializer )
139     {
140         this.valueSerializer = valueSerializer;
141     }
142 
143 
144     /**
145      * @return the readTimeOut
146      */
147     public long getReadTimeOut()
148     {
149         return readTimeOut;
150     }
151 
152 
153     /**
154      * @param readTimeOut the readTimeOut to set
155      */
156     public void setReadTimeOut( long readTimeOut )
157     {
158         this.readTimeOut = readTimeOut;
159     }
160 
161 
162     /**
163      * @return the filePath
164      */
165     public String getFilePath()
166     {
167         return filePath;
168     }
169 
170 
171     /**
172      * @param filePath the filePath to set
173      */
174     public void setFilePath( String filePath )
175     {
176         this.filePath = filePath;
177     }
178 
179 
180     /**
181      * @return the writeBufferSize
182      */
183     public int getWriteBufferSize()
184     {
185         return writeBufferSize;
186     }
187 
188 
189     /**
190      * @param writeBufferSize the writeBufferSize to set
191      */
192     public void setWriteBufferSize( int writeBufferSize )
193     {
194         this.writeBufferSize = writeBufferSize;
195     }
196 
197 
198     /**
199      * @return the name
200      */
201     public String getName()
202     {
203         return name;
204     }
205 
206 
207     /**
208      * @param name the name to set
209      */
210     public void setName( String name )
211     {
212         this.name = name.trim();
213     }
214 
215 
216     /**
217      * @return true if duplicate key support is enabled
218      */
219     public boolean isAllowDuplicates()
220     {
221         return allowDuplicates;
222     }
223 
224 
225     /**
226      * enable duplicate key support
227      *
228      * @param allowDuplicates
229      * @throws IllegalStateException if the B-tree was already initialized or when tried to turn off duplicate suport on
230      * an existing B-tree containing duplicate keys
231      */
232     public void setAllowDuplicates( boolean allowDuplicates )
233     {
234         this.allowDuplicates = allowDuplicates;
235     }
236 
237 
238     /**
239      * @return the cacheSize
240      */
241     public int getCacheSize()
242     {
243         return cacheSize;
244     }
245 
246 
247     /**
248      * @param cacheSize the cacheSize to set.
249      */
250     public void setCacheSize( int cacheSize )
251     {
252         this.cacheSize = cacheSize;
253     }
254 
255 
256     /**
257      * @return the cache
258      */
259     public BTree<?, V> getParentBTree()
260     {
261         return parentBTree;
262     }
263 
264 
265     /**
266      * @param cache the cache to set.
267      */
268     public void setParentBTree( BTree<?, V> parentBTree )
269     {
270         this.parentBTree = parentBTree;
271     }
272 
273 
274     /**
275      * @return The BtreeType for this Btree
276      */
277     public BTreeTypeEnum getBtreeType()
278     {
279         return btreeType;
280     }
281 
282 
283     /**
284      * @param btreeType The BtreeType
285      */
286     public void setBtreeType( BTreeTypeEnum btreeType )
287     {
288         this.btreeType = btreeType;
289     }
290 }