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