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.IOException;
25  
26  import jdbm.RecordManager;
27  
28  import org.apache.directory.api.ldap.model.exception.LdapException;
29  import org.apache.directory.api.ldap.model.schema.AttributeType;
30  import org.apache.directory.api.ldap.model.schema.MatchingRule;
31  import org.apache.directory.api.ldap.model.schema.SchemaManager;
32  import org.apache.directory.api.ldap.model.schema.comparators.UuidComparator;
33  import org.apache.directory.server.constants.ApacheSchemaConstants;
34  import org.apache.directory.server.i18n.I18n;
35  import org.apache.directory.server.xdbm.ParentIdAndRdn;
36  import org.apache.directory.server.xdbm.ParentIdAndRdnComparator;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  
41  /**
42   * A special index which stores Rdn objects.
43   * 
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class JdbmRdnIndex extends JdbmIndex<ParentIdAndRdn>
47  {
48  
49      /** A logger for this class */
50      private static final Logger LOG = LoggerFactory.getLogger( JdbmRdnIndex.class );
51  
52  
53      public JdbmRdnIndex()
54      {
55          super( ApacheSchemaConstants.APACHE_RDN_AT_OID, true );
56          initialized = false;
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public void init( RecordManager recMan, SchemaManager schemaManager, AttributeType attributeType ) throws LdapException, IOException
65      {
66          LOG.debug( "Initializing an Index for attribute '{}'", attributeType.getName() );
67  
68          this.attributeType = attributeType;
69  
70          if ( attributeId == null )
71          {
72              setAttributeId( attributeType.getName() );
73          }
74  
75          if ( this.wkDirPath == null )
76          {
77              throw new NullPointerException( "The index working directory has not be set" );
78          }
79  
80          //System.out.println( "IDX Created index " + path )
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         ParentIdAndRdnComparator<String> comp = new ParentIdAndRdnComparator<>( mr.getOid() );
116 
117         UuidComparator.INSTANCE.setSchemaManager( schemaManager );
118 
119         ParentIdAndRdnSerializerl/btree/jdbm/ParentIdAndRdnSerializer.html#ParentIdAndRdnSerializer">ParentIdAndRdnSerializer parentIdAndSerializer = new ParentIdAndRdnSerializer( schemaManager );
120 
121         forward = new JdbmTable<ParentIdAndRdn, String>( schemaManager, attributeType.getOid() + FORWARD_BTREE,
122             recMan, comp, parentIdAndSerializer, UuidSerializer.INSTANCE );
123         reverse = new JdbmTable<String, ParentIdAndRdn>( schemaManager, attributeType.getOid() + REVERSE_BTREE,
124             recMan, UuidComparator.INSTANCE, UuidSerializer.INSTANCE, parentIdAndSerializer );
125     }
126 }