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.Comparator; 024import java.util.concurrent.atomic.AtomicInteger; 025 026import org.apache.directory.api.ldap.model.exception.LdapException; 027import org.apache.directory.api.ldap.model.schema.SchemaManager; 028import org.apache.directory.server.core.api.partition.PartitionTxn; 029import org.apache.directory.server.i18n.I18n; 030 031 032/** 033 * A Abstract Table implementation aggregating the methods common with all the 034 * different Table implementation. 035 * 036 * @param <K> The key 037 * @param <V> The stored value 038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 039 */ 040public abstract class AbstractTable<K, V> implements Table<K, V> 041{ 042 /** the name of this table */ 043 protected final String name; 044 045 /** The global SchemaManager */ 046 protected SchemaManager schemaManager; 047 048 /** a key comparator for the keys in this Table */ 049 protected final Comparator<K> keyComparator; 050 051 /** a value comparator for the values in this Table */ 052 protected final Comparator<V> valueComparator; 053 054 /** the current count of Tuples in this Table */ 055 protected long count; 056 057 /** whether or not this table allows for duplicates */ 058 protected boolean allowsDuplicates; 059 060 /** A counter used to differ the commit on disk after N operations */ 061 protected AtomicInteger commitNumber; 062 063 064 /** 065 * Create an instance of Table 066 * 067 * @param schemaManager The server schemaManager 068 * @param name the name of the table 069 * @param keyComparator a key comparator 070 * @param valueComparator a value comparator 071 */ 072 protected AbstractTable( SchemaManager schemaManager, String name, Comparator<K> keyComparator, 073 Comparator<V> valueComparator ) 074 { 075 this.schemaManager = schemaManager; 076 this.name = name; 077 078 if ( keyComparator == null ) 079 { 080 throw new IllegalArgumentException( I18n.err( I18n.ERR_591 ) ); 081 } 082 else 083 { 084 this.keyComparator = keyComparator; 085 } 086 087 this.valueComparator = valueComparator; 088 089 commitNumber = new AtomicInteger( 0 ); 090 } 091 092 093 /** 094 * {@inheritDoc} 095 */ 096 public Comparator<K> getKeyComparator() 097 { 098 return keyComparator; 099 } 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}