From 543652e3cf5db56b735e99dfd3f26d0a88642089 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Wed, 13 May 2015 16:05:43 +0000
Subject: [PATCH] Code cleanup

---
 opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java         |  134 +++++++-------------------------
 opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/ImportLDIFReader.java |   65 +++++++++++-----
 2 files changed, 75 insertions(+), 124 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/ImportLDIFReader.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/ImportLDIFReader.java
index cdf868f..dd98d28 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/ImportLDIFReader.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/ImportLDIFReader.java
@@ -47,11 +47,42 @@
 import org.opends.server.util.LDIFException;
 import org.opends.server.util.LDIFReader;
 
-/**
- * This class specializes the LDIFReader for imports.
- */
+/** This class specializes the LDIFReader for imports. */
 final class ImportLDIFReader extends LDIFReader
 {
+  /**
+   * A class holding the entry, its entryID as assigned by the LDIF reader and its suffix as
+   * determined by the LDIF reader.
+   */
+  static final class EntryInformation
+  {
+    private final Entry entry;
+    private final EntryID entryID;
+    private final Suffix suffix;
+
+    private EntryInformation(Entry entry, EntryID entryID, Suffix suffix)
+    {
+      this.entry = entry;
+      this.entryID = entryID;
+      this.suffix = suffix;
+    }
+
+    Entry getEntry()
+    {
+      return entry;
+    }
+
+    EntryID getEntryID()
+    {
+      return entryID;
+    }
+
+    Suffix getSuffix()
+    {
+      return suffix;
+    }
+  }
+
   private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
   private final RootContainer rootContainer;
@@ -76,19 +107,16 @@
   /**
    * Reads the next entry from the LDIF source.
    *
-   * @return The next entry read from the LDIF source, or <CODE>null</CODE> if the end of the LDIF
+   * @return The next entry information read from the LDIF source, or <CODE>null</CODE> if the end of the LDIF
    *         data is reached.
    * @param suffixesMap
    *          A map of suffixes instances.
-   * @param entryInfo
-   *          A object to hold information about the entry ID and what suffix was selected.
    * @throws IOException
    *           If an I/O problem occurs while reading from the file.
    * @throws LDIFException
    *           If the information read cannot be parsed as an LDIF entry.
    */
-  public final Entry readEntry(Map<DN, Suffix> suffixesMap, Importer.EntryInformation entryInfo) throws IOException,
-      LDIFException
+  public final EntryInformation readEntry(Map<DN, Suffix> suffixesMap) throws IOException, LDIFException
   {
     final boolean checkSchema = importConfig.validateSchema();
     while (true)
@@ -106,7 +134,7 @@
           return null;
         }
         lastEntryBodyLines = lines;
-        lastEntryHeaderLines = new LinkedList<StringBuilder>();
+        lastEntryHeaderLines = new LinkedList<>();
 
         // Read the DN of the entry and see if it is one that should be included
         // in the import.
@@ -158,21 +186,16 @@
       {
         continue;
       }
-      entryInfo.setEntryID(entryID);
-      entryInfo.setSuffix(suffix);
-      // The entry should be included in the import, so return it.
-      return entry;
+      return new EntryInformation(entry, entryID, suffix);
     }
   }
 
   private Entry createEntry(List<StringBuilder> lines, DN entryDN, boolean checkSchema, Suffix suffix)
   {
     // Read the set of attributes from the entry.
-    Map<ObjectClass, String> objectClasses = new HashMap<ObjectClass, String>();
-    Map<AttributeType, List<AttributeBuilder>> userAttrBuilders =
-        new HashMap<AttributeType, List<AttributeBuilder>>();
-    Map<AttributeType, List<AttributeBuilder>> operationalAttrBuilders =
-        new HashMap<AttributeType, List<AttributeBuilder>>();
+    Map<ObjectClass, String> objectClasses = new HashMap<>();
+    Map<AttributeType, List<AttributeBuilder>> userAttrBuilders = new HashMap<>();
+    Map<AttributeType, List<AttributeBuilder>> operationalAttrBuilders = new HashMap<>();
     try
     {
       for (StringBuilder line : lines)
@@ -236,13 +259,13 @@
         final DN entryDN = entry.getName();
         LocalizableMessage m;
         LocalizableMessage rejectMessage = pluginResult.getErrorMessage();
-        if (rejectMessage == null)
+        if (rejectMessage != null)
         {
-          m = ERR_LDIF_REJECTED_BY_PLUGIN_NOMESSAGE.get(entryDN);
+          m = ERR_LDIF_REJECTED_BY_PLUGIN.get(entryDN, rejectMessage);
         }
         else
         {
-          m = ERR_LDIF_REJECTED_BY_PLUGIN.get(entryDN, rejectMessage);
+          m = ERR_LDIF_REJECTED_BY_PLUGIN_NOMESSAGE.get(entryDN);
         }
 
         logToRejectWriter(lines, m);
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java
index b4e3ad9..0e4f9ef 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java
@@ -103,6 +103,7 @@
 import org.opends.server.backends.RebuildConfig.RebuildMode;
 import org.opends.server.backends.persistit.PersistItStorage;
 import org.opends.server.backends.pluggable.AttributeIndex.MatchingRuleIndex;
+import org.opends.server.backends.pluggable.ImportLDIFReader.EntryInformation;
 import org.opends.server.backends.pluggable.spi.Cursor;
 import org.opends.server.backends.pluggable.spi.ReadOperation;
 import org.opends.server.backends.pluggable.spi.ReadableTransaction;
@@ -198,6 +199,15 @@
 
   /** The DN attribute type. */
   private static final AttributeType DN_TYPE;
+  static
+  {
+    AttributeType attrType = DirectoryServer.getAttributeType("dn");
+    if (attrType == null)
+    {
+      attrType = DirectoryServer.getDefaultAttributeType("dn");
+    }
+    DN_TYPE = attrType;
+  }
 
   /** Root container. */
   private final RootContainer rootContainer;
@@ -267,7 +277,12 @@
 
   /** Map of DNs to Suffix objects. */
   private final Map<DN, Suffix> dnSuffixMap = new LinkedHashMap<>();
-  /** Map of indexIDs to indexes. */
+  /**
+   * Map of indexIDs to indexes.
+   * <p>
+   * Mainly used to support multiple suffixes. Each index in each suffix gets a unique ID to
+   * identify which tree it needs to go to in phase two processing.
+   */
   private final ConcurrentHashMap<Integer, Index> indexIDToIndexMap = new ConcurrentHashMap<>();
   /** Map of indexIDs to entry containers. */
   private final ConcurrentHashMap<Integer, EntryContainer> indexIDToECMap = new ConcurrentHashMap<>();
@@ -287,16 +302,6 @@
   /** Number of phase one buffers. */
   private int phaseOneBufferCount;
 
-  static
-  {
-    AttributeType attrType = DirectoryServer.getAttributeType("dn");
-    if (attrType == null)
-    {
-      attrType = DirectoryServer.getDefaultAttributeType("dn");
-    }
-    DN_TYPE = attrType;
-  }
-
   /**
    * Create a new import job with the specified rebuild index config.
    *
@@ -403,7 +408,7 @@
    * @param backendCfg
    *          the backend configuration object
    * @return true if the backend must be cleared, false otherwise
-   * @see Importer#getSuffix(WriteableTransaction, EntryContainer) for per-suffix cleanups.
+   * @see Importer#prepareSuffix(WriteableTransaction, EntryContainer) for per-suffix cleanups.
    */
   static boolean mustClearBackend(LDIFImportConfig importCfg, PluggableBackendCfg backendCfg)
   {
@@ -648,16 +653,12 @@
       if (suffix != null)
       {
         dnSuffixMap.put(ec.getBaseDN(), suffix);
-        generateIndexID(suffix);
+        generateIndexIDs(suffix);
       }
     }
   }
 
-  /**
-   * Mainly used to support multiple suffixes. Each index in each suffix gets an
-   * unique ID to identify which DB it needs to go to in phase two processing.
-   */
-  private void generateIndexID(Suffix suffix)
+  private void generateIndexIDs(Suffix suffix)
   {
     for (AttributeIndex attributeIndex : suffix.getAttrIndexMap().values())
     {
@@ -1359,31 +1360,22 @@
 
     private final Set<ByteString> insertKeySet = new HashSet<>();
     private final Set<ByteString> deleteKeySet = new HashSet<>();
-    private final EntryInformation entryInfo = new EntryInformation();
     private Entry oldEntry;
-    private EntryID entryID;
 
     @Override
     void call0(WriteableTransaction txn) throws Exception
     {
       try
       {
-        while (true)
+        EntryInformation entryInfo;
+        while ((entryInfo = reader.readEntry(dnSuffixMap)) != null)
         {
           if (importCfg.isCancelled() || isCanceled)
           {
             freeBufferQueue.add(IndexOutputBuffer.poison());
             return;
           }
-          oldEntry = null;
-          Entry entry = reader.readEntry(dnSuffixMap, entryInfo);
-          if (entry == null)
-          {
-            break;
-          }
-          entryID = entryInfo.getEntryID();
-          Suffix suffix = entryInfo.getSuffix();
-          processEntry(txn, entry, suffix);
+          processEntry(txn, entryInfo.getEntry(), entryInfo.getEntryID(), entryInfo.getSuffix());
         }
         flushIndexBuffers();
       }
@@ -1395,17 +1387,14 @@
       }
     }
 
-    void processEntry(WriteableTransaction txn, Entry entry, Suffix suffix)
+    @Override
+    void processEntry(WriteableTransaction txn, Entry entry, EntryID entryID, Suffix suffix)
         throws DirectoryException, StorageRuntimeException, InterruptedException
     {
       DN entryDN = entry.getName();
 
       EntryID oldID = suffix.getDN2ID().get(txn, entryDN);
-      if (oldID != null)
-      {
-        oldEntry = suffix.getID2Entry().get(txn, oldID);
-      }
-
+      oldEntry = oldID != null ? suffix.getID2Entry().get(txn, oldID) : null;
       if (oldEntry == null)
       {
         if (validateDNs && !dnSanityCheck(txn, entry, suffix))
@@ -1477,7 +1466,6 @@
     private final Storage storage;
     private final Map<IndexKey, IndexOutputBuffer> indexBufferMap = new HashMap<>();
     private final Set<ByteString> insertKeySet = new HashSet<>();
-    private final EntryInformation entryInfo = new EntryInformation();
     private final IndexKey dnIndexKey = new IndexKey(DN_TYPE, DN2ID_INDEX_NAME, 1);
 
     public ImportTask(final Storage storage)
@@ -1504,21 +1492,15 @@
     {
       try
       {
-        while (true)
+        EntryInformation entryInfo;
+        while ((entryInfo = reader.readEntry(dnSuffixMap)) != null)
         {
           if (importCfg.isCancelled() || isCanceled)
           {
             freeBufferQueue.add(IndexOutputBuffer.poison());
             return;
           }
-          Entry entry = reader.readEntry(dnSuffixMap, entryInfo);
-          if (entry == null)
-          {
-            break;
-          }
-          EntryID entryID = entryInfo.getEntryID();
-          Suffix suffix = entryInfo.getSuffix();
-          processEntry(txn, entry, entryID, suffix);
+          processEntry(txn, entryInfo.getEntry(), entryInfo.getEntryID(), entryInfo.getSuffix());
         }
         flushIndexBuffers();
       }
@@ -2829,21 +2811,20 @@
 
     private void rebuildIndexMap(WriteableTransaction txn, boolean onlyDegraded)
     {
-      final RebuildMode rebuildMode = rebuildConfig.getRebuildMode();
       for (final Map.Entry<AttributeType, AttributeIndex> mapEntry : suffix.getAttrIndexMap().entrySet())
       {
         final AttributeType attributeType = mapEntry.getKey();
         final AttributeIndex attributeIndex = mapEntry.getValue();
-        if (mustRebuild(attributeType, rebuildMode))
+        if (mustRebuild(attributeType))
         {
           rebuildAttributeIndexes(txn, attributeIndex, attributeType, onlyDegraded);
         }
       }
     }
 
-    private boolean mustRebuild(final AttributeType attrType, RebuildMode rebuildMode)
+    private boolean mustRebuild(final AttributeType attrType)
     {
-      switch (rebuildMode)
+      switch (rebuildConfig.getRebuildMode())
       {
       case ALL:
       case DEGRADED:
@@ -3352,59 +3333,6 @@
   }
 
   /**
-   * A class to hold information about the entry determined by the LDIF reader.
-   * Mainly the suffix the entry belongs under and the ID assigned to it by the
-   * reader.
-   */
-  public class EntryInformation
-  {
-    private EntryID entryID;
-    private Suffix suffix;
-
-    /**
-     * Return the suffix associated with the entry.
-     *
-     * @return Entry's suffix instance;
-     */
-    private Suffix getSuffix()
-    {
-      return suffix;
-    }
-
-    /**
-     * Set the suffix instance associated with the entry.
-     *
-     * @param suffix
-     *          The suffix associated with the entry.
-     */
-    public void setSuffix(Suffix suffix)
-    {
-      this.suffix = suffix;
-    }
-
-    /**
-     * Set the entry's ID.
-     *
-     * @param entryID
-     *          The entry ID to set the entry ID to.
-     */
-    public void setEntryID(EntryID entryID)
-    {
-      this.entryID = entryID;
-    }
-
-    /**
-     * Return the entry ID associated with the entry.
-     *
-     * @return The entry ID associated with the entry.
-     */
-    private EntryID getEntryID()
-    {
-      return entryID;
-    }
-  }
-
-  /**
    * This class is used as an index key for hash maps that need to process multiple suffix index
    * elements into a single queue and/or maps based on both attribute type and index ID (ie.,
    * cn.equality, sn.equality,...).

--
Gitblit v1.10.0