From 34ffc6b8a4a78ff74f12ebde68a8e87115cbcfd7 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 16 Apr 2015 13:56:01 +0000
Subject: [PATCH] Code cleanup

---
 opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java         |   59 +++++++++++------------------
 opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/IndexInputBuffer.java |   58 ++++------------------------
 2 files changed, 32 insertions(+), 85 deletions(-)

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 91c22e7..fc15ec3 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
@@ -30,6 +30,7 @@
 import static org.opends.server.admin.std.meta.BackendIndexCfgDefn.IndexType.*;
 import static org.opends.server.backends.pluggable.EntryIDSet.*;
 import static org.opends.server.backends.pluggable.IndexOutputBuffer.*;
+import static org.opends.server.backends.pluggable.SuffixContainer.*;
 import static org.opends.server.util.DynamicConstants.*;
 import static org.opends.server.util.ServerConstants.*;
 import static org.opends.server.util.StaticUtils.*;
@@ -147,7 +148,6 @@
   private static final int READER_WRITER_BUFFER_SIZE = 8 * KB;
   private static final int MIN_DB_CACHE_MEMORY = MAX_DB_CACHE_SIZE
       + MAX_DB_LOG_SIZE;
-  private static final int BYTE_BUFFER_CAPACITY = 128;
 
   /** Max size of phase one buffer. */
   private static final int MAX_BUFFER_SIZE = 2 * MB;
@@ -1256,9 +1256,8 @@
       super(storage);
     }
 
-    /** {@inheritDoc} */
     @Override
-    Void call0(WriteableTransaction txn) throws Exception
+    void call0(WriteableTransaction txn) throws Exception
     {
       for (Suffix suffix : dnSuffixMap.values())
       {
@@ -1308,7 +1307,6 @@
           }
         }
       }
-      return null;
     }
   }
 
@@ -1320,9 +1318,8 @@
       super(storage);
     }
 
-    /** {@inheritDoc} */
     @Override
-    Void call0(WriteableTransaction txn) throws Exception
+    void call0(WriteableTransaction txn) throws Exception
     {
       for (Suffix suffix : dnSuffixMap.values())
       {
@@ -1382,7 +1379,6 @@
           }
         }
       }
-      return null;
     }
 
     private List<ByteString> includeBranchesAsBytes(Suffix suffix)
@@ -1415,9 +1411,8 @@
     private Entry oldEntry;
     private EntryID entryID;
 
-    /** {@inheritDoc} */
     @Override
-    Void call0(WriteableTransaction txn) throws Exception
+    void call0(WriteableTransaction txn) throws Exception
     {
       try
       {
@@ -1426,7 +1421,7 @@
           if (importConfiguration.isCancelled() || isCanceled)
           {
             freeBufferQueue.add(IndexOutputBuffer.poison());
-            return null;
+            return;
           }
           oldEntry = null;
           Entry entry = reader.readEntry(dnSuffixMap, entryInfo);
@@ -1439,7 +1434,6 @@
           processEntry(txn, entry, suffix);
         }
         flushIndexBuffers();
-        return null;
       }
       catch (Exception e)
       {
@@ -1555,7 +1549,8 @@
       return null;
     }
 
-    Void call0(WriteableTransaction txn) throws Exception {
+    void call0(WriteableTransaction txn) throws Exception
+    {
       try
       {
         while (true)
@@ -1563,7 +1558,7 @@
           if (importConfiguration.isCancelled() || isCanceled)
           {
             freeBufferQueue.add(IndexOutputBuffer.poison());
-            return null;
+            return;
           }
           Entry entry = reader.readEntry(dnSuffixMap, entryInfo);
           if (entry == null)
@@ -1575,7 +1570,6 @@
           processEntry(txn, entry, entryID, suffix);
         }
         flushIndexBuffers();
-        return null;
       }
       catch (Exception e)
       {
@@ -1983,7 +1977,6 @@
         return null;
       }
 
-      final ByteStringBuilder key = new ByteStringBuilder(BYTE_BUFFER_CAPACITY);
       ImportIDSet insertIDSet = null;
       ImportIDSet deleteIDSet = null;
       ImportRecord previousRecord = null;
@@ -2009,13 +2002,12 @@
                 addToDB(previousRecord.getIndexID(), insertIDSet, deleteIDSet);
               }
 
-              // this is a new record, reinitialize all
-              int indexID = b.getIndexID();
-              b.fetchKey(key);
-              previousRecord = ImportRecord.from(key, indexID);
+              // this is a new record
+              final ImportRecord newRecord = b.currentRecord();
+              insertIDSet = newImportIDSet(newRecord);
+              deleteIDSet = newImportIDSet(newRecord);
 
-              insertIDSet = newImportIDSet(key, indexID);
-              deleteIDSet = newImportIDSet(key, indexID);
+              previousRecord = newRecord;
             }
 
             // merge all entryIds into the idSets
@@ -2047,15 +2039,15 @@
       }
     }
 
-    private ImportIDSet newImportIDSet(ByteStringBuilder key, Integer indexID)
+    private ImportIDSet newImportIDSet(ImportRecord record)
     {
       if (indexMgr.isDN2ID())
       {
-        return new ImportIDSet(key, newDefinedSet(), 1, false);
+        return new ImportIDSet(record.getKey(), newDefinedSet(), 1, false);
       }
 
-      final Index index = indexIDToIndexMap.get(indexID);
-      return new ImportIDSet(key, newDefinedSet(), index.getIndexEntryLimit(), index.getMaintainCount());
+      final Index index = indexIDToIndexMap.get(record.getIndexID());
+      return new ImportIDSet(record.getKey(), newDefinedSet(), index.getIndexEntryLimit(), index.getMaintainCount());
     }
 
     private void addToDB(int indexID, ImportIDSet insertSet, ImportIDSet deleteSet) throws DirectoryException
@@ -2843,10 +2835,7 @@
       }
     }
 
-    /**
-     * Print start message.
-     */
-    void printStartMessage(WriteableTransaction txn) throws StorageRuntimeException
+    private void printStartMessage(WriteableTransaction txn) throws StorageRuntimeException
     {
       totalEntries = suffix.getID2Entry().getRecordCount(txn);
 
@@ -2891,9 +2880,8 @@
       }
     }
 
-    /** {@inheritDoc} */
     @Override
-    Void call0(WriteableTransaction txn) throws Exception
+    void call0(WriteableTransaction txn) throws Exception
     {
       ID2Entry id2entry = entryContainer.getID2Entry();
       Cursor<ByteString, ByteString> cursor = txn.openCursor(id2entry.getName());
@@ -2903,7 +2891,7 @@
         {
           if (isCanceled)
           {
-            return null;
+            return;
           }
           EntryID entryID = new EntryID(cursor.getKey());
           Entry entry =
@@ -2913,7 +2901,6 @@
           entriesProcessed.getAndIncrement();
         }
         flushIndexBuffers();
-        return null;
       }
       catch (Exception e)
       {
@@ -3193,7 +3180,7 @@
       for (String index : rebuildList)
       {
         final String lowerName = index.toLowerCase();
-        if ("dn2id".equals(lowerName))
+        if (DN2ID_INDEX_NAME.equals(lowerName))
         {
           indexCount += 3;
         }
@@ -3209,8 +3196,8 @@
           }
           indexCount++;
         }
-        else if ("id2subtree".equals(lowerName)
-            || "id2children".equals(lowerName))
+        else if (ID2SUBTREE_INDEX_NAME.equals(lowerName)
+            || ID2CHILDREN_INDEX_NAME.equals(lowerName))
         {
           throw attributeIndexNotConfigured(index);
         }
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/IndexInputBuffer.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/IndexInputBuffer.java
index d03d77c..6f4c564 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/IndexInputBuffer.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/IndexInputBuffer.java
@@ -85,8 +85,8 @@
    * @throws IOException
    *           If an IO error occurred when priming the cache.
    */
-  public IndexInputBuffer(IndexManager indexMgr, FileChannel channel,
-      long begin, long end, int bufferID, int cacheSize) throws IOException
+  IndexInputBuffer(IndexManager indexMgr, FileChannel channel, long begin, long end, int bufferID, int cacheSize)
+      throws IOException
   {
     this.indexMgr = indexMgr;
     this.channel = channel;
@@ -130,42 +130,17 @@
    * @throws IOException
    *           If an IO error occurred.
    */
-  public boolean hasMoreData() throws IOException
+  boolean hasMoreData() throws IOException
   {
     boolean hasMore = begin + offset < end;
     return cache.remaining() != 0 || hasMore;
   }
 
-  /**
-   * Returns the length of the next key.
-   *
-   * @return The length of the next key.
-   */
-  public int getKeyLen()
-  {
-    return keyBuffer.length();
-  }
-
-  /**
-   * Fetches the next key into the provided byte string builder.
-   *
-   * @param b
-   *          A builder where to fetch the key
-   */
-  public void fetchKey(ByteStringBuilder b)
-  {
-    b.clear().append(keyBuffer);
-  }
-
-  /**
-   * Returns the index ID of the next record.
-   *
-   * @return The index ID of the next record.
-   */
-  public Integer getIndexID()
+  ImportRecord currentRecord()
   {
     if (record == null)
     {
+      // ensure record fetched
       try
       {
         fetchNextRecord();
@@ -176,7 +151,7 @@
         throw new RuntimeException(ex);
       }
     }
-    return record != null ? record.getIndexID() : null;
+    return record;
   }
 
   /**
@@ -186,7 +161,7 @@
    * @throws IOException
    *           If an IO error occurred.
    */
-  public void fetchNextRecord() throws IOException
+  void fetchNextRecord() throws IOException
   {
     switch (recordState)
     {
@@ -233,15 +208,14 @@
   }
 
   /**
-   * Reads the next ID set from the record and merges it with the provided ID
-   * set.
+   * Reads the next ID set from the record and merges it with the provided ID set.
    *
    * @param idSet
    *          The ID set to be merged.
    * @throws IOException
    *           If an IO error occurred.
    */
-  public void mergeIDSet(ImportIDSet idSet) throws IOException
+  void mergeIDSet(ImportIDSet idSet) throws IOException
   {
     if (recordState == RecordState.START)
     {
@@ -310,18 +284,4 @@
     }
     return cmp;
   }
-
-  ImportRecord currentRecord()
-  {
-    ensureRecordFetched();
-    return record;
-  }
-
-  private void ensureRecordFetched()
-  {
-    if (keyBuffer.length() == 0)
-    {
-      getIndexID();
-    }
-  }
 }

--
Gitblit v1.10.0