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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.backends.jeb;
028
029import static com.sleepycat.je.LockMode.*;
030import static com.sleepycat.je.OperationStatus.*;
031
032import static org.opends.server.backends.jeb.JebFormat.*;
033
034import java.util.Comparator;
035
036import org.opends.server.types.DN;
037
038import com.sleepycat.je.*;
039
040/**
041 * This class represents the DN database, or dn2id, which has one record
042 * for each entry.  The key is the normalized entry DN and the value
043 * is the entry ID.
044 */
045public class DN2ID extends DatabaseContainer
046{
047  /** The key comparator used for the DN database. */
048  private final Comparator<byte[]> comparator;
049  private final int prefixRDNComponents;
050
051  /**
052   * Create a DN2ID instance for the DN database in a given entryContainer.
053   *
054   * @param name The name of the DN database.
055   * @param env The JE environment.
056   * @param entryContainer The entryContainer of the DN database.
057   * @throws DatabaseException If an error occurs in the JE database.
058   */
059  @SuppressWarnings("unchecked")
060  DN2ID(String name, Environment env, EntryContainer entryContainer)
061      throws DatabaseException
062  {
063    super(name, env, entryContainer);
064
065    comparator = new AttributeIndex.KeyComparator();
066    prefixRDNComponents = entryContainer.getBaseDN().size();
067
068    this.dbConfig = JEBUtils.toDatabaseConfigNoDuplicates(env);
069    this.dbConfig.setKeyPrefixing(true);
070    this.dbConfig.setBtreeComparator((Class<? extends Comparator<byte[]>>) comparator.getClass());
071  }
072
073  /**
074   * Insert a new record into the DN database.
075   * @param txn A JE database transaction to be used for the database operation,
076   * or null if none.
077   * @param dn The entry DN, which is the key to the record.
078   * @param id The entry ID, which is the value of the record.
079   * @return true if the record was inserted, false if a record with that key
080   * already exists.
081   * @throws DatabaseException If an error occurred while attempting to insert
082   * the new record.
083   */
084  boolean insert(Transaction txn, DN dn, EntryID id) throws DatabaseException
085  {
086    DatabaseEntry key = new DatabaseEntry(dnToDNKey(dn, prefixRDNComponents));
087    DatabaseEntry data = id.getDatabaseEntry();
088
089    return insert(txn, key, data) == SUCCESS;
090  }
091
092  /**
093   * Write a record to the DN database, where the key and value are already
094   * formatted.
095   * @param txn A JE database transaction to be used for the database operation,
096   * or null if none.
097   * @param key A DatabaseEntry containing the record key.
098   * @param data A DatabaseEntry containing the record value.
099   * @return true if the record was written, false if it was not written.
100   * @throws DatabaseException If an error occurred while attempting to write
101   * the record.
102   */
103  @Override
104  public OperationStatus put(Transaction txn, DatabaseEntry key, DatabaseEntry data) throws DatabaseException
105  {
106    return super.put(txn, key, data);
107  }
108
109  /**
110   * Remove a record from the DN database.
111   * @param txn A JE database transaction to be used for the database operation,
112   * or null if none.
113   * @param dn The entry DN, which is the key to the record.
114   * @return true if the record was removed, false if it was not removed.
115   * @throws DatabaseException If an error occurred while attempting to remove
116   * the record.
117   */
118  boolean remove(Transaction txn, DN dn) throws DatabaseException
119  {
120    DatabaseEntry key = new DatabaseEntry(dnToDNKey(dn, prefixRDNComponents));
121
122    return delete(txn, key) == SUCCESS;
123  }
124
125  /**
126   * Fetch the entry ID for a given DN.
127   * @param txn A JE database transaction to be used for the database read, or
128   * null if none is required.
129   * @param dn The DN for which the entry ID is desired.
130   * @param lockMode The JE locking mode to be used for the read.
131   * @return The entry ID, or null if the given DN is not in the DN database.
132   * @throws DatabaseException If an error occurs in the JE database.
133   */
134  public EntryID get(Transaction txn, DN dn, LockMode lockMode) throws DatabaseException
135  {
136    DatabaseEntry key = new DatabaseEntry(dnToDNKey(dn, prefixRDNComponents));
137    DatabaseEntry data = new DatabaseEntry();
138
139    if (read(txn, key, data, DEFAULT) == SUCCESS)
140    {
141      return new EntryID(data);
142    }
143    return null;
144  }
145
146  /** {@inheritDoc} */
147  @Override
148  public OperationStatus read(Transaction txn, DatabaseEntry key, DatabaseEntry data, LockMode lockMode)
149  {
150    return super.read(txn, key, data, lockMode);
151  }
152
153  /**
154   * Gets the comparator for records stored in this database.
155   *
156   * @return The comparator for records stored in this database.
157   */
158  public Comparator<byte[]> getComparator()
159  {
160    return comparator;
161  }
162}