From 7d2ed0b575ab7daccff664179046474a929a44c5 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Fri, 10 Apr 2015 15:24:40 +0000
Subject: [PATCH] OPENDJ-1878: minor code cleanup

---
 opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/EntryContainer.java       |   81 +++++++------------
 opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java              |   28 ++----
 opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/EntryContainer.java |   24 +----
 opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/BackendImpl.java    |    6 -
 opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/BackendImpl.java          |    6 -
 opendj-server-legacy/src/main/java/org/opends/server/api/Backend.java                       |   38 ++++-----
 6 files changed, 66 insertions(+), 117 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/api/Backend.java b/opendj-server-legacy/src/main/java/org/opends/server/api/Backend.java
index a8a9ccf..636bb23 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/api/Backend.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/api/Backend.java
@@ -366,16 +366,14 @@
   }
 
   /**
-   * Retrieves the requested entry from this backend.  Note that the
-   * caller must hold a read or write lock on the specified DN.
+   * Retrieves the requested entry from this backend. The caller is not required to hold any locks
+   * on the specified DN.
    *
-   * @param  entryDN  The distinguished name of the entry to retrieve.
-   *
-   * @return  The requested entry, or {@code null} if the entry does
-   *          not exist.
-   *
-   * @throws  DirectoryException  If a problem occurs while trying to
-   *                              retrieve the entry.
+   * @param entryDN
+   *          The distinguished name of the entry to retrieve.
+   * @return The requested entry, or {@code null} if the entry does not exist.
+   * @throws DirectoryException
+   *           If a problem occurs while trying to retrieve the entry.
    */
   public abstract Entry getEntry(DN entryDN) throws DirectoryException;
 
@@ -414,20 +412,16 @@
   public abstract long numSubordinates(DN entryDN, boolean subtree) throws DirectoryException;
 
   /**
-   * Indicates whether an entry with the specified DN exists in the
-   * backend. The default implementation obtains a read lock and calls
-   * {@code getEntry}, but backend implementations may override this
-   * with a more efficient version that does not require a lock.  The
-   * caller is not required to hold any locks on the specified DN.
+   * Indicates whether an entry with the specified DN exists in the backend. The default
+   * implementation calls {@code getEntry}, but backend implementations may override this with a
+   * more efficient version. The caller is not required to hold any locks on the specified DN.
    *
-   * @param  entryDN  The DN of the entry for which to determine
-   *                  existence.
-   *
-   * @return  {@code true} if the specified entry exists in this
-   *          backend, or {@code false} if it does not.
-   *
-   * @throws  DirectoryException  If a problem occurs while trying to
-   *                              make the determination.
+   * @param entryDN
+   *          The DN of the entry for which to determine existence.
+   * @return {@code true} if the specified entry exists in this backend, or {@code false} if it does
+   *         not.
+   * @throws DirectoryException
+   *           If a problem occurs while trying to make the determination.
    */
   public boolean entryExists(DN entryDN) throws DirectoryException
   {
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/BackendImpl.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/BackendImpl.java
index a517e4e..5069936 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/BackendImpl.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/BackendImpl.java
@@ -429,14 +429,12 @@
   public Entry getEntry(DN entryDN) throws DirectoryException
   {
     readerBegin();
-
     checkRootContainerInitialized();
     EntryContainer ec = rootContainer.getEntryContainer(entryDN);
     ec.sharedLock.lock();
-    Entry entry;
     try
     {
-      entry = ec.getEntry(entryDN);
+      return ec.getEntry(entryDN);
     }
     catch (DatabaseException e)
     {
@@ -448,8 +446,6 @@
       ec.sharedLock.unlock();
       readerEnd();
     }
-
-    return entry;
   }
 
 
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/EntryContainer.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/EntryContainer.java
index f2e4406..5184f4c 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/EntryContainer.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/EntryContainer.java
@@ -1221,7 +1221,7 @@
   Entry getEntry(EntryID entryID) throws DirectoryException
   {
     // Try the entry cache first.
-    final EntryCache entryCache = getEntryCache();
+    final EntryCache<?> entryCache = getEntryCache();
     final Entry cacheEntry = entryCache.getEntry(backend, entryID.longValue());
     if (cacheEntry != null)
     {
@@ -1874,9 +1874,7 @@
 
     try
     {
-      // Read the ID from dn2id.
-      EntryID id = dn2id.get(null, entryDN, LockMode.DEFAULT);
-      return id != null;
+      return dn2id.get(null, entryDN, LockMode.DEFAULT) != null;
     }
     catch (DatabaseException e)
     {
@@ -1886,63 +1884,46 @@
   }
 
   /**
-   * Fetch an entry by DN, trying the entry cache first, then the database.
-   * Retrieves the requested entry, trying the entry cache first,
-   * then the database.  Note that the caller must hold a read or write lock
-   * on the specified DN.
+   * Fetch an entry by DN, trying the entry cache first, then the database. Retrieves the requested
+   * entry, trying the entry cache first, then the database.
    *
-   * @param entryDN The distinguished name of the entry to retrieve.
-   * @return The requested entry, or <CODE>null</CODE> if the entry does not
-   *         exist.
-   * @throws DirectoryException If a problem occurs while trying to retrieve
-   *                            the entry.
-   * @throws DatabaseException An error occurred during a database operation.
+   * @param entryDN
+   *          The distinguished name of the entry to retrieve.
+   * @return The requested entry, or <CODE>null</CODE> if the entry does not exist.
+   * @throws DirectoryException
+   *           If a problem occurs while trying to retrieve the entry.
+   * @throws DatabaseException
+   *           An error occurred during a database operation.
    */
-  Entry getEntry(DN entryDN)
-  throws DatabaseException, DirectoryException
+  Entry getEntry(DN entryDN) throws DatabaseException, DirectoryException
   {
     EntryCache<?> entryCache = DirectoryServer.getEntryCache();
-    Entry entry = null;
-
-    // Try the entry cache first.
     if (entryCache != null)
     {
-      entry = entryCache.getEntry(entryDN);
+      Entry entry = entryCache.getEntry(entryDN);
+      if (entry != null)
+      {
+        return entry;
+      }
     }
 
-    if (entry == null)
+    EntryID entryID = dn2id.get(null, entryDN, LockMode.DEFAULT);
+    if (entryID == null)
     {
-      // Read dn2id.
-      EntryID entryID = dn2id.get(null, entryDN, LockMode.DEFAULT);
-      if (entryID == null)
-      {
-        // The entryDN does not exist.
-
-        // Check for referral entries above the target entry.
-        dn2uri.targetEntryReferrals(entryDN, null);
-
-        return null;
-      }
-
-      // Read id2entry.
-      entry = id2entry.get(null, entryID, LockMode.DEFAULT);
-
-      if (entry == null)
-      {
-        // The entryID does not exist.
-        throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
-            ERR_JEB_MISSING_ID2ENTRY_RECORD.get(entryID));
-      }
-
-      // Put the entry in the cache making sure not to overwrite
-      // a newer copy that may have been inserted since the time
-      // we read the cache.
-      if (entryCache != null)
-      {
-        entryCache.putEntryIfAbsent(entry, backend, entryID.longValue());
-      }
+      // The entryDN does not exist. Check for referral entries above the target entry.
+      dn2uri.targetEntryReferrals(entryDN, null);
+      return null;
     }
 
+    Entry entry = id2entry.get(null, entryID, LockMode.DEFAULT);
+    if (entry != null && entryCache != null)
+    {
+      /*
+       * Put the entry in the cache making sure not to overwrite a newer copy that may have been
+       * inserted since the time we read the cache.
+       */
+      entryCache.putEntryIfAbsent(entry, backend, entryID.longValue());
+    }
     return entry;
   }
 
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/BackendImpl.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/BackendImpl.java
index c00bd21..bc06b09 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/BackendImpl.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/BackendImpl.java
@@ -376,12 +376,10 @@
   public Entry getEntry(DN entryDN) throws DirectoryException
   {
     EntryContainer ec = accessBegin(null, entryDN);
-
     ec.sharedLock.lock();
-    Entry entry;
     try
     {
-      entry = ec.getEntry(entryDN);
+      return ec.getEntry(entryDN);
     }
     catch (StorageRuntimeException e)
     {
@@ -393,8 +391,6 @@
       ec.sharedLock.unlock();
       accessEnd();
     }
-
-    return entry;
   }
 
   /** {@inheritDoc} */
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/EntryContainer.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/EntryContainer.java
index 5c23733..f54798a 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/EntryContainer.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/EntryContainer.java
@@ -1921,8 +1921,6 @@
   private Entry getEntry0(ReadableTransaction txn, final DN entryDN) throws StorageRuntimeException, DirectoryException
   {
     final EntryCache<?> entryCache = DirectoryServer.getEntryCache();
-
-    // Try the entry cache first.
     if (entryCache != null)
     {
       final Entry entry = entryCache.getEntry(entryDN);
@@ -1934,29 +1932,21 @@
 
     try
     {
-      // Read dn2id.
-      EntryID entryID = dn2id.get(txn, entryDN);
+      final EntryID entryID = dn2id.get(txn, entryDN);
       if (entryID == null)
       {
-        // The entryDN does not exist.
-        // Check for referral entries above the target entry.
+        // The entryDN does not exist. Check for referral entries above the target entry.
         dn2uri.targetEntryReferrals(txn, entryDN, null);
         return null;
       }
 
-      // Read id2entry.
       final Entry entry = id2entry.get(txn, entryID);
-      if (entry == null)
+      if (entry != null && entryCache != null)
       {
-        // The entryID does not exist.
-        throw new DirectoryException(getServerErrorResultCode(), ERR_JEB_MISSING_ID2ENTRY_RECORD.get(entryID));
-      }
-
-      // Put the entry in the cache making sure not to overwrite
-      // a newer copy that may have been inserted since the time
-      // we read the cache.
-      if (entryCache != null)
-      {
+        /*
+         * Put the entry in the cache making sure not to overwrite a newer copy that may have been
+         * inserted since the time we read the cache.
+         */
         entryCache.putEntryIfAbsent(entry, backend, entryID.longValue());
       }
       return entry;
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java b/opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java
index 937f721..0dbcad8 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java
@@ -5446,32 +5446,24 @@
 
 
   /**
-   * Retrieves the entry with the requested DN.  It will first determine which
-   * backend should be used for this DN and will then use that backend to
-   * retrieve the entry.  The caller must already hold the appropriate lock on
-   * the specified entry.
+   * Retrieves the entry with the requested DN. It will first determine which backend should be used
+   * for this DN and will then use that backend to retrieve the entry. The caller is not required to
+   * hold any locks on the specified DN.
    *
-   * @param  entryDN  The DN of the entry to retrieve.
-   *
-   * @return  The requested entry, or <CODE>null</CODE> if it does not exist.
-   *
-   * @throws  DirectoryException  If a problem occurs while attempting to
-   *                              retrieve the entry.
+   * @param entryDN
+   *          The DN of the entry to retrieve.
+   * @return The requested entry, or <CODE>null</CODE> if it does not exist.
+   * @throws DirectoryException
+   *           If a problem occurs while attempting to retrieve the entry.
    */
-  public static Entry getEntry(DN entryDN)
-         throws DirectoryException
+  public static Entry getEntry(DN entryDN) throws DirectoryException
   {
     if (entryDN.isRootDN())
     {
       return directoryServer.rootDSEBackend.getRootDSE();
     }
-
     final Backend<?> backend = getBackend(entryDN);
-    if (backend != null)
-    {
-      return backend.getEntry(entryDN);
-    }
-    return null;
+    return backend != null ? backend.getEntry(entryDN) : null;
   }
 
 

--
Gitblit v1.10.0