001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.backends.jeb;
028    
029    import com.sleepycat.je.*;
030    
031    import org.opends.server.types.DN;
032    import org.opends.server.util.StaticUtils;
033    import static org.opends.server.loggers.debug.DebugLogger.*;
034    import org.opends.server.loggers.debug.DebugTracer;
035    
036    import java.util.Comparator;
037    
038    /**
039     * This class represents the DN database, or dn2id, which has one record
040     * for each entry.  The key is the normalized entry DN and the value
041     * is the entry ID.
042     */
043    public class DN2ID extends DatabaseContainer
044    {
045      /**
046       * The tracer object for the debug logger.
047       */
048      private static final DebugTracer TRACER = getTracer();
049    
050      /**
051       * The key comparator used for the DN database.
052       */
053      private Comparator<byte[]> dn2idComparator;
054    
055      /**
056       * Create a DN2ID instance for the DN database in a given entryContainer.
057       *
058       * @param name The name of the DN database.
059       * @param env The JE environment.
060       * @param entryContainer The entryContainer of the DN database.
061       * @throws DatabaseException If an error occurs in the JE database.
062       */
063      DN2ID(String name, Environment env, EntryContainer entryContainer)
064          throws DatabaseException
065      {
066        super(name, env, entryContainer);
067    
068        dn2idComparator = new EntryContainer.KeyReverseComparator();
069        DatabaseConfig dn2idConfig = new DatabaseConfig();
070    
071        if(env.getConfig().getReadOnly())
072        {
073          dn2idConfig.setReadOnly(true);
074          dn2idConfig.setAllowCreate(false);
075          dn2idConfig.setTransactional(false);
076        }
077        else if(!env.getConfig().getTransactional())
078        {
079          dn2idConfig.setAllowCreate(true);
080          dn2idConfig.setTransactional(false);
081          dn2idConfig.setDeferredWrite(true);
082        }
083        else
084        {
085          dn2idConfig.setAllowCreate(true);
086          dn2idConfig.setTransactional(true);
087        }
088    
089        this.dbConfig = dn2idConfig;
090        this.dbConfig.setBtreeComparator(dn2idComparator.getClass());
091      }
092    
093      /**
094       * Create a DN database key from an entry DN.
095       * @param dn The entry DN.
096       * @return A DatabaseEntry containing the key.
097       */
098      private static DatabaseEntry DNdata(DN dn)
099      {
100        byte[] normDN = StaticUtils.getBytes(dn.toNormalizedString());
101        return new DatabaseEntry(normDN);
102      }
103    
104      /**
105       * Insert a new record into the DN database.
106       * @param txn A JE database transaction to be used for the database operation,
107       * or null if none.
108       * @param dn The entry DN, which is the key to the record.
109       * @param id The entry ID, which is the value of the record.
110       * @return true if the record was inserted, false if a record with that key
111       * already exists.
112       * @throws DatabaseException If an error occurred while attempting to insert
113       * the new record.
114       */
115      public boolean insert(Transaction txn, DN dn, EntryID id)
116           throws DatabaseException
117      {
118        DatabaseEntry key = DNdata(dn);
119        DatabaseEntry data = id.getDatabaseEntry();
120    
121        OperationStatus status;
122    
123        status = insert(txn, key, data);
124        if (status != OperationStatus.SUCCESS)
125        {
126          return false;
127        }
128        return true;
129      }
130    
131      /**
132       * Write a record to the DN database.  If a record with the given key already
133       * exists, the record will be replaced, otherwise a new record will be
134       * inserted.
135       * @param txn A JE database transaction to be used for the database operation,
136       * or null if none.
137       * @param dn The entry DN, which is the key to the record.
138       * @param id The entry ID, which is the value of the record.
139       * @return true if the record was written, false if it was not written.
140       * @throws DatabaseException If an error occurred while attempting to write
141       * the record.
142       */
143      public boolean put(Transaction txn, DN dn, EntryID id)
144           throws DatabaseException
145      {
146        DatabaseEntry key = DNdata(dn);
147        DatabaseEntry data = id.getDatabaseEntry();
148    
149        OperationStatus status;
150        status = put(txn, key, data);
151        if (status != OperationStatus.SUCCESS)
152        {
153          return false;
154        }
155        return true;
156      }
157    
158      /**
159       * Write a record to the DN database, where the key and value are already
160       * formatted.
161       * @param txn A JE database transaction to be used for the database operation,
162       * or null if none.
163       * @param key A DatabaseEntry containing the record key.
164       * @param data A DatabaseEntry containing the record value.
165       * @return true if the record was written, false if it was not written.
166       * @throws DatabaseException If an error occurred while attempting to write
167       * the record.
168       */
169      public boolean putRaw(Transaction txn, DatabaseEntry key, DatabaseEntry data)
170           throws DatabaseException
171      {
172        OperationStatus status;
173        status = put(txn, key, data);
174        if (status != OperationStatus.SUCCESS)
175        {
176          return false;
177        }
178        return true;
179      }
180    
181      /**
182       * Remove a record from the DN database.
183       * @param txn A JE database transaction to be used for the database operation,
184       * or null if none.
185       * @param dn The entry DN, which is the key to the record.
186       * @return true if the record was removed, false if it was not removed.
187       * @throws DatabaseException If an error occurred while attempting to remove
188       * the record.
189       */
190      public boolean remove(Transaction txn, DN dn)
191           throws DatabaseException
192      {
193        DatabaseEntry key = DNdata(dn);
194    
195        OperationStatus status = delete(txn, key);
196        if (status != OperationStatus.SUCCESS)
197        {
198          return false;
199        }
200        return true;
201      }
202    
203      /**
204       * Fetch the entry ID for a given DN.
205       * @param txn A JE database transaction to be used for the database read, or
206       * null if none is required.
207       * @param dn The DN for which the entry ID is desired.
208       * @param lockMode The JE locking mode to be used for the read.
209       * @return The entry ID, or null if the given DN is not in the DN database.
210       * @throws DatabaseException If an error occurs in the JE database.
211       */
212      public EntryID get(Transaction txn, DN dn, LockMode lockMode)
213           throws DatabaseException
214      {
215        DatabaseEntry key = DNdata(dn);
216        DatabaseEntry data = new DatabaseEntry();
217    
218        OperationStatus status;
219        status = read(txn, key, data, LockMode.DEFAULT);
220        if (status != OperationStatus.SUCCESS)
221        {
222          return null;
223        }
224        return new EntryID(data);
225      }
226    
227      /**
228       * Gets the comparator for records stored in this database.
229       *
230       * @return The comparator for records stored in this database.
231       */
232      public Comparator<byte[]> getComparator()
233      {
234        return dn2idComparator;
235      }
236    }