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.xdbm;
021
022
023import java.util.concurrent.atomic.AtomicInteger;
024
025import org.apache.directory.api.ldap.model.schema.AttributeType;
026import org.apache.directory.server.i18n.I18n;
027
028
029/**
030 * A generic index implementation that is just used to hold the index configuration
031 * parameters (attributeId, cacheSize, wkDirPath). All other methods are not working.
032 *
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public abstract class AbstractIndex<K, E> implements Index<K, E>
036{
037    /** The attribute identifier for this index */
038    protected String attributeId;
039
040    /** the attribute type resolved for this JdbmIndex */
041    protected AttributeType attributeType;
042
043    /** the size (number of index entries) for the cache */
044    protected int cacheSize = DEFAULT_INDEX_CACHE_SIZE;
045
046    /** whether or not this index has been initialized */
047    protected boolean initialized;
048
049    /** Tells if this index has a Reverse table */
050    protected boolean withReverse;
051
052    /** A counter used to differ the commit on disk after N operations */
053    protected AtomicInteger commitNumber;
054
055
056    /**
057     * Creates a new instance of AbstractIndex.
058     */
059    protected AbstractIndex()
060    {
061        this( null, true );
062    }
063
064
065    /**
066     * Creates a new instance of AbstractIndex.
067     * 
068     * @param withReverse If we should create a reverse index
069     */
070    protected AbstractIndex( boolean withReverse )
071    {
072        this( null, withReverse );
073    }
074
075
076    /**
077     * Creates a new instance of AbstractIndex.
078     * 
079     * @param attributeId the attribute ID
080     * @param withReverse If we should create a reverse index
081     */
082    protected AbstractIndex( String attributeId, boolean withReverse )
083    {
084        this.attributeId = attributeId;
085        this.withReverse = withReverse;
086        commitNumber = new AtomicInteger( 0 );
087    }
088
089
090    public String getAttributeId()
091    {
092        return attributeId;
093    }
094
095
096    /**
097     * {@inheritDoc}
098     */
099    public AttributeType getAttribute()
100    {
101        return attributeType;
102    }
103
104
105    public void setAttributeId( String attributeId )
106    {
107        protect( "attributeId" );
108        this.attributeId = attributeId;
109    }
110
111
112    /**
113     * {@inheritDoc}
114     */
115    public boolean isDupsEnabled()
116    {
117        return !attributeType.isSingleValued();
118    }
119
120
121    /**
122     * Gets the size of the index cache in terms of the number of index entries to be cached.
123     *
124     * @return the size of the index cache
125     */
126    public int getCacheSize()
127    {
128        return cacheSize;
129    }
130
131
132    /**
133     * Sets the size of the index cache in terms of the number of index entries to be cached.
134     *
135     * @param cacheSize the size of the index cache
136     */
137    public void setCacheSize( int cacheSize )
138    {
139        protect( "cacheSize" );
140        this.cacheSize = cacheSize;
141    }
142
143
144    /**
145     * Protects configuration properties from being set after initialization.
146     *
147     * @param property the property to protect
148     */
149    protected void protect( String property )
150    {
151        if ( initialized )
152        {
153            throw new IllegalStateException( I18n.err( I18n.ERR_575, property ) );
154        }
155    }
156
157
158    /**
159     * {@inheritDoc}
160     */
161    public boolean hasReverse()
162    {
163        return withReverse;
164    }
165}