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  
21  package org.apache.directory.server.core.partition.impl.btree.jdbm;
22  
23  
24  import java.io.File;
25  import java.io.IOException;
26  
27  import jdbm.RecordManager;
28  
29  import org.apache.directory.api.ldap.model.exception.LdapException;
30  import org.apache.directory.api.ldap.model.name.Dn;
31  import org.apache.directory.api.ldap.model.schema.AttributeType;
32  import org.apache.directory.api.ldap.model.schema.MatchingRule;
33  import org.apache.directory.api.ldap.model.schema.SchemaManager;
34  import org.apache.directory.api.ldap.model.schema.comparators.UuidComparator;
35  import org.apache.directory.server.i18n.I18n;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  
40  /**
41   * A special index which stores DN objects.
42   * 
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public class JdbmDnIndex extends JdbmIndex<Dn>
46  {
47  
48      /** A logger for this class */
49      private static final Logger LOG = LoggerFactory.getLogger( JdbmDnIndex.class );
50  
51  
52      public JdbmDnIndex( String oid )
53      {
54          super( oid, true );
55          initialized = false;
56      }
57  
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public void init( RecordManager recMan, SchemaManager schemaManager, AttributeType attributeType ) throws LdapException, IOException
64      {
65          LOG.debug( "Initializing an Index for attribute '{}'", attributeType.getName() );
66  
67          this.attributeType = attributeType;
68  
69          if ( attributeId == null )
70          {
71              setAttributeId( attributeType.getName() );
72          }
73  
74          if ( this.wkDirPath == null )
75          {
76              throw new NullPointerException( "The index working directory has not be set" );
77          }
78  
79          String path = new File( this.wkDirPath, attributeType.getOid() ).getAbsolutePath();
80  
81          this.recMan = recMan;
82  
83          try
84          {
85              initTables( schemaManager );
86          }
87          catch ( IOException e )
88          {
89              // clean up
90              close( null );
91              throw e;
92          }
93  
94          initialized = true;
95      }
96  
97  
98      /**
99       * Initializes the forward and reverse tables used by this Index.
100      * 
101      * @param schemaManager The server schemaManager
102      * @throws IOException if we cannot initialize the forward and reverse
103      * tables
104      * @throws NamingException
105      */
106     private void initTables( SchemaManager schemaManager ) throws IOException
107     {
108         MatchingRule mr = attributeType.getEquality();
109 
110         if ( mr == null )
111         {
112             throw new IOException( I18n.err( I18n.ERR_574, attributeType.getName() ) );
113         }
114 
115         DnSerializerComparator/core/partition/impl/btree/jdbm/DnSerializerComparator.html#DnSerializerComparator">DnSerializerComparator comp = new DnSerializerComparator( mr.getOid() );
116 
117         UuidComparator.INSTANCE.setSchemaManager( schemaManager );
118 
119         DnSerializerer/core/partition/impl/btree/jdbm/DnSerializer.html#DnSerializer">DnSerializer dnSerializer = new DnSerializer( schemaManager );
120 
121         forward = new JdbmTable<Dn, String>( schemaManager, attributeType.getOid() + FORWARD_BTREE,
122             recMan, comp, dnSerializer, UuidSerializer.INSTANCE );
123         reverse = new JdbmTable<String, Dn>( schemaManager, attributeType.getOid() + REVERSE_BTREE,
124             recMan, UuidComparator.INSTANCE, UuidSerializer.INSTANCE, dnSerializer );
125     }
126 }