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 */
019package org.apache.directory.server.core.factory;
020
021
022import java.io.File;
023import java.util.Set;
024
025import org.apache.directory.api.ldap.model.name.Dn;
026import org.apache.directory.api.ldap.model.schema.SchemaManager;
027import org.apache.directory.server.core.api.DnFactory;
028import org.apache.directory.server.core.api.partition.Partition;
029import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex;
030import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
031import org.apache.directory.server.xdbm.Index;
032
033
034/**
035 * A factory used to generate {@link JdbmPartition}s.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class JdbmPartitionFactory implements PartitionFactory
040{
041
042    /**
043     * {@inheritDoc}
044     */
045    public JdbmPartition createPartition( SchemaManager schemaManager, DnFactory dnFactory, String id, String suffix,
046        int cacheSize,
047        File workingDirectory )
048        throws Exception
049    {
050        JdbmPartition partition = new JdbmPartition( schemaManager, dnFactory );
051        partition.setId( id );
052        partition.setSuffixDn( new Dn( schemaManager, suffix ) );
053        partition.setCacheSize( cacheSize );
054        partition.setPartitionPath( workingDirectory.toURI() );
055
056        return partition;
057    }
058
059
060    /**
061     * {@inheritDoc}
062     */
063    public void addIndex( Partition partition, String attributeId, int cacheSize ) throws Exception
064    {
065        if ( !( partition instanceof JdbmPartition ) )
066        {
067            throw new IllegalArgumentException( "Partition must be a JdbmPartition" );
068        }
069
070        JdbmPartition jdbmPartition = ( JdbmPartition ) partition;
071        Set<Index<?, String>> indexedAttributes = jdbmPartition.getIndexedAttributes();
072
073        JdbmIndex<Object> index = new JdbmIndex<>( attributeId, false );
074        index.setCacheSize( cacheSize );
075
076        indexedAttributes.add( index );
077        jdbmPartition.setIndexedAttributes( indexedAttributes );
078    }
079
080}