001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.mavibot.btree;
021
022
023import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
024
025
026/**
027 * The B+Tree Configuration. This class can be used to store all the configurable
028 * parameters used by the B-tree class
029 *
030 * @param <K> The type for the keys
031 * @param <V> The type for the stored values
032 *
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class PersistedBTreeConfiguration<K, V>
036{
037    /** Number of entries in each Page. */
038    private int pageSize = BTree.DEFAULT_PAGE_SIZE;
039
040    /** The size of the buffer used to write data in disk */
041    private int writeBufferSize = BTree.DEFAULT_WRITE_BUFFER_SIZE;
042
043    /** The Key and Value serializer used for this tree. If none is provided,
044     * the B-tree will deduce the serializer to use from the generic type, and
045     * use the default Java serialization  */
046    private ElementSerializer<K> keySerializer;
047    private ElementSerializer<V> valueSerializer;
048
049    /** The B-tree name */
050    private String name;
051
052    /** The path where the B-tree file will be stored. Default to the local
053     * temporary directory.
054     */
055    private String filePath;
056
057    /**
058     * The maximum delay to wait before a revision is considered as unused.
059     * This delay is necessary so that a read that does not ends does not
060     * hold a revision in memory forever.
061     * The default value is 10000 (10 seconds). If the value is 0 or below,
062     * the delay is considered as infinite
063     */
064    private long readTimeOut = PersistedBTree.DEFAULT_READ_TIMEOUT;
065
066    /** Flag to enable duplicate key support */
067    private boolean allowDuplicates;
068
069    /** The B-tree type */
070    private BTreeTypeEnum btreeType = BTreeTypeEnum.PERSISTED;
071
072    /** The cache size, if it's <= 0, we don't have cache */
073    private int cacheSize;
074
075    /** The inherited B-tree if we create a sub B-tree */
076    private BTree<?, V> parentBTree;
077
078
079    /**
080     * @return the pageSize
081     */
082    public int getPageSize()
083    {
084        return pageSize;
085    }
086
087
088    /**
089     * @param pageSize the pageSize to set
090     */
091    public void setPageSize( int pageSize )
092    {
093        this.pageSize = pageSize;
094    }
095
096
097    /**
098     * @return the key serializer
099     */
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}