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.core.partition.impl.avl;
021
022
023import java.net.URI;
024
025import org.apache.directory.api.ldap.model.exception.LdapException;
026import org.apache.directory.api.ldap.model.schema.SchemaManager;
027import org.apache.directory.api.ldap.model.schema.comparators.UuidComparator;
028import org.apache.directory.server.constants.ApacheSchemaConstants;
029import org.apache.directory.server.core.api.DnFactory;
030import org.apache.directory.server.core.api.partition.Partition;
031import org.apache.directory.server.core.api.partition.PartitionReadTxn;
032import org.apache.directory.server.core.api.partition.PartitionWriteTxn;
033import org.apache.directory.server.core.partition.impl.btree.AbstractBTreePartition;
034import org.apache.directory.server.xdbm.Index;
035import org.apache.directory.server.xdbm.impl.avl.AvlIndex;
036import org.apache.directory.server.xdbm.impl.avl.AvlMasterTable;
037import org.apache.directory.server.xdbm.impl.avl.AvlRdnIndex;
038import org.apache.directory.server.xdbm.search.impl.CursorBuilder;
039import org.apache.directory.server.xdbm.search.impl.DefaultOptimizer;
040import org.apache.directory.server.xdbm.search.impl.DefaultSearchEngine;
041import org.apache.directory.server.xdbm.search.impl.EvaluatorBuilder;
042import org.apache.directory.server.xdbm.search.impl.NoOpOptimizer;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045
046
047/**
048 * An XDBM Partition backed by in memory AVL Trees.
049 *
050 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
051 */
052public class AvlPartition extends AbstractBTreePartition
053{
054    /** static logger */
055    private static final Logger LOG = LoggerFactory.getLogger( AvlPartition.class );
056
057
058    /**
059     * Creates a store based on AVL Trees.
060     * 
061     * @param schemaManager the schema manager
062     */
063    public AvlPartition( SchemaManager schemaManager )
064    {
065        super( schemaManager );
066    }
067
068
069    /**
070     * Creates a store based on AVL Trees.
071     *
072     * @param schemaManager the schema manager
073     * @param dnFactory the DN factory
074     */
075    public AvlPartition( SchemaManager schemaManager, DnFactory dnFactory )
076    {
077        super( schemaManager, dnFactory );
078    }
079    
080    
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    protected void doRepair() throws LdapException
086    {
087        // Nothing to do
088    }
089
090    
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    protected void doInit() throws LdapException
096    {
097        if ( !initialized )
098        {
099            EvaluatorBuilder evaluatorBuilder = new EvaluatorBuilder( this, schemaManager );
100            CursorBuilder cursorBuilder = new CursorBuilder( this, evaluatorBuilder );
101
102            // setup optimizer and registries for parent
103            if ( !optimizerEnabled )
104            {
105                setOptimizer( new NoOpOptimizer() );
106            }
107            else
108            {
109                setOptimizer( new DefaultOptimizer( this ) );
110            }
111
112            setSearchEngine( new DefaultSearchEngine( this, cursorBuilder, evaluatorBuilder, getOptimizer() ) );
113
114            if ( isInitialized() )
115            {
116                return;
117            }
118
119            // Create the master table (the table containing all the entries)
120            master = new AvlMasterTable( id, UuidComparator.INSTANCE, null, false );
121
122            super.doInit();
123        }
124    }
125
126
127    /**
128     * {@inheritDoc}
129     */
130    public String getDefaultId()
131    {
132        return Partition.DEFAULT_ID;
133    }
134
135
136    /**
137     * {@inheritDoc}
138     */
139    public String getRootId()
140    {
141        return Partition.ROOT_ID;
142    }
143
144
145    /**
146     * always returns false, cause this is a in-memory store
147     */
148    @Override
149    public boolean isSyncOnWrite()
150    {
151        return false;
152    }
153
154
155    /**
156     * Always returns 0 (zero), cause this is a in-memory store
157     */
158    @Override
159    public int getCacheSize()
160    {
161        return 0;
162    }
163
164
165    @Override
166    protected Index<?, String> convertAndInit( Index<?, String> index ) throws LdapException
167    {
168        AvlIndex<?> avlIndex;
169
170        if ( index.getAttributeId().equals( ApacheSchemaConstants.APACHE_RDN_AT_OID ) )
171        {
172            avlIndex = new AvlRdnIndex( index.getAttributeId() );
173        }
174        else if ( index instanceof AvlIndex<?> )
175        {
176            avlIndex = ( AvlIndex<?> ) index;
177        }
178        else
179        {
180            LOG.debug( "Supplied index {} is not a AvlIndex. "
181                + "Will create new AvlIndex using copied configuration parameters.", index );
182            avlIndex = new AvlIndex( index.getAttributeId(), true );
183        }
184
185        avlIndex.init( schemaManager, schemaManager.lookupAttributeTypeRegistry( index.getAttributeId() ) );
186
187        return avlIndex;
188    }
189
190
191    /**
192     * {@inheritDoc}
193     */
194    protected final Index createSystemIndex( String oid, URI path, boolean withReverse ) throws LdapException
195    {
196        LOG.debug( "Supplied index {} is not a JdbmIndex.  "
197            + "Will create new JdbmIndex using copied configuration parameters." );
198
199        AvlIndex<?> avlIndex;
200
201        if ( oid.equals( ApacheSchemaConstants.APACHE_RDN_AT_OID ) )
202        {
203            avlIndex = new AvlRdnIndex( oid );
204        }
205        else
206        {
207            LOG.debug( "Supplied index {} is not a AvlIndex. "
208                + "Will create new AvlIndex using copied configuration parameters." );
209            avlIndex = new AvlIndex( oid, withReverse );
210        }
211
212        return avlIndex;
213    }
214
215
216    /**
217     * {@inheritDoc}
218     */
219    @Override
220    public URI getPartitionPath()
221    {
222        // It's a in-memory partition, return null
223        return null;
224    }
225
226
227    @Override
228    public PartitionReadTxn beginReadTransaction()
229    {
230        return new PartitionReadTxn();
231    }
232
233
234    @Override
235    public PartitionWriteTxn beginWriteTransaction()
236    {
237        return new PartitionWriteTxn();
238    }
239}