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 2014 ForgeRock AS
026 */
027package org.opends.server.backends.jeb;
028
029import org.forgerock.util.Reject;
030import org.opends.server.api.CompressedSchema;
031import org.opends.server.types.EntryEncodeConfig;
032
033/**
034 * Configuration class to indicate desired compression and cryptographic options
035 * for the data stored in the database.
036 */
037public final class DataConfig
038{
039  /** Indicates whether data should be compressed before writing to the database. */
040  private boolean compressed;
041
042  /** The configuration to use when encoding entries in the database. */
043  private EntryEncodeConfig encodeConfig = new EntryEncodeConfig();
044
045  /**
046   * Construct a new DataConfig object with the specified settings.
047   *
048   * @param compressed true if data should be compressed, false if not.
049   * @param compactEncoding true if data should be encoded in compact form,
050   * false if not.
051   * @param compressedSchema the compressed schema manager to use.  It must not
052   * be {@code null} if compactEncoding is {@code true}.
053   */
054  public DataConfig(boolean compressed, boolean compactEncoding, CompressedSchema compressedSchema)
055  {
056    this.compressed = compressed;
057    setCompactEncoding(compactEncoding, compressedSchema);
058  }
059
060  /**
061   * Determine whether data should be compressed before writing to the database.
062   * @return true if data should be compressed, false if not.
063   */
064  public boolean isCompressed()
065  {
066    return compressed;
067  }
068
069  /**
070   * Determine whether entries should be encoded with the compact form before
071   * writing to the database.
072   * @return true if data should be encoded in the compact form.
073   */
074  public boolean isCompactEncoding()
075  {
076    return encodeConfig.compressAttributeDescriptions();
077  }
078
079  /**
080   * Configure whether data should be compressed before writing to the database.
081   * @param compressed true if data should be compressed, false if not.
082   */
083  public void setCompressed(boolean compressed)
084  {
085    this.compressed = compressed;
086  }
087
088  /**
089   * Configure whether data should be encoded with the compact form before
090   * writing to the database.
091   * @param compactEncoding true if data should be encoded in compact form,
092   * false if not.
093   * @param compressedSchema The compressed schema manager to use.  It must not
094   * be {@code null} if compactEncoding is {@code true}.
095   */
096  public void setCompactEncoding(boolean compactEncoding, CompressedSchema compressedSchema)
097  {
098    if (compressedSchema == null)
099    {
100      Reject.ifTrue(compactEncoding);
101      this.encodeConfig = new EntryEncodeConfig(false, compactEncoding, false);
102    }
103    else
104    {
105      this.encodeConfig = new EntryEncodeConfig(false, compactEncoding, compactEncoding, compressedSchema);
106    }
107  }
108
109  /**
110   * Get the EntryEncodeConfig object in use by this configuration.
111   * @return the EntryEncodeConfig object in use by this configuration.
112   */
113  public EntryEncodeConfig getEntryEncodeConfig()
114  {
115    return this.encodeConfig;
116  }
117
118  /**
119   * Get a string representation of this object.
120   * @return A string representation of this object.
121   */
122  @Override
123  public String toString()
124  {
125    final StringBuilder builder = new StringBuilder();
126    builder.append("DataConfig(compressed=");
127    builder.append(compressed);
128    builder.append(", ");
129    encodeConfig.toString(builder);
130    builder.append(")");
131    return builder.toString();
132  }
133}