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

Jean-Noel Rouvignac
19.50.2015 836daf9ab712183520854b08b4dc90279490b27e
Code simplifications with indexes
3 files modified
103 ■■■■■ changed files
opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/AttributeIndex.java 87 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java 11 ●●●● patch | view | raw | blame | history
opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/VerifyJob.java 5 ●●●● patch | view | raw | blame | history
opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/AttributeIndex.java
@@ -108,9 +108,7 @@
    }
  }
  /**
   * This class implements an attribute indexer for matching rules in a Backend.
   */
  /** This class implements an attribute indexer for matching rules in a Backend. */
  static final class MatchingRuleIndex extends DefaultIndex
  {
    private final AttributeType attributeType;
@@ -127,31 +125,24 @@
    void indexEntry(Entry entry, Set<ByteString> keys)
    {
      List<Attribute> attributes = entry.getAttribute(attributeType, true);
      if (attributes != null)
      {
        indexAttribute(attributes, keys);
      }
    Set<ByteString> indexEntry(Entry entry)
    {
      final Set<ByteString> keys = new HashSet<>();
      indexEntry(entry, keys);
      return keys;
    }
    private void modifyEntry(Entry oldEntry, Entry newEntry, Map<ByteString, Boolean> modifiedKeys)
    {
      List<Attribute> oldAttributes = oldEntry.getAttribute(attributeType, true);
      if (oldAttributes != null)
      {
        final Set<ByteString> keys = new HashSet<ByteString>();
        indexAttribute(oldAttributes, keys);
        for (ByteString key : keys)
      for (ByteString key : indexEntry(oldEntry))
        {
          modifiedKeys.put(key, false);
        }
      }
      List<Attribute> newAttributes = newEntry.getAttribute(attributeType, true);
      if (newAttributes != null)
      {
        final Set<ByteString> keys = new HashSet<ByteString>();
        indexAttribute(newAttributes, keys);
        for (ByteString key : keys)
      for (ByteString key : indexEntry(newEntry))
        {
          final Boolean needsAdding = modifiedKeys.get(key);
          if (needsAdding == null)
@@ -166,10 +157,13 @@
          }
        }
      }
    }
    private void indexAttribute(List<Attribute> attributes, Set<ByteString> keys)
    {
      if (attributes == null)
      {
        return;
      }
      for (Attribute attr : attributes)
      {
        if (!attr.isVirtual())
@@ -202,9 +196,7 @@
  /** The key bytes used for the presence index as a {@link ByteString}. */
  static final ByteString PRESENCE_KEY = ByteString.valueOf("+");
  /**
   * A special indexer for generating presence indexes.
   */
  /** A special indexer for generating presence indexes. */
  private static final Indexer PRESENCE_INDEXER = new Indexer()
  {
    @Override
@@ -253,10 +245,9 @@
    final int indexEntryLimit = config.getIndexEntryLimit();
    final IndexingOptions indexingOptions = new IndexingOptionsImpl(config.getSubstringLength());
    final Map<String, MatchingRuleIndex> indexes = new HashMap<>();
    Collection<Indexer> indexers = new ArrayList<>();
    for(IndexType indexType : config.getIndexType()) {
      Collection<? extends Indexer> indexers;
      MatchingRule rule;
      switch (indexType)
      {
      case PRESENCE:
@@ -267,31 +258,26 @@
            getExtensibleIndexers(config.getAttribute(), config.getIndexExtensibleMatchingRule(), indexingOptions);
        break;
      case APPROXIMATE:
        indexers =
            throwIfNoMatchingRule(attributeType, indexType, attributeType.getApproximateMatchingRule()).createIndexers(
                indexingOptions);
        rule = throwIfNoMatchingRule(attributeType, indexType, attributeType.getApproximateMatchingRule());
        indexers.addAll(rule.createIndexers(indexingOptions));
        break;
      case EQUALITY:
        indexers =
            throwIfNoMatchingRule(attributeType, indexType, attributeType.getEqualityMatchingRule()).createIndexers(
                indexingOptions);
        rule = throwIfNoMatchingRule(attributeType, indexType, attributeType.getEqualityMatchingRule());
        indexers.addAll(rule.createIndexers(indexingOptions));
        break;
      case ORDERING:
        indexers =
            throwIfNoMatchingRule(attributeType, indexType, attributeType.getOrderingMatchingRule()).createIndexers(
                indexingOptions);
        rule = throwIfNoMatchingRule(attributeType, indexType, attributeType.getOrderingMatchingRule());
        indexers.addAll(rule.createIndexers(indexingOptions));
        break;
      case SUBSTRING:
        indexers =
            throwIfNoMatchingRule(attributeType, indexType, attributeType.getSubstringMatchingRule()).createIndexers(
                indexingOptions);
        rule = throwIfNoMatchingRule(attributeType, indexType, attributeType.getSubstringMatchingRule());
        indexers.addAll(rule.createIndexers(indexingOptions));
        break;
      default:
       throw new ConfigException(ERR_CONFIG_INDEX_TYPE_NEEDS_MATCHING_RULE.get(attributeType, indexType.toString()));
      }
      buildIndexesForIndexers(entryContainer, attributeType, state, indexes, indexEntryLimit, indexers);
    }
    return indexes;
    return buildIndexesForIndexers(entryContainer, attributeType, state, indexEntryLimit, indexers);
  }
  private static MatchingRule throwIfNoMatchingRule(AttributeType attributeType, IndexType type, MatchingRule rule)
@@ -304,9 +290,10 @@
    return rule;
  }
  private static void buildIndexesForIndexers(EntryContainer entryContainer, AttributeType attributeType, State state,
      Map<String, MatchingRuleIndex> indexes, int indexEntryLimit, Collection<? extends Indexer> indexers)
  private static Map<String, MatchingRuleIndex> buildIndexesForIndexers(EntryContainer entryContainer,
      AttributeType attributeType, State state, int indexEntryLimit, Collection<? extends Indexer> indexers)
  {
    final Map<String, MatchingRuleIndex> indexes = new HashMap<>();
    for (Indexer indexer : indexers)
    {
      final String indexID = indexer.getIndexID();
@@ -315,6 +302,7 @@
        indexes.put(indexID, new MatchingRuleIndex(entryContainer, attributeType, state, indexer, indexEntryLimit));
      }
    }
    return indexes;
  }
  private static Collection<Indexer> getExtensibleIndexers(AttributeType attributeType, Set<String> extensibleRules,
@@ -431,7 +419,7 @@
  {
    for (MatchingRuleIndex index : indexIdToIndexes.values())
    {
      for (ByteString key : indexEntry(index, entry))
      for (ByteString key : index.indexEntry(entry))
      {
        buffer.put(index, key, entryID);
      }
@@ -451,23 +439,15 @@
  {
    for (MatchingRuleIndex index : indexIdToIndexes.values())
    {
      for (ByteString key : indexEntry(index, entry))
      for (ByteString key : index.indexEntry(entry))
      {
        buffer.remove(index, key, entryID);
      }
    }
  }
  private Set<ByteString> indexEntry(MatchingRuleIndex index, Entry entry)
  {
    final Set<ByteString> keys = new HashSet<>();
    index.indexEntry(entry, keys);
    return keys;
  }
  /**
   * Update the index to reflect a sequence of modifications in a Modify
   * operation.
   * Update the index to reflect a sequence of modifications in a Modify operation.
   *
   * @param buffer The index buffer used to buffer up the index changes.
   * @param entryID The ID of the entry that was modified.
@@ -480,7 +460,7 @@
  {
    for (MatchingRuleIndex index : indexIdToIndexes.values())
    {
      TreeMap<ByteString, Boolean> modifiedKeys = new TreeMap<ByteString, Boolean>();
      TreeMap<ByteString, Boolean> modifiedKeys = new TreeMap<>();
      index.modifyEntry(oldEntry, newEntry, modifiedKeys);
      for (Map.Entry<ByteString, Boolean> modifiedKey : modifiedKeys.entrySet())
      {
@@ -785,7 +765,6 @@
      {
        updateIndex(updatedIndex, newConfiguration.getIndexEntryLimit(), ccr);
      }
    }
    catch (Exception e)
    {
opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java
@@ -56,7 +56,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
@@ -1407,7 +1406,6 @@
  {
    private final Storage storage;
    private final Map<IndexKey, IndexOutputBuffer> indexBufferMap = new HashMap<>();
    private final Set<ByteString> keySet = new HashSet<>();
    private final IndexKey dnIndexKey = new IndexKey(DN_TYPE, DN2ID_INDEX_NAME, 1);
    public ImportTask(final Storage storage)
@@ -1550,10 +1548,7 @@
    void processAttribute0(MatchingRuleIndex index, Entry entry, EntryID entryID, IndexKey indexKey, boolean insert)
        throws InterruptedException
    {
      keySet.clear();
      index.indexEntry(entry, keySet);
      for (ByteString key : keySet)
      for (ByteString key : index.indexEntry(entry))
      {
        processKey(index, key, entryID, indexKey, insert);
      }
@@ -3306,8 +3301,8 @@
      if (obj instanceof IndexKey)
      {
        IndexKey oKey = (IndexKey) obj;
        if (attributeType.equals(oKey.getAttributeType())
            && indexID.equals(oKey.getIndexID()))
        if (attributeType.equals(oKey.attributeType)
            && indexID.equals(oKey.indexID))
        {
          return true;
        }
opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/VerifyJob.java
@@ -35,7 +35,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedList;
@@ -957,9 +956,7 @@
  {
    for (MatchingRuleIndex index : attrIndex.getNameToIndexes().values())
    {
      Set<ByteString> keys = new HashSet<ByteString>();
      index.indexEntry(entry, keys);
      for (ByteString key : keys)
      for (ByteString key : index.indexEntry(entry))
      {
        verifyAttributeInIndex(index, txn, key, entryID);
      }