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-2008 Sun Microsystems, Inc.
025 *      Portions copyright 2011-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 java.util.Arrays;
033
034import org.opends.server.util.StaticUtils;
035
036import com.sleepycat.je.*;
037
038/**
039 * This class is responsible for storing the configuration state of
040 * the JE backend for a particular suffix.
041 */
042public class State extends DatabaseContainer
043{
044  private static final byte[] falseBytes = new byte[]{0x00};
045  private static final byte[] trueBytes = new byte[]{0x01};
046
047  /**
048   * Create a new State object.
049   *
050   * @param name The name of the entry database.
051   * @param env The JE Environment.
052   * @param entryContainer The entryContainer of the entry database.
053   * @throws com.sleepycat.je.DatabaseException If an error occurs in the
054   * JE database.
055   *
056   */
057  State(String name, Environment env, EntryContainer entryContainer)
058      throws DatabaseException
059  {
060    super(name, env, entryContainer);
061    this.dbConfig = JEBUtils.toDatabaseConfigNoDuplicates(env);
062  }
063
064  /**
065   * Return the key associated with the index in the state database.
066   *
067   * @param index The index we need the key for.
068   * @return the key
069   * @throws DatabaseException If an error occurs in the JE database.
070   */
071  private DatabaseEntry keyForIndex(DatabaseContainer index)
072    throws DatabaseException
073  {
074    String shortName =
075      index.getName().replace(entryContainer.getDatabasePrefix(), "");
076    return new DatabaseEntry(StaticUtils.getBytes(shortName));
077  }
078
079  /**
080   * Remove a record from the entry database.
081   *
082   * @param txn The database transaction or null if none.
083   * @param index The index storing the trusted state info.
084   * @return true if the entry was removed, false if it was not.
085   * @throws DatabaseException If an error occurs in the JE database.
086   */
087  boolean removeIndexTrustState(Transaction txn, DatabaseContainer index)
088       throws DatabaseException
089  {
090    DatabaseEntry key = keyForIndex(index);
091
092    return delete(txn, key) == SUCCESS;
093  }
094
095  /**
096   * Fetch index state from the database.
097   * @param txn The database transaction or null if none.
098   * @param index The index storing the trusted state info.
099   * @return The trusted state of the index in the database.
100   * @throws DatabaseException If an error occurs in the JE database.
101   */
102  public boolean getIndexTrustState(Transaction txn, DatabaseContainer index)
103      throws DatabaseException
104  {
105    DatabaseEntry key = keyForIndex(index);
106    DatabaseEntry data = new DatabaseEntry();
107
108    if (read(txn, key, data, DEFAULT) == SUCCESS)
109    {
110      byte[] bytes = data.getData();
111      return Arrays.equals(bytes, trueBytes);
112    }
113    return false;
114  }
115
116  /**
117   * Put index state to database.
118   * @param txn The database transaction or null if none.
119   * @param index The index storing the trusted state info.
120   * @param trusted The state value to put into the database.
121   * @throws DatabaseException If an error occurs in the JE database.
122   */
123  void putIndexTrustState(Transaction txn, DatabaseContainer index, boolean trusted)
124       throws DatabaseException
125  {
126    DatabaseEntry key = keyForIndex(index);
127    DatabaseEntry data = new DatabaseEntry();
128
129    data.setData(trusted ? trueBytes : falseBytes);
130    put(txn, key, data);
131  }
132
133}