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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.backends.jeb;
028    
029    import org.opends.server.types.Attribute;
030    import org.opends.server.types.Entry;
031    import org.opends.server.types.Modification;
032    import org.opends.server.types.AttributeType;
033    
034    import java.util.Comparator;
035    import java.util.List;
036    import java.util.Set;
037    import java.util.Map;
038    
039    /**
040     * An implementation of an Indexer for attribute presence.
041     */
042    public class PresenceIndexer extends Indexer
043    {
044    
045    
046      /**
047       * The comparator for index keys generated by this class.
048       */
049      private static final Comparator<byte[]> comparator =
050           new AttributeIndex.KeyComparator();
051    
052      /**
053       * The attribute type for which this instance will
054       * generate index keys.
055       */
056      private AttributeType attributeType;
057    
058      /**
059       * Create a new attribute presence indexer.
060       * @param attributeType The attribute type for which the indexer
061       * is required.
062       */
063      public PresenceIndexer(AttributeType attributeType)
064      {
065        this.attributeType = attributeType;
066      }
067    
068      /**
069       * Get a string representation of this object.
070       * @return A string representation of this object.
071       */
072      public String toString()
073      {
074        return attributeType.getNameOrOID() + ".presence";
075      }
076    
077      /**
078       * Get the comparator that must be used to compare index keys
079       * generated by this class.
080       *
081       * @return A byte array comparator.
082       */
083      public Comparator<byte[]> getComparator()
084      {
085        return comparator;
086      }
087    
088    
089    
090      /**
091       * Generate the set of index keys for an entry.
092       *
093       * @param entry The entry.
094       * @param keys The set into which the generated keys will be inserted.
095       */
096      public void indexEntry(Entry entry,  Set<byte[]> keys)
097      {
098        List<Attribute> attrList =
099             entry.getAttribute(attributeType);
100        if (attrList != null)
101        {
102          if (!attrList.isEmpty())
103          {
104            keys.add(AttributeIndex.presenceKey.getData());
105          }
106        }
107      }
108    
109    
110    
111      /**
112       * Generate the set of index keys to be added and the set of index keys
113       * to be deleted for an entry that has been replaced.
114       *
115       * @param oldEntry The original entry contents.
116       * @param newEntry The new entry contents.
117       * @param modifiedKeys The map into which the modified keys will be inserted.
118       */
119      public void replaceEntry(Entry oldEntry, Entry newEntry,
120                               Map<byte[], Boolean> modifiedKeys)
121      {
122        List<Attribute> newAttributes = newEntry.getAttribute(attributeType, true);
123        List<Attribute> oldAttributes = oldEntry.getAttribute(attributeType, true);
124        if(oldAttributes == null)
125        {
126          if(newAttributes != null)
127          {
128            modifiedKeys.put(AttributeIndex.presenceKey.getData(), true);
129          }
130        }
131        else
132        {
133          if(newAttributes == null)
134          {
135            modifiedKeys.put(AttributeIndex.presenceKey.getData(), false);
136          }
137        }
138      }
139    
140    
141    
142      /**
143       * Generate the set of index keys to be added and the set of index keys
144       * to be deleted for an entry that was modified.
145       *
146       * @param oldEntry The original entry contents.
147       * @param newEntry The new entry contents.
148       * @param mods The set of modifications that were applied to the entry.
149       * @param modifiedKeys The map into which the modified keys will be inserted.
150       */
151      public void modifyEntry(Entry oldEntry, Entry newEntry,
152                              List<Modification> mods,
153                              Map<byte[], Boolean> modifiedKeys)
154      {
155        List<Attribute> newAttributes = newEntry.getAttribute(attributeType, true);
156        List<Attribute> oldAttributes = oldEntry.getAttribute(attributeType, true);
157        if(oldAttributes == null)
158        {
159          if(newAttributes != null)
160          {
161            modifiedKeys.put(AttributeIndex.presenceKey.getData(), true);
162          }
163        }
164        else
165        {
166          if(newAttributes == null)
167          {
168            modifiedKeys.put(AttributeIndex.presenceKey.getData(), false);
169          }
170        }
171      }
172    }