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

Matthew Swift
10.24.2015 7d2ed0b575ab7daccff664179046474a929a44c5
OPENDJ-1878: minor code cleanup

* JE/Pluggable EntryContainer: defend against cases where caller does not hold any locks (very common)
* Backend/DirectoryServer: update getEntry() and entryExists() Javadoc to specify that no locks are required
* other miscellaneous code cleanup.
6 files modified
183 ■■■■■ changed files
opendj-server-legacy/src/main/java/org/opends/server/api/Backend.java 38 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/BackendImpl.java 6 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/EntryContainer.java 81 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/BackendImpl.java 6 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/EntryContainer.java 24 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java 28 ●●●●● patch | view | raw | blame | history
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
  {
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;
  }
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;
  }
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} */
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;
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;
  }