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.IOException;
024
025import org.apache.directory.api.ldap.model.exception.LdapException;
026import org.apache.directory.api.ldap.model.name.Dn;
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.api.ldap.model.schema.comparators.UuidComparator;
031import org.apache.directory.mavibot.btree.serializer.StringSerializer;
032import org.apache.directory.server.i18n.I18n;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036
037/**
038 * A special index which stores DN objects.
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042public class MavibotDnIndex extends MavibotIndex<Dn>
043{
044
045    /** A logger for this class */
046    private static final Logger LOG = LoggerFactory.getLogger( MavibotDnIndex.class );
047
048
049    public MavibotDnIndex( String oid )
050    {
051        super( oid, true );
052        initialized = false;
053    }
054
055
056    /**
057     * {@inheritDoc}
058     */
059    @Override
060    public void init( SchemaManager schemaManager, AttributeType attributeType ) throws LdapException, IOException
061    {
062        LOG.debug( "Initializing an Index for attribute '{}'", attributeType.getName() );
063
064        this.attributeType = attributeType;
065
066        if ( attributeId == null )
067        {
068            setAttributeId( attributeType.getName() );
069        }
070
071        if ( this.wkDirPath == null )
072        {
073            throw new NullPointerException( "The index working directory has not be set" );
074        }
075
076        try
077        {
078            initTables( schemaManager );
079        }
080        catch ( IOException e )
081        {
082            // clean up
083            close( null );
084            throw e;
085        }
086
087        initialized = true;
088    }
089
090
091    private void initTables( SchemaManager schemaManager ) throws IOException
092    {
093        MatchingRule mr = attributeType.getEquality();
094
095        if ( mr == null )
096        {
097            throw new IOException( I18n.err( I18n.ERR_574, attributeType.getName() ) );
098        }
099
100        /*
101         * The forward key/value map stores attribute values to master table
102         * primary keys.  A value for an attribute can occur several times in
103         * different entries so the forward map can have more than one value.
104         */
105        UuidComparator.INSTANCE.setSchemaManager( schemaManager );
106
107        DnSerializer dnSerializer = new DnSerializer();
108
109        String forwardTableName = attributeType.getOid() + FORWARD_BTREE;
110        forward = new MavibotTable<Dn, String>( recordMan, schemaManager, forwardTableName, dnSerializer,
111            StringSerializer.INSTANCE, true );
112
113        String reverseTableName = attributeType.getOid() + REVERSE_BTREE;
114        reverse = new MavibotTable<String, Dn>( recordMan, schemaManager, reverseTableName, StringSerializer.INSTANCE,
115            dnSerializer, !attributeType.isSingleValued() );
116    }
117}