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

Jean-Noel Rouvignac
13.33.2015 9dea9b99c7ee9acd02ad2e15dfa7a7dd8fc97c80
More code simplifications.

Importer.java:
In getSuffix(), used early exit + extracted method getDescendants().
Inlined processDN2URI() and simplified resulting code.

SUffix.java:
Added a constructor
AutoRefactor
2 files modified
184 ■■■■■ changed files
opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java 160 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Suffix.java 24 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java
@@ -681,92 +681,89 @@
  private Suffix getSuffix(WriteableTransaction txn, EntryContainer entryContainer)
      throws ConfigException, DirectoryException
  {
    DN baseDN = entryContainer.getBaseDN();
    EntryContainer sourceEntryContainer = null;
    List<DN> includeBranches = new ArrayList<>();
    List<DN> excludeBranches = new ArrayList<>();
    if (!importCfg.appendToExistingData()
        && !importCfg.clearBackend())
    if (importCfg.appendToExistingData() || importCfg.clearBackend())
    {
      for (DN dn : importCfg.getExcludeBranches())
      return new Suffix(entryContainer);
    }
    final DN baseDN = entryContainer.getBaseDN();
    if (importCfg.getExcludeBranches().contains(baseDN))
    {
      // This entire base DN was explicitly excluded. Skip.
      return null;
    }
    EntryContainer sourceEntryContainer = null;
    List<DN> excludeBranches = getDescendants(baseDN, importCfg.getExcludeBranches());
    List<DN> includeBranches = null;
    if (!importCfg.getIncludeBranches().isEmpty())
    {
      includeBranches = getDescendants(baseDN, importCfg.getIncludeBranches());
      if (includeBranches.isEmpty())
      {
        if (baseDN.equals(dn))
        // There are no branches in the explicitly defined include list under this base DN.
        // Skip this base DN altogether.
        return null;
      }
      // Remove any overlapping include branches.
      Iterator<DN> includeBranchIterator = includeBranches.iterator();
      while (includeBranchIterator.hasNext())
      {
        DN includeDN = includeBranchIterator.next();
        if (!isAnyNotEqualAndAncestorOf(includeBranches, includeDN))
        {
          // This entire base DN was explicitly excluded. Skip.
          return null;
        }
        if (baseDN.isAncestorOf(dn))
        {
          excludeBranches.add(dn);
          includeBranchIterator.remove();
        }
      }
      if (!importCfg.getIncludeBranches().isEmpty())
      // Remove any exclude branches that are not are not under a include branch
      // since they will be migrated as part of the existing entries
      // outside of the include branches anyways.
      Iterator<DN> excludeBranchIterator = excludeBranches.iterator();
      while (excludeBranchIterator.hasNext())
      {
        for (DN dn : importCfg.getIncludeBranches())
        DN excludeDN = excludeBranchIterator.next();
        if (!isAnyAncestorOf(includeBranches, excludeDN))
        {
          if (baseDN.isAncestorOf(dn))
          {
            includeBranches.add(dn);
          }
          excludeBranchIterator.remove();
        }
      }
        if (includeBranches.isEmpty())
        {
          /*
           * There are no branches in the explicitly defined include list under
           * this base DN. Skip this base DN all together.
           */
          return null;
        }
      if (excludeBranches.isEmpty()
          && includeBranches.size() == 1
          && includeBranches.get(0).equals(baseDN))
      {
        // This entire base DN is explicitly included in the import with
        // no exclude branches that we need to migrate.
        // Just clear the entry container.
        clearSuffix(entryContainer);
      }
      else
      {
        sourceEntryContainer = entryContainer;
        // Remove any overlapping include branches.
        Iterator<DN> includeBranchIterator = includeBranches.iterator();
        while (includeBranchIterator.hasNext())
        {
          DN includeDN = includeBranchIterator.next();
          if (!isAnyNotEqualAndAncestorOf(includeBranches, includeDN))
          {
            includeBranchIterator.remove();
          }
        }
        // Remove any exclude branches that are not are not under a include
        // branch since they will be migrated as part of the existing entries
        // outside of the include branches anyways.
        Iterator<DN> excludeBranchIterator = excludeBranches.iterator();
        while (excludeBranchIterator.hasNext())
        {
          DN excludeDN = excludeBranchIterator.next();
          if (!isAnyAncestorOf(includeBranches, excludeDN))
          {
            excludeBranchIterator.remove();
          }
        }
        if (excludeBranches.isEmpty()
            && includeBranches.size() == 1
            && includeBranches.get(0).equals(baseDN))
        {
          // This entire base DN is explicitly included in the import with
          // no exclude branches that we need to migrate.
          // Just clear the entry container.
          clearSuffix(entryContainer);
        }
        else
        {
          sourceEntryContainer = entryContainer;
          // Create a temp entry container
          DN tempDN = baseDN.child(DN.valueOf("dc=importTmp"));
          entryContainer = rootContainer.openEntryContainer(tempDN, txn);
        }
        // Create a temp entry container
        DN tempDN = baseDN.child(DN.valueOf("dc=importTmp"));
        entryContainer = rootContainer.openEntryContainer(tempDN, txn);
      }
    }
    return new Suffix(entryContainer, sourceEntryContainer, includeBranches, excludeBranches);
  }
  private List<DN> getDescendants(DN baseDN, Set<DN> dns)
  {
    final List<DN> results = new ArrayList<>();
    for (DN dn : dns)
    {
      if (baseDN.isAncestorOf(dn))
      {
        results.add(dn);
      }
    }
    return results;
  }
  private static void clearSuffix(EntryContainer entryContainer)
  {
    entryContainer.lock();
@@ -1418,14 +1415,15 @@
        }
        suffix.removePending(entryDN);
        processDN2ID(suffix, entryDN, entryID);
        suffix.getDN2URI().addEntry(txn, entry);
      }
      else
      {
        suffix.removePending(entryDN);
        entryID = oldID;
        suffix.getDN2URI().replaceEntry(txn, oldEntry, entry);
      }
      processDN2URI(txn, suffix, oldEntry, entry);
      suffix.getID2Entry().put(txn, entryID, entry);
      if (oldEntry != null)
      {
@@ -1543,7 +1541,7 @@
      }
      suffix.removePending(entryDN);
      processDN2ID(suffix, entryDN, entryID);
      processDN2URI(txn, suffix, null, entry);
      suffix.getDN2URI().addEntry(txn, entry);
      processIndexes(suffix, entry, entryID);
      processVLVIndexes(txn, suffix, entry, entryID);
      suffix.getID2Entry().put(txn, entryID, entry);
@@ -1709,20 +1707,6 @@
      int indexID = processKey(dn2id, dnBytes, entryID, dnIndexKey, true);
      indexIDToECMap.putIfAbsent(indexID, suffix.getEntryContainer());
    }
    void processDN2URI(WriteableTransaction txn, Suffix suffix, Entry oldEntry, Entry newEntry)
        throws StorageRuntimeException
    {
      DN2URI dn2uri = suffix.getDN2URI();
      if (oldEntry != null)
      {
        dn2uri.replaceEntry(txn, oldEntry, newEntry);
      }
      else
      {
        dn2uri.addEntry(txn, newEntry);
      }
    }
  }
  /**
@@ -2707,7 +2691,7 @@
    void initialize() throws ConfigException, InitializationException
    {
      entryContainer = rootContainer.getEntryContainer(rebuildConfig.getBaseDN());
      suffix = new Suffix(entryContainer, null, null, null);
      suffix = new Suffix(entryContainer);
    }
    private void printStartMessage(WriteableTransaction txn) throws StorageRuntimeException
@@ -3176,7 +3160,7 @@
      }
      if (rebuildDn2uri)
      {
        processDN2URI(txn, suffix, null, entry);
        suffix.getDN2URI().addEntry(txn, entry);
      }
      processIndexes(entry, entryID);
      processVLVIndexes(txn, entry, entryID);
opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Suffix.java
@@ -49,7 +49,6 @@
 */
class Suffix
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
  private final List<DN> includeBranches, excludeBranches;
@@ -58,9 +57,13 @@
  private final EntryContainer entryContainer;
  private final Object synchObject = new Object();
  private static final int PARENT_ID_SET_SIZE = 16 * 1024;
  private final ConcurrentHashMap<DN, CountDownLatch> pendingMap =
          new ConcurrentHashMap<DN, CountDownLatch>();
  private final Set<DN> parentSet = new HashSet<DN>(PARENT_ID_SET_SIZE);
  private final ConcurrentHashMap<DN, CountDownLatch> pendingMap = new ConcurrentHashMap<>();
  private final Set<DN> parentSet = new HashSet<>(PARENT_ID_SET_SIZE);
  Suffix(EntryContainer entryContainer)
  {
    this(entryContainer, null, null, null);
  }
  /**
   * Creates a suffix instance using the specified parameters.
@@ -82,7 +85,7 @@
    }
    else
    {
      this.includeBranches = new ArrayList<DN>(0);
      this.includeBranches = new ArrayList<>(0);
    }
    if (excludeBranches != null)
    {
@@ -90,7 +93,7 @@
    }
    else
    {
      this.excludeBranches = new ArrayList<DN>(0);
      this.excludeBranches = new ArrayList<>(0);
    }
  }
@@ -104,7 +107,6 @@
    return entryContainer.getDN2ID();
  }
  /**
   * Returns the ID2Entry instance pertaining to a suffix instance.
   *
@@ -115,7 +117,6 @@
    return entryContainer.getID2Entry();
  }
  /**
   * Returns the DN2URI instance pertaining to a suffix instance.
   *
@@ -126,7 +127,6 @@
    return entryContainer.getDN2URI();
  }
  /**
   * Returns the entry container pertaining to a suffix instance.
   *
@@ -137,7 +137,6 @@
    return entryContainer;
  }
  /**
   * Return the Attribute Type - Index map used to map an attribute type to an
   * index instance.
@@ -149,7 +148,6 @@
    return entryContainer.getAttributeIndexMap();
  }
  /**
   * Make sure the specified parent DN is not in the pending map.
   *
@@ -164,7 +162,6 @@
    }
  }
  /**
   * Add specified DN to the pending map.
   *
@@ -175,7 +172,6 @@
    pendingMap.putIfAbsent(dn, new CountDownLatch(1));
  }
  /**
   * Remove the specified DN from the pending map, it may not exist if the
   * entries are being migrated so just return.
@@ -191,7 +187,6 @@
    }
  }
  /**
   * Return {@code true} if the specified dn is contained in the parent set, or
   * in the specified DN cache. This would indicate that the parent has already
@@ -242,7 +237,6 @@
    return parentThere;
  }
  /**
   * Sets the trusted status of all of the indexes and vlvIndexes.
   *