mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
07.58.2014 3ab9614db1a1a1a30c271424c4189999bf71b87a
opendj3-server-dev/src/server/org/opends/server/backends/jeb/ApproximateIndexer.java
@@ -26,42 +26,27 @@
 */
package org.opends.server.backends.jeb;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.ldap.DecodeException;
import org.opends.server.api.ApproximateMatchingRule;
import org.opends.server.api.ExtensibleIndexer;
import org.opends.server.types.*;
import org.opends.server.types.AttributeType;
import org.opends.server.types.AttributeValue;
/**
 * An implementation of an Indexer for attribute approximate matching.
 */
public class ApproximateIndexer extends Indexer
public class ApproximateIndexer extends ExtensibleIndexer
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
  /**
   * The comparator for index keys generated by this class.
   */
  private static final Comparator<byte[]> comparator =
       new AttributeIndex.KeyComparator();
  /**
   * The attribute type approximate matching rule.
   */
  private ApproximateMatchingRule approximateRule;
    /**
   * The attribute type for which this instance will
   * generate index keys.
   */
  private AttributeType attributeType;
  /**
   * Create a new attribute approximate indexer for the given index
   * configuration.
@@ -70,121 +55,31 @@
   */
  public ApproximateIndexer(AttributeType attributeType)
  {
    this.attributeType = attributeType;
    this.approximateRule = attributeType.getApproximateMatchingRule();
  }
  /**
   * Get a string representation of this object.  The returned value is
   * used to name an index created using this object.
   * @return A string representation of this object.
   */
  /** {@inheritDoc} */
  @Override
  public String toString()
  public String getIndexID()
  {
    return attributeType.getNameOrOID() + ".approximate";
    // TODO Auto-generated method stub
    throw new RuntimeException();
  }
  /**
   * Get the comparator that must be used to compare index keys
   * generated by this class.
   *
   * @return A byte array comparator.
   */
  /** {@inheritDoc} */
  @Override
  public Comparator<byte[]> getComparator()
  public String getExtensibleIndexID()
  {
    return comparator;
    return "approximate";
  }
  /**
   * Generate the set of index keys for an entry.
   *
   * @param entry The entry.
   * @param keys The set into which the generated keys will be inserted.
   */
  /** {@inheritDoc} */
  @Override
  public void indexEntry(Entry entry, Set<byte[]> keys)
  {
    List<Attribute> attrList =
         entry.getAttribute(attributeType);
    if (attrList != null)
    {
      indexAttribute(attrList, keys);
    }
  }
  /**
   * Generate the set of index keys to be added and the set of index keys
   * to be deleted for an entry that has been replaced.
   *
   * @param oldEntry The original entry contents.
   * @param newEntry The new entry contents.
   * @param modifiedKeys The map into which the modified keys will be inserted.
   */
  @Override
  public void replaceEntry(Entry oldEntry, Entry newEntry,
                           Map<byte[], Boolean> modifiedKeys)
  {
    List<Attribute> newAttributes = newEntry.getAttribute(attributeType, true);
    List<Attribute> oldAttributes = oldEntry.getAttribute(attributeType, true);
    indexAttribute(oldAttributes, modifiedKeys, false);
    indexAttribute(newAttributes, modifiedKeys, true);
  }
  /**
   * Generate the set of index keys to be added and the set of index keys
   * to be deleted for an entry that was modified.
   *
   * @param oldEntry The original entry contents.
   * @param newEntry The new entry contents.
   * @param mods The set of modifications that were applied to the entry.
   * @param modifiedKeys The map into which the modified keys will be inserted.
   */
  @Override
  public void modifyEntry(Entry oldEntry, Entry newEntry,
                          List<Modification> mods,
                          Map<byte[], Boolean> modifiedKeys)
  {
    List<Attribute> newAttributes = newEntry.getAttribute(attributeType, true);
    List<Attribute> oldAttributes = oldEntry.getAttribute(attributeType, true);
    indexAttribute(oldAttributes, modifiedKeys, false);
    indexAttribute(newAttributes, modifiedKeys, true);
  }
  /**
   * Generate the set of index keys for an attribute.
   * @param attrList The attribute to be indexed.
   * @param keys The set into which the keys will be inserted.
   */
  private void indexAttribute(List<Attribute> attrList,
                              Set<byte[]> keys)
  {
    if (attrList == null) return;
    for (Attribute attr : attrList)
    {
      if (!attr.isVirtual())
      {
        for (AttributeValue value : attr)
        {
          getKeys(value, keys);
        }
      }
    }
  }
  private void getKeys(AttributeValue value, Set<byte[]> keys)
  public void getKeys(AttributeValue value, Set<byte[]> keys)
  {
    try
    {
      byte[] keyBytes =
           approximateRule.normalizeAttributeValue(value.getValue()).toByteArray();
      keys.add(keyBytes);
      keys.add(approximateRule.normalizeAttributeValue(value.getValue()).toByteArray());
    }
    catch (DecodeException e)
    {
@@ -192,37 +87,4 @@
    }
  }
  /**
   * Generate the set of index keys for an attribute.
   * @param attrList The attribute to be indexed.
   * @param modifiedKeys The map into which the modified
   * keys will be inserted.
   * @param insert <code>true</code> if generated keys should
   * be inserted or <code>false</code> otherwise.
   */
  private void indexAttribute(List<Attribute> attrList,
                              Map<byte[], Boolean> modifiedKeys,
                              Boolean insert)
  {
    if (attrList == null) return;
    for (Attribute attr : attrList)
    {
      if (!attr.isVirtual())
      {
        for (AttributeValue value : attr)
        {
          getKeys(value, modifiedKeys, insert);
        }
      }
    }
  }
  private void getKeys(AttributeValue value, Map<byte[], Boolean> modifiedKeys,
      Boolean insert)
  {
    Set<byte[]> keys = new HashSet<byte[]>();
    getKeys(value, keys);
    ExtensibleIndexer.computeModifiedKeys(modifiedKeys, insert, keys);
  }
}