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.Comparator;
24  import java.util.concurrent.atomic.AtomicInteger;
25  
26  import org.apache.directory.api.ldap.model.exception.LdapException;
27  import org.apache.directory.api.ldap.model.schema.SchemaManager;
28  import org.apache.directory.server.core.api.partition.PartitionTxn;
29  import org.apache.directory.server.i18n.I18n;
30  
31  
32  /**
33   * A Abstract Table implementation aggregating the methods common with all the 
34   * different Table implementation.
35   *
36   * @param <K> The key
37   * @param <V> The stored value
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public abstract class AbstractTable<K, V> implements Table<K, V>
41  {
42      /** the name of this table */
43      protected final String name;
44  
45      /** The global SchemaManager */
46      protected SchemaManager schemaManager;
47  
48      /** a key comparator for the keys in this Table */
49      protected final Comparator<K> keyComparator;
50  
51      /** a value comparator for the values in this Table */
52      protected final Comparator<V> valueComparator;
53  
54      /** the current count of Tuples in this Table */
55      protected long count;
56  
57      /** whether or not this table allows for duplicates */
58      protected boolean allowsDuplicates;
59  
60      /** A counter used to differ the commit on disk after N operations */
61      protected AtomicInteger commitNumber;
62  
63  
64      /**
65       * Create an instance of Table
66       * 
67       * @param schemaManager The server schemaManager
68       * @param name the name of the table
69       * @param keyComparator a key comparator
70       * @param valueComparator a value comparator
71       */
72      protected AbstractTable( SchemaManager schemaManager, String name, Comparator<K> keyComparator,
73          Comparator<V> valueComparator )
74      {
75          this.schemaManager = schemaManager;
76          this.name = name;
77  
78          if ( keyComparator == null )
79          {
80              throw new IllegalArgumentException( I18n.err( I18n.ERR_591 ) );
81          }
82          else
83          {
84              this.keyComparator = keyComparator;
85          }
86  
87          this.valueComparator = valueComparator;
88  
89          commitNumber = new AtomicInteger( 0 );
90      }
91  
92  
93      /**
94       * {@inheritDoc}
95       */
96      public Comparator<K> getKeyComparator()
97      {
98          return keyComparator;
99      }
100 
101 
102     /**
103      * {@inheritDoc}
104      */
105     public Comparator<V> getValueComparator()
106     {
107         return valueComparator;
108     }
109 
110 
111     /**
112      * {@inheritDoc}
113      */
114     public String getName()
115     {
116         return name;
117     }
118 
119 
120     /**
121      * {@inheritDoc}
122      */
123     public long count( PartitionTxn transaction ) throws LdapException
124     {
125         return count;
126     }
127 
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public long greaterThanCount( PartitionTxn transaction, K key ) throws LdapException
134     {
135         // take a best guess
136         return Math.min( count, 10L );
137     }
138 
139 
140     /**
141      * {@inheritDoc}
142      */
143     @Override
144     public long lessThanCount( PartitionTxn transaction, K key ) throws LdapException
145     {
146         // take a best guess
147         return Math.min( count, 10L );
148     }
149 
150 
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     public boolean isDupsEnabled()
156     {
157         return allowsDuplicates;
158     }
159 
160 
161     /**
162      * @see Object#toString()
163      */
164     public String toString()
165     {
166         StringBuilder sb = new StringBuilder();
167 
168         sb.append( "Name    : " ).append( name ).append( '\n' );
169         sb.append( "NbElems : " ).append( count ).append( '\n' );
170         sb.append( "Dups    : " ).append( allowsDuplicates ).append( '\n' );
171         sb.append( "Key     : " ).append( keyComparator.getClass().getName() ).append( '\n' );
172         sb.append( "Value   : " ).append( valueComparator.getClass().getName() ).append( '\n' );
173 
174         return sb.toString();
175     }
176 }