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.IOException;
025
026import jdbm.RecordManager;
027
028import org.apache.directory.api.ldap.model.exception.LdapException;
029import org.apache.directory.api.ldap.model.schema.AttributeType;
030import org.apache.directory.api.ldap.model.schema.MatchingRule;
031import org.apache.directory.api.ldap.model.schema.SchemaManager;
032import org.apache.directory.api.ldap.model.schema.comparators.UuidComparator;
033import org.apache.directory.server.constants.ApacheSchemaConstants;
034import org.apache.directory.server.i18n.I18n;
035import org.apache.directory.server.xdbm.ParentIdAndRdn;
036import org.apache.directory.server.xdbm.ParentIdAndRdnComparator;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040
041/**
042 * A special index which stores Rdn objects.
043 * 
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 */
046public class JdbmRdnIndex extends JdbmIndex<ParentIdAndRdn>
047{
048
049    /** A logger for this class */
050    private static final Logger LOG = LoggerFactory.getLogger( JdbmRdnIndex.class );
051
052
053    public JdbmRdnIndex()
054    {
055        super( ApacheSchemaConstants.APACHE_RDN_AT_OID, true );
056        initialized = false;
057    }
058
059
060    /**
061     * {@inheritDoc}
062     */
063    @Override
064    public void init( RecordManager recMan, SchemaManager schemaManager, AttributeType attributeType ) throws LdapException, IOException
065    {
066        LOG.debug( "Initializing an Index for attribute '{}'", attributeType.getName() );
067
068        this.attributeType = attributeType;
069
070        if ( attributeId == null )
071        {
072            setAttributeId( attributeType.getName() );
073        }
074
075        if ( this.wkDirPath == null )
076        {
077            throw new NullPointerException( "The index working directory has not be set" );
078        }
079
080        //System.out.println( "IDX Created index " + path )
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        ParentIdAndRdnComparator<String> comp = new ParentIdAndRdnComparator<>( mr.getOid() );
116
117        UuidComparator.INSTANCE.setSchemaManager( schemaManager );
118
119        ParentIdAndRdnSerializer parentIdAndSerializer = new ParentIdAndRdnSerializer( schemaManager );
120
121        forward = new JdbmTable<ParentIdAndRdn, String>( schemaManager, attributeType.getOid() + FORWARD_BTREE,
122            recMan, comp, parentIdAndSerializer, UuidSerializer.INSTANCE );
123        reverse = new JdbmTable<String, ParentIdAndRdn>( schemaManager, attributeType.getOid() + REVERSE_BTREE,
124            recMan, UuidComparator.INSTANCE, UuidSerializer.INSTANCE, parentIdAndSerializer );
125    }
126}