View Javadoc
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, E> implements Index<K, E>
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      protected AbstractIndex()
60      {
61          this( null, true );
62      }
63  
64  
65      /**
66       * Creates a new instance of AbstractIndex.
67       * 
68       * @param withReverse If we should create a reverse index
69       */
70      protected AbstractIndex( boolean withReverse )
71      {
72          this( null, withReverse );
73      }
74  
75  
76      /**
77       * Creates a new instance of AbstractIndex.
78       * 
79       * @param attributeId the attribute ID
80       * @param withReverse If we should create a reverse index
81       */
82      protected AbstractIndex( String attributeId, boolean withReverse )
83      {
84          this.attributeId = attributeId;
85          this.withReverse = withReverse;
86          commitNumber = new AtomicInteger( 0 );
87      }
88  
89  
90      public String getAttributeId()
91      {
92          return attributeId;
93      }
94  
95  
96      /**
97       * {@inheritDoc}
98       */
99      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 }