1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.directory.server.xdbm; 21 22 23 import java.util.concurrent.atomic.AtomicInteger; 24 25 import org.apache.directory.api.ldap.model.schema.AttributeType; 26 import org.apache.directory.server.i18n.I18n; 27 28 29 /** 30 * A generic index implementation that is just used to hold the index configuration 31 * parameters (attributeId, cacheSize, wkDirPath). All other methods are not working. 32 * 33 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 34 */ 35 public abstract class AbstractIndex<K, O, ID> implements Index<K, O, ID> 36 { 37 /** The attribute identifier for this index */ 38 protected String attributeId; 39 40 /** the attribute type resolved for this JdbmIndex */ 41 protected AttributeType attributeType; 42 43 /** the size (number of index entries) for the cache */ 44 protected int cacheSize = DEFAULT_INDEX_CACHE_SIZE; 45 46 /** whether or not this index has been initialized */ 47 protected boolean initialized; 48 49 /** Tells if this index has a Reverse table */ 50 protected boolean withReverse; 51 52 /** A counter used to differ the commit on disk after N operations */ 53 protected AtomicInteger commitNumber; 54 55 56 /** 57 * Creates a new instance of AbstractIndex. 58 * 59 * @param attributeId the attribute ID 60 */ 61 protected AbstractIndex() 62 { 63 this( null, true ); 64 } 65 66 67 /** 68 * Creates a new instance of AbstractIndex. 69 * 70 * @param attributeId the attribute ID 71 */ 72 protected AbstractIndex( boolean withReverse ) 73 { 74 this( null, withReverse ); 75 } 76 77 78 /** 79 * Creates a new instance of AbstractIndex. 80 * 81 * @param attributeId the attribute ID 82 */ 83 protected AbstractIndex( String attributeId, boolean withReverse ) 84 { 85 this.attributeId = attributeId; 86 this.withReverse = withReverse; 87 commitNumber = new AtomicInteger( 0 ); 88 } 89 90 91 public String getAttributeId() 92 { 93 return attributeId; 94 } 95 96 97 /** 98 * {@inheritDoc} 99 */ 100 public AttributeType getAttribute() 101 { 102 return attributeType; 103 } 104 105 106 public void setAttributeId( String attributeId ) 107 { 108 protect( "attributeId" ); 109 this.attributeId = attributeId; 110 } 111 112 113 /** 114 * {@inheritDoc} 115 */ 116 public boolean isDupsEnabled() 117 { 118 return !attributeType.isSingleValued(); 119 } 120 121 122 /** 123 * Gets the size of the index cache in terms of the number of index entries to be cached. 124 * 125 * @return the size of the index cache 126 */ 127 public int getCacheSize() 128 { 129 return cacheSize; 130 } 131 132 133 /** 134 * Sets the size of the index cache in terms of the number of index entries to be cached. 135 * 136 * @param cacheSize the size of the index cache 137 */ 138 public void setCacheSize( int cacheSize ) 139 { 140 protect( "cacheSize" ); 141 this.cacheSize = cacheSize; 142 } 143 144 145 /** 146 * Protects configuration properties from being set after initialization. 147 * 148 * @param property the property to protect 149 */ 150 protected void protect( String property ) 151 { 152 if ( initialized ) 153 { 154 throw new IllegalStateException( I18n.err( I18n.ERR_575, property ) ); 155 } 156 } 157 158 159 /** 160 * {@inheritDoc} 161 */ 162 public boolean hasReverse() 163 { 164 return withReverse; 165 } 166 }