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-2015 ForgeRock AS
026 */
027package org.opends.server.backends.jeb;
028
029import java.util.List;
030import java.util.Map;
031import java.util.Set;
032
033import org.forgerock.opendj.ldap.ByteString;
034import org.opends.server.types.Attribute;
035import org.opends.server.types.AttributeType;
036import org.opends.server.types.Entry;
037import org.opends.server.types.Modification;
038
039/**
040 * An implementation of an Indexer for attribute presence.
041 */
042public class PresenceIndexer extends Indexer
043{
044  /** The key bytes used for the presence index. */
045  static final byte[] presenceKeyBytes = "+".getBytes();
046
047  /** The key bytes used for the presence index as a {@link ByteString}. */
048  static final ByteString presenceKey = ByteString.wrap(presenceKeyBytes);
049
050  /** The attribute type for which this instance will generate index keys. */
051  private AttributeType attributeType;
052
053  /**
054   * Create a new attribute presence indexer.
055   * @param attributeType The attribute type for which the indexer
056   * is required.
057   */
058  public PresenceIndexer(AttributeType attributeType)
059  {
060    this.attributeType = attributeType;
061  }
062
063  /** {@inheritDoc} */
064  @Override
065  public String toString()
066  {
067    return attributeType.getNameOrOID() + ".presence";
068  }
069
070  /** {@inheritDoc} */
071  @Override
072  public void indexEntry(Entry entry, Set<ByteString> keys)
073  {
074    List<Attribute> attrList = entry.getAttribute(attributeType);
075    if (attrList != null && !attrList.isEmpty())
076    {
077      keys.add(presenceKey);
078    }
079  }
080
081  /** {@inheritDoc} */
082  @Override
083  public void modifyEntry(Entry oldEntry, Entry newEntry,
084                          List<Modification> mods,
085                          Map<ByteString, Boolean> modifiedKeys)
086  {
087    List<Attribute> newAttributes = newEntry.getAttribute(attributeType, true);
088    List<Attribute> oldAttributes = oldEntry.getAttribute(attributeType, true);
089    if(oldAttributes == null)
090    {
091      if(newAttributes != null)
092      {
093        modifiedKeys.put(presenceKey, true);
094      }
095    }
096    else
097    {
098      if(newAttributes == null)
099      {
100        modifiedKeys.put(presenceKey, false);
101      }
102    }
103  }
104}