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.xdbm.impl.avl;
022
023
024import org.apache.directory.api.ldap.model.exception.LdapException;
025import org.apache.directory.api.ldap.model.exception.LdapOtherException;
026import org.apache.directory.api.ldap.model.schema.AttributeType;
027import org.apache.directory.api.ldap.model.schema.MatchingRule;
028import org.apache.directory.api.ldap.model.schema.SchemaManager;
029import org.apache.directory.api.ldap.model.schema.comparators.UuidComparator;
030import org.apache.directory.server.i18n.I18n;
031import org.apache.directory.server.xdbm.ParentIdAndRdn;
032import org.apache.directory.server.xdbm.ParentIdAndRdnComparator;
033
034
035/**
036 * A special index which stores Rdn objects.
037 * 
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class AvlRdnIndex extends AvlIndex<ParentIdAndRdn>
041{
042    public AvlRdnIndex()
043    {
044        super();
045    }
046
047
048    public AvlRdnIndex( String attributeId )
049    {
050        super( attributeId, true );
051    }
052
053
054    @Override
055    public void init( SchemaManager schemaManager, AttributeType attributeType ) throws LdapException
056    {
057        this.attributeType = attributeType;
058
059        MatchingRule mr = attributeType.getEquality();
060
061        if ( mr == null )
062        {
063            mr = attributeType.getOrdering();
064        }
065
066        if ( mr == null )
067        {
068            mr = attributeType.getSubstring();
069        }
070
071        normalizer = mr.getNormalizer();
072
073        if ( normalizer == null )
074        {
075            throw new LdapOtherException( I18n.err( I18n.ERR_212, attributeType ) );
076        }
077
078        ParentIdAndRdnComparator<String> comp = new ParentIdAndRdnComparator<>( mr.getOid() );
079
080        UuidComparator.INSTANCE.setSchemaManager( schemaManager );
081
082        /*
083         * The forward key/value map stores attribute values to master table
084         * primary keys.  A value for an attribute can occur several times in
085         * different entries so the forward map can have more than one value.
086         */
087        forward = new AvlTable<ParentIdAndRdn, String>( attributeType.getName(), comp, UuidComparator.INSTANCE,
088            false );
089        reverse = new AvlTable<String, ParentIdAndRdn>( attributeType.getName(), UuidComparator.INSTANCE, comp,
090            false );
091    }
092}