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 */
020
021package org.apache.directory.server.core.partition.impl.btree.jdbm;
022
023
024import java.io.File;
025import java.io.IOException;
026
027import jdbm.RecordManager;
028
029import org.apache.directory.api.ldap.model.exception.LdapException;
030import org.apache.directory.api.ldap.model.name.Dn;
031import org.apache.directory.api.ldap.model.schema.AttributeType;
032import org.apache.directory.api.ldap.model.schema.MatchingRule;
033import org.apache.directory.api.ldap.model.schema.SchemaManager;
034import org.apache.directory.api.ldap.model.schema.comparators.UuidComparator;
035import org.apache.directory.server.i18n.I18n;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039
040/**
041 * A special index which stores DN objects.
042 * 
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 */
045public class JdbmDnIndex extends JdbmIndex<Dn>
046{
047
048    /** A logger for this class */
049    private static final Logger LOG = LoggerFactory.getLogger( JdbmDnIndex.class );
050
051
052    public JdbmDnIndex( String oid )
053    {
054        super( oid, true );
055        initialized = false;
056    }
057
058
059    /**
060     * {@inheritDoc}
061     */
062    @Override
063    public void init( RecordManager recMan, SchemaManager schemaManager, AttributeType attributeType ) throws LdapException, IOException
064    {
065        LOG.debug( "Initializing an Index for attribute '{}'", attributeType.getName() );
066
067        this.attributeType = attributeType;
068
069        if ( attributeId == null )
070        {
071            setAttributeId( attributeType.getName() );
072        }
073
074        if ( this.wkDirPath == null )
075        {
076            throw new NullPointerException( "The index working directory has not be set" );
077        }
078
079        String path = new File( this.wkDirPath, attributeType.getOid() ).getAbsolutePath();
080
081        this.recMan = recMan;
082
083        try
084        {
085            initTables( schemaManager );
086        }
087        catch ( IOException e )
088        {
089            // clean up
090            close( null );
091            throw e;
092        }
093
094        initialized = true;
095    }
096
097
098    /**
099     * Initializes the forward and reverse tables used by this Index.
100     * 
101     * @param schemaManager The server schemaManager
102     * @throws IOException if we cannot initialize the forward and reverse
103     * tables
104     * @throws NamingException
105     */
106    private void initTables( SchemaManager schemaManager ) throws IOException
107    {
108        MatchingRule mr = attributeType.getEquality();
109
110        if ( mr == null )
111        {
112            throw new IOException( I18n.err( I18n.ERR_574, attributeType.getName() ) );
113        }
114
115        DnSerializerComparator comp = new DnSerializerComparator( mr.getOid() );
116
117        UuidComparator.INSTANCE.setSchemaManager( schemaManager );
118
119        DnSerializer dnSerializer = new DnSerializer( schemaManager );
120
121        forward = new JdbmTable<Dn, String>( schemaManager, attributeType.getOid() + FORWARD_BTREE,
122            recMan, comp, dnSerializer, UuidSerializer.INSTANCE );
123        reverse = new JdbmTable<String, Dn>( schemaManager, attributeType.getOid() + REVERSE_BTREE,
124            recMan, UuidComparator.INSTANCE, UuidSerializer.INSTANCE, dnSerializer );
125    }
126}