From 8389bdd13eaa937c47894b9de4c59f9647da80b4 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Mon, 18 May 2015 15:31:06 +0000
Subject: [PATCH] Code cleanup

---
 opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/jeb/TestBackendImpl.java      |   47 ++++-----------
 opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/AttributeIndex.java |   15 +++--
 opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java       |   92 ++++++++++--------------------
 3 files changed, 52 insertions(+), 102 deletions(-)

diff --git a/opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/AttributeIndex.java b/opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/AttributeIndex.java
index bb70938..660aad4 100644
--- a/opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/AttributeIndex.java
+++ b/opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/AttributeIndex.java
@@ -431,9 +431,7 @@
   {
     for (MatchingRuleIndex index : indexIdToIndexes.values())
     {
-      final Set<ByteString> keys = new HashSet<>();
-      index.indexEntry(entry, keys);
-      for (ByteString key : keys)
+      for (ByteString key : indexEntry(index, entry))
       {
         buffer.put(index, key, entryID);
       }
@@ -453,15 +451,20 @@
   {
     for (MatchingRuleIndex index : indexIdToIndexes.values())
     {
-      HashSet<ByteString> keys = new HashSet<ByteString>();
-      index.indexEntry(entry, keys);
-      for (ByteString key : keys)
+      for (ByteString key : indexEntry(index, 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.
diff --git a/opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java b/opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java
index 92d24dd..5bb3005 100644
--- a/opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java
+++ b/opendj-sdk/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java
@@ -1357,8 +1357,6 @@
       super(storage);
     }
 
-    private final Set<ByteString> insertKeySet = new HashSet<>();
-    private final Set<ByteString> deleteKeySet = new HashSet<>();
     private Entry oldEntry;
 
     @Override
@@ -1413,46 +1411,20 @@
       }
 
       suffix.getID2Entry().put(txn, entryID, entry);
-      if (oldEntry != null)
-      {
-        processAllIndexes(suffix, entry, entryID);
-      }
-      else
-      {
-        processIndexes(suffix, entry, entryID);
-      }
+      processIndexes(suffix, entry, entryID, oldEntry != null);
       processVLVIndexes(txn, suffix, entry, entryID);
       importCount.getAndIncrement();
     }
 
-    void processAllIndexes(Suffix suffix, Entry entry, EntryID entryID) throws StorageRuntimeException,
-        InterruptedException
-    {
-      for (Map.Entry<AttributeType, AttributeIndex> mapEntry : suffix.getAttrIndexMap().entrySet())
-      {
-        fillIndexKey(mapEntry.getValue(), entry, mapEntry.getKey(), entryID);
-      }
-    }
-
     @Override
     void processAttribute(MatchingRuleIndex index, Entry entry, EntryID entryID, IndexKey indexKey)
         throws StorageRuntimeException, InterruptedException
     {
       if (oldEntry != null)
       {
-        deleteKeySet.clear();
-        index.indexEntry(oldEntry, deleteKeySet);
-        for (ByteString delKey : deleteKeySet)
-        {
-          processKey(index, delKey, entryID, indexKey, false);
-        }
+        processAttribute0(index, oldEntry, entryID, indexKey, false);
       }
-      insertKeySet.clear();
-      index.indexEntry(entry, insertKeySet);
-      for (ByteString key : insertKeySet)
-      {
-        processKey(index, key, entryID, indexKey, true);
-      }
+      processAttribute0(index, entry, entryID, indexKey, true);
     }
   }
 
@@ -1464,7 +1436,7 @@
   {
     private final Storage storage;
     private final Map<IndexKey, IndexOutputBuffer> indexBufferMap = new HashMap<>();
-    private final Set<ByteString> insertKeySet = new HashSet<>();
+    private final Set<ByteString> keySet = new HashSet<>();
     private final IndexKey dnIndexKey = new IndexKey(DN_TYPE, DN2ID_INDEX_NAME, 1);
 
     public ImportTask(final Storage storage)
@@ -1523,7 +1495,7 @@
       suffix.removePending(entryDN);
       processDN2ID(suffix, entryDN, entryID);
       suffix.getDN2URI().addEntry(txn, entry);
-      processIndexes(suffix, entry, entryID);
+      processIndexes(suffix, entry, entryID, false);
       processVLVIndexes(txn, suffix, entry, entryID);
       suffix.getID2Entry().put(txn, entryID, entry);
       importCount.getAndIncrement();
@@ -1564,25 +1536,24 @@
       return dnCache.insert(entryDN);
     }
 
-    void processIndexes(Suffix suffix, Entry entry, EntryID entryID) throws StorageRuntimeException,
-        InterruptedException
+    void processIndexes(Suffix suffix, Entry entry, EntryID entryID, boolean allIndexes)
+        throws StorageRuntimeException, InterruptedException
     {
       for (Map.Entry<AttributeType, AttributeIndex> mapEntry : suffix.getAttrIndexMap().entrySet())
       {
-        AttributeType attributeType = mapEntry.getKey();
-        if (entry.hasAttribute(attributeType))
+        AttributeType attrType = mapEntry.getKey();
+        AttributeIndex attrIndex = mapEntry.getValue();
+        if (allIndexes || entry.hasAttribute(attrType))
         {
-          fillIndexKey(mapEntry.getValue(), entry, attributeType, entryID);
-        }
-      }
-    }
+          for (Map.Entry<String, MatchingRuleIndex> mapEntry2 : attrIndex.getNameToIndexes().entrySet())
+          {
+            String indexID = mapEntry2.getKey();
+            MatchingRuleIndex index = mapEntry2.getValue();
 
-    void fillIndexKey(AttributeIndex attrIndex, Entry entry, AttributeType attrType, EntryID entryID)
-        throws InterruptedException, StorageRuntimeException
-    {
-      for (Map.Entry<String, MatchingRuleIndex> mapEntry : attrIndex.getNameToIndexes().entrySet())
-      {
-        processAttribute(mapEntry.getValue(), mapEntry.getKey(), entry, attrType, entryID);
+            IndexKey indexKey = new IndexKey(attrType, indexID, index.getIndexEntryLimit());
+            processAttribute(index, entry, entryID, indexKey);
+          }
+        }
       }
     }
 
@@ -1598,24 +1569,21 @@
       buffer.flush(txn);
     }
 
-    private void processAttribute(MatchingRuleIndex index, String indexID, Entry entry,
-        AttributeType attributeType, EntryID entryID) throws InterruptedException
-    {
-      if (index != null)
-      {
-        IndexKey indexKey = new IndexKey(attributeType, indexID, index.getIndexEntryLimit());
-        processAttribute(index, entry, entryID, indexKey);
-      }
-    }
-
     void processAttribute(MatchingRuleIndex index, Entry entry, EntryID entryID, IndexKey indexKey)
         throws StorageRuntimeException, InterruptedException
     {
-      insertKeySet.clear();
-      index.indexEntry(entry, insertKeySet);
-      for (ByteString key : insertKeySet)
+      processAttribute0(index, entry, entryID, indexKey, true);
+    }
+
+    void processAttribute0(MatchingRuleIndex index, Entry entry, EntryID entryID, IndexKey indexKey, boolean insert)
+        throws InterruptedException
+    {
+      keySet.clear();
+      index.indexEntry(entry, keySet);
+
+      for (ByteString key : keySet)
       {
-        processKey(index, key, entryID, indexKey, true);
+        processKey(index, key, entryID, indexKey, insert);
       }
     }
 
@@ -1631,7 +1599,7 @@
       getAll(futures);
     }
 
-    int processKey(Tree tree, ByteString key, EntryID entryID, IndexKey indexKey, boolean insert)
+    final int processKey(Tree tree, ByteString key, EntryID entryID, IndexKey indexKey, boolean insert)
         throws InterruptedException
     {
       int sizeNeeded = IndexOutputBuffer.getRequiredSize(key.length(), entryID.longValue());
diff --git a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/jeb/TestBackendImpl.java b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/jeb/TestBackendImpl.java
index bd9f098..1b59f52 100644
--- a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/jeb/TestBackendImpl.java
+++ b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/jeb/TestBackendImpl.java
@@ -93,9 +93,7 @@
 import static org.opends.server.util.StaticUtils.*;
 import static org.testng.Assert.*;
 
-/**
- * BackendImpl Tester.
- */
+/** BackendImpl Tester. */
 @SuppressWarnings("javadoc")
 public class TestBackendImpl extends JebTestCase {
 
@@ -589,7 +587,6 @@
    */
   @Test
   public void testAddNoParent() throws Exception {
-
     try
     {
       for (Entry entry : entries) {
@@ -597,11 +594,10 @@
       }
       failBecauseExceptionWasNotThrown(DirectoryException.class);
     }
-    catch (DirectoryException e)
+    catch (DirectoryException expected)
     {
        // expected
     }
-
   }
 
   @Test(dependsOnMethods = "testAddNoParent")
@@ -678,10 +674,9 @@
     backend.getNumberOfEntriesInBaseDN(DN.valueOf("ou=People,dc=test,dc=com"));
   }
 
-
   @Test(dependsOnMethods = "testAdd")
   public void testSearchIndex() throws Exception {
-    Set<String> attribs = new LinkedHashSet<String>();
+    Set<String> attribs = new LinkedHashSet<>();
     String debugString;
     List<SearchResultEntry> result;
 
@@ -734,7 +729,7 @@
   {
     int finalStartPos = debugString.indexOf("final=") + 13;
     int finalEndPos = debugString.indexOf("]", finalStartPos);
-    int finalCount = Integer.valueOf(debugString.substring(finalStartPos, finalEndPos));
+    int finalCount = Integer.parseInt(debugString.substring(finalStartPos, finalEndPos));
     assertEquals(finalCount, expectedCount);
   }
 
@@ -785,7 +780,7 @@
   @Test(dependsOnMethods = {"testAdd", "testSearchIndex",
       "testSearchScope", "testMatchedDN"})
   public void testDeleteEntry() throws Exception {
-    List<Control> noControls = new ArrayList<Control>(0);
+    List<Control> noControls = new ArrayList<>(0);
     EntryContainer ec =
         backend.getRootContainer().getEntryContainer(DN.valueOf("ou=People,dc=test,dc=com"));
 
@@ -803,7 +798,6 @@
           DN.valueOf("uid=user.539,ou=People,dc=test,dc=com"));
       backend.deleteEntry(DN.valueOf("uid=user.539,ou=People,dc=test,dc=com"), delete);
 
-
       assertFalse(ec.entryExists(DN.valueOf("uid=user.539,ou=People,dc=test,dc=com")));
       assertNull(ec.getDN2ID().get(null,
           DN.valueOf("uid=user.539,ou=People,dc=test,dc=com"), LockMode.DEFAULT));
@@ -834,7 +828,7 @@
 
   private static List<AttributeIndexer> newAttributeIndexers(AttributeType attrType, MatchingRule matchingRule)
   {
-    List<AttributeIndexer> indexers = new ArrayList<AttributeIndexer>();
+    List<AttributeIndexer> indexers = new ArrayList<>();
     for (org.forgerock.opendj.ldap.spi.Indexer indexer : matchingRule.createIndexers(getOptions()))
     {
       indexers.add(new AttributeIndexer(attrType, indexer));
@@ -851,18 +845,7 @@
 
   private static void assertIndexContainsID(List<? extends Indexer> indexers, Entry entry, Index index, EntryID entryID)
   {
-    for (Indexer indexer : indexers)
-    {
-      Set<ByteString> addKeys = new HashSet<ByteString>();
-      indexer.indexEntry(entry, addKeys);
-
-      DatabaseEntry key = new DatabaseEntry();
-      for (ByteString keyBytes : addKeys)
-      {
-        key.setData(keyBytes.toByteArray());
-        assertEquals(index.containsID(null, key, entryID), TRUE);
-      }
-    }
+    assertIndexContainsID(indexers, entry, index, entryID, TRUE);
   }
 
   private static void assertIndexContainsID(List<? extends Indexer> indexers, Entry entry, Index index,
@@ -870,7 +853,7 @@
   {
     for (Indexer indexer : indexers)
     {
-      Set<ByteString> addKeys = new HashSet<ByteString>();
+      Set<ByteString> addKeys = new HashSet<>();
       indexer.indexEntry(entry, addKeys);
 
       assertIndexContainsID(addKeys, index, entryID, expected);
@@ -961,7 +944,7 @@
     ec.sharedLock.lock();
     try
     {
-      List<Modification> modifications = new ArrayList<Modification>();
+      List<Modification> modifications = new ArrayList<>();
       modifications.add(new Modification(ADD, create("title", "debugger")));
 
       AttributeBuilder builder = new AttributeBuilder("title");
@@ -1001,16 +984,16 @@
       nameIndex = ec.getAttributeIndex(name);
 
       // This current entry in the DB shouldn't be in the presence titleIndex.
-      addKeys = new HashSet<ByteString>();
+      addKeys = new HashSet<>();
       addKeys.add(PresenceIndexer.presenceKey);
       assertIndexContainsID(addKeys, titleIndex.getIndex("presence"), entryID, FALSE);
 
       // This current entry should be in the presence nameIndex.
-      addKeys = new HashSet<ByteString>();
+      addKeys = new HashSet<>();
       addKeys.add(PresenceIndexer.presenceKey);
       assertIndexContainsID(addKeys, nameIndex.getIndex("presence"), entryID, TRUE);
 
-      List<Control> noControls = new ArrayList<Control>(0);
+      List<Control> noControls = new ArrayList<>(0);
       ModifyOperationBasis modifyOp = new ModifyOperationBasis(getRootConnection(), nextOperationID(), nextMessageID(),
           noControls, DN.valueOf("uid=user.1,ou=People,dc=test,dc=com"), modifications);
 
@@ -1124,7 +1107,7 @@
           DN.valueOf("ou=People,dc=test,dc=com"), LockMode.DEFAULT);
       assertTrue(newSuperiorID.compareTo(oldID) > 0);
 
-      List<Control> noControls = new ArrayList<Control>(0);
+      List<Control> noControls = new ArrayList<>(0);
       ModifyDNOperationBasis modifyDN = new ModifyDNOperationBasis(
           getRootConnection(), nextOperationID(), nextMessageID(),
           noControls,
@@ -1414,7 +1397,6 @@
     assertEquals(backend.getNumberOfChildren(dn), -1);
   }
 
-
   /**
    * Provides a set of DNs for the matched DN test case.
    *
@@ -1423,7 +1405,6 @@
    */
   @DataProvider(name = "MatchedDNs")
   public Object[][] initMatchedDNs() throws Exception {
-
     ResultCode success      = ResultCode.SUCCESS;
     ResultCode noSuchObject = ResultCode.NO_SUCH_OBJECT;
 
@@ -1441,7 +1422,6 @@
     };
   }
 
-
   /**
    * Executes an internal search operation and check the result code and
    * matched DN field.
@@ -1577,5 +1557,4 @@
   private static String substringIndexId() {
     return SMR_CASE_IGNORE_NAME + ":" + getOptions().substringKeySize();
   }
-
 }

--
Gitblit v1.10.0