001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2009 Sun Microsystems, Inc. 015 * Portions Copyright 2013-2015 ForgeRock AS. 016 */ 017package org.opends.server.types; 018 019import org.forgerock.opendj.ldap.ByteStringBuilder; 020import org.forgerock.opendj.ldap.ByteSequenceReader; 021 022import org.forgerock.i18n.LocalizableMessage; 023import org.opends.server.api.CompressedSchema; 024import org.opends.server.core.DirectoryServer; 025 026import static org.opends.messages.CoreMessages.*; 027 028/** 029 * This class defines a data structure that contains configuration 030 * information about how an entry should be encoded. 031 */ 032@org.opends.server.types.PublicAPI( 033 stability=org.opends.server.types.StabilityLevel.VOLATILE, 034 mayInstantiate=true, 035 mayExtend=false, 036 mayInvoke=true) 037public final class EntryEncodeConfig 038{ 039 /** 040 * The encode mask value that can be used to indicate that the 041 * encoded entry should not contain a DN. 042 */ 043 private static final byte ENCODE_FLAG_EXCLUDE_DN = 0x01; 044 045 046 047 /** 048 * The encode mask value that can be used that the encoded 049 * representation should compress the set of object classes. 050 */ 051 private static final byte ENCODE_FLAG_COMPRESS_OCS = 0x02; 052 053 054 055 /** 056 * The encode mask value that can be used that the encoded 057 * representation should compress the set of attribute descriptions 058 * to conserve space and improve performance. 059 */ 060 private static final byte ENCODE_FLAG_COMPRESS_ADS = 0x04; 061 062 063 064 /** 065 * A reference to an entry encode configuration with all the default 066 * settings. 067 */ 068 public static final EntryEncodeConfig 069 DEFAULT_CONFIG = new EntryEncodeConfig(); 070 071 072 073 /** Indicates whether to compress the attribute descriptions. */ 074 private final boolean compressAttrDescriptions; 075 076 /** Indicates whether to compress the object class sets. */ 077 private final boolean compressObjectClassSets; 078 079 /** Indicates whether to exclude the DN. */ 080 private final boolean excludeDN; 081 082 /** The encoded representation of this encode configuration. */ 083 private final byte encodedRepresentation; 084 085 /** The compressed schema handler for this encode configuration. */ 086 private final CompressedSchema compressedSchema; 087 088 089 090 /** 091 * Creates a new encoded entry configuration with the default 092 * settings. 093 */ 094 public EntryEncodeConfig() 095 { 096 excludeDN = false; 097 compressAttrDescriptions = false; 098 compressObjectClassSets = false; 099 100 compressedSchema = DirectoryServer.getDefaultCompressedSchema(); 101 102 encodedRepresentation = 0x00; 103 } 104 105 106 107 /** 108 * Creates a new encoded entry configuration with the specified 109 * settings. 110 * 111 * @param excludeDN Indicates whether to exclude 112 * the DN from the encoded entry. 113 * @param compressAttrDescriptions Indicates whether to compress 114 * attribute descriptions. 115 * @param compressObjectClassSets Indicates whether to compress 116 * object class sets. 117 */ 118 public EntryEncodeConfig(boolean excludeDN, 119 boolean compressAttrDescriptions, 120 boolean compressObjectClassSets) 121 { 122 this.excludeDN = excludeDN; 123 this.compressAttrDescriptions = compressAttrDescriptions; 124 this.compressObjectClassSets = compressObjectClassSets; 125 126 compressedSchema = DirectoryServer.getDefaultCompressedSchema(); 127 128 byte flagByte = 0x00; 129 if (excludeDN) 130 { 131 flagByte |= ENCODE_FLAG_EXCLUDE_DN; 132 } 133 134 if (compressAttrDescriptions) 135 { 136 flagByte |= ENCODE_FLAG_COMPRESS_ADS; 137 } 138 139 if (compressObjectClassSets) 140 { 141 flagByte |= ENCODE_FLAG_COMPRESS_OCS; 142 } 143 144 encodedRepresentation = flagByte; 145 } 146 147 148 149 /** 150 * Creates a new encoded entry configuration with the specified 151 * settings. 152 * 153 * @param excludeDN Indicates whether to exclude 154 * the DN from the encoded entry. 155 * @param compressAttrDescriptions Indicates whether to compress 156 * attribute descriptions. 157 * @param compressObjectClassSets Indicates whether to compress 158 * object class sets. 159 * @param compressedSchema The compressed schema manager 160 * for this encode config. 161 */ 162 public EntryEncodeConfig(boolean excludeDN, 163 boolean compressAttrDescriptions, 164 boolean compressObjectClassSets, 165 CompressedSchema compressedSchema) 166 { 167 this.excludeDN = excludeDN; 168 this.compressAttrDescriptions = compressAttrDescriptions; 169 this.compressObjectClassSets = compressObjectClassSets; 170 this.compressedSchema = compressedSchema; 171 172 byte flagByte = 0x00; 173 if (excludeDN) 174 { 175 flagByte |= ENCODE_FLAG_EXCLUDE_DN; 176 } 177 178 if (compressAttrDescriptions) 179 { 180 flagByte |= ENCODE_FLAG_COMPRESS_ADS; 181 } 182 183 if (compressObjectClassSets) 184 { 185 flagByte |= ENCODE_FLAG_COMPRESS_OCS; 186 } 187 188 encodedRepresentation = flagByte; 189 } 190 191 192 193 /** 194 * Indicates whether the encoded entry should exclude the DN. 195 * 196 * @return {@code true} if the encoded entry should exclude the DN, 197 * or {@code false} if not. 198 */ 199 public boolean excludeDN() 200 { 201 return excludeDN; 202 } 203 204 205 206 /** 207 * Indicates whether the encoded entry should use compressed 208 * attribute descriptions. 209 * 210 * @return {@code true} if the encoded entry should use compressed 211 * attribute descriptions, or {@code false} if not. 212 */ 213 public boolean compressAttributeDescriptions() 214 { 215 return compressAttrDescriptions; 216 } 217 218 219 220 /** 221 * Indicates whether the encoded entry should use compressed object 222 * class sets. 223 * 224 * @return {@code true} if the encoded entry should use compressed 225 * object class sets, or {@code false} if not. 226 */ 227 public boolean compressObjectClassSets() 228 { 229 return compressObjectClassSets; 230 } 231 232 233 234 /** 235 * Retrieves the compressed schema manager that may be used to 236 * generate compact schema encodings with this entry encode 237 * configuration. 238 * 239 * @return The compressed schema manager that may be used to 240 * generate compact schema encodings with this entry encode 241 * configuration. 242 */ 243 public CompressedSchema getCompressedSchema() 244 { 245 return compressedSchema; 246 } 247 248 249 250 /** 251 * Encodes this entry encode configuration into a byte array 252 * suitable for inclusion in the encoded entry. 253 * 254 * @param buffer The buffer to encode this configuration to. 255 */ 256 public void encode(ByteStringBuilder buffer) 257 { 258 buffer.appendBERLength(1); 259 buffer.appendByte(encodedRepresentation); 260 } 261 262 263 /** 264 * Decodes the entry encode configuration from current position and 265 * length of the given byte array. 266 * 267 * @param buffer The byte array containing the encoded 268 * entry. 269 * @param length The number of bytes contained in the 270 * encode configuration. 271 * @param compressedSchema The compressed schema manager to use 272 * when decoding. 273 * 274 * @return The decoded configuration. 275 * 276 * @throws DirectoryException If the configuration cannot be 277 * properly decoded. 278 */ 279 public static EntryEncodeConfig 280 decode(ByteSequenceReader buffer, int length, 281 CompressedSchema compressedSchema) 282 throws DirectoryException 283 { 284 if (length != 1) 285 { 286 LocalizableMessage message = ERR_ENTRYENCODECFG_INVALID_LENGTH.get(); 287 throw new DirectoryException( 288 DirectoryServer.getServerErrorResultCode(), 289 message); 290 } 291 292 byte b = buffer.readByte(); 293 boolean excludeDN = is(b, ENCODE_FLAG_EXCLUDE_DN); 294 boolean compressAttrDescriptions = is(b, ENCODE_FLAG_COMPRESS_ADS); 295 boolean compressObjectClassSets = is(b, ENCODE_FLAG_COMPRESS_OCS); 296 return new EntryEncodeConfig(excludeDN, compressAttrDescriptions, 297 compressObjectClassSets, 298 compressedSchema); 299 } 300 301 private static boolean is(byte b, byte flag) 302 { 303 return (b & flag) == flag; 304 } 305 306 /** 307 * Retrieves a string representation of this entry encode 308 * configuration. 309 * 310 * @return A string representation of this entry encode 311 * configuration. 312 */ 313 public String toString() 314 { 315 StringBuilder buffer = new StringBuilder(); 316 toString(buffer); 317 return buffer.toString(); 318 } 319 320 321 322 /** 323 * Appends a string representation of this entry encode 324 * configuration to the provided buffer. 325 * 326 * @param buffer The buffer to which the information should be 327 * appended. 328 */ 329 public void toString(StringBuilder buffer) 330 { 331 buffer.append("EntryEncodeConfig(excludeDN="); 332 buffer.append(excludeDN); 333 buffer.append(", compressAttrDescriptions="); 334 buffer.append(compressAttrDescriptions); 335 buffer.append(", compressObjectClassSets="); 336 buffer.append(compressObjectClassSets); 337 buffer.append(")"); 338 } 339} 340