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

Jean-Noel Rouvignac
09.59.2013 be36718d412e99be93dbb0a8775a6a57ab10eba9
ReplicationDB.java:
Collapsed try/finally embedded in try/catch into single try/catch/finally statements.
Used a static import for OperationStatus.SUCCESS to help readability with if statement conditions.
In count(), moved the cursor variable inside findFirstCounterRecordAfterStartPoint() and findFirstCounterRecordBeforeStopPoint() to ease readability.
In ReplServerDBCursor ctor, removed an uneeded else statement (because of an early exit)
1 files modified
155 ■■■■■ changed files
opends/src/server/org/opends/server/replication/server/ReplicationDB.java 155 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/replication/server/ReplicationDB.java
@@ -27,6 +27,9 @@
 */
package org.opends.server.replication.server;
import static com.sleepycat.je.LockMode.*;
import static com.sleepycat.je.OperationStatus.*;
import static org.opends.messages.ReplicationMessages.*;
import static org.opends.server.loggers.ErrorLogger.*;
import static org.opends.server.util.StaticUtils.*;
@@ -180,10 +183,7 @@
   */
  public void addEntries(List<UpdateMsg> changes)
  {
    try
    {
      dbCloseLock.readLock().lock();
      try
      {
        // If the DB has been closed then return immediately.
@@ -203,15 +203,14 @@
          counterCurrValue++;
        }
      }
      finally
      {
        dbCloseLock.readLock().unlock();
      }
    }
    catch (DatabaseException e)
    {
      replicationServer.handleUnexpectedDatabaseException(e);
    }
    finally
    {
      dbCloseLock.readLock().unlock();
    }
  }
  private void insertCounterRecordIfNeeded(ChangeNumber changeNumber)
@@ -253,19 +252,12 @@
   */
  public void shutdown()
  {
    try
    {
      dbCloseLock.writeLock().lock();
      try
      {
        db.close();
        db = null;
      }
      finally
      {
        dbCloseLock.writeLock().unlock();
      }
    }
    catch (DatabaseException e)
    {
      MessageBuilder mb = new MessageBuilder();
@@ -273,6 +265,10 @@
      mb.append(stackTraceToSingleLineString(e));
      logError(mb.toMessage());
    }
    finally
    {
      dbCloseLock.writeLock().unlock();
    }
  }
  /**
@@ -327,10 +323,9 @@
   */
  public ChangeNumber readFirstChange()
  {
    Cursor cursor = null;
    try
    {
      dbCloseLock.readLock().lock();
    Cursor cursor = null;
      try
      {
        // If the DB has been closed then return immediately.
@@ -343,8 +338,7 @@
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry data = new DatabaseEntry();
        LockMode defaultMode = LockMode.DEFAULT;
        if (cursor.getFirst(key, data, defaultMode) != OperationStatus.SUCCESS)
      if (cursor.getFirst(key, data, LockMode.DEFAULT) != SUCCESS)
        {
          // database is empty
          return null;
@@ -357,7 +351,7 @@
        }
        // First record is a counter record .. go next
        if (cursor.getNext(key, data, defaultMode) != OperationStatus.SUCCESS)
      if (cursor.getNext(key, data, LockMode.DEFAULT) != SUCCESS)
        {
          // DB contains only a counter record
          return null;
@@ -366,16 +360,15 @@
        // it is safe to return this record
        return toChangeNumber(key.getData());
      }
      finally
      {
        closeAndReleaseReadLock(cursor);
      }
    }
    catch (DatabaseException e)
    {
      replicationServer.handleUnexpectedDatabaseException(e);
      return null;
    }
    finally
    {
      closeAndReleaseReadLock(cursor);
    }
  }
@@ -387,10 +380,9 @@
   */
  public ChangeNumber readLastChange()
  {
    Cursor cursor = null;
    try
    {
      dbCloseLock.readLock().lock();
    Cursor cursor = null;
      try
      {
        // If the DB has been closed then return immediately.
@@ -403,8 +395,7 @@
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry data = new DatabaseEntry();
        LockMode defaultMode = LockMode.DEFAULT;
        if (cursor.getLast(key, data, defaultMode) != OperationStatus.SUCCESS)
      if (cursor.getLast(key, data, LockMode.DEFAULT) != SUCCESS)
        {
          // database is empty
          return null;
@@ -416,7 +407,7 @@
          return cn;
        }
        if (cursor.getPrev(key, data, defaultMode) != OperationStatus.SUCCESS)
      if (cursor.getPrev(key, data, LockMode.DEFAULT) != SUCCESS)
        {
          /*
           * database only contain a counter record - don't know how much it can
@@ -428,16 +419,15 @@
        // it is safe to return this record
        return toChangeNumber(key.getData());
      }
      finally
      {
        closeAndReleaseReadLock(cursor);
      }
    }
    catch (DatabaseException e)
    {
      replicationServer.handleUnexpectedDatabaseException(e);
      return null;
    }
    finally
    {
      closeAndReleaseReadLock(cursor);
    }
  }
  /**
@@ -456,10 +446,9 @@
      return null;
    }
    Cursor cursor = null;
    try
    {
      dbCloseLock.readLock().lock();
    Cursor cursor = null;
      try
      {
        // If the DB has been closed then return immediately.
@@ -471,13 +460,11 @@
        DatabaseEntry key = createReplicationKey(changeNumber);
        DatabaseEntry data = new DatabaseEntry();
        cursor = db.openCursor(null, null);
        if (cursor.getSearchKeyRange(key, data, LockMode.DEFAULT)
            == OperationStatus.SUCCESS)
      if (cursor.getSearchKeyRange(key, data, LockMode.DEFAULT) == SUCCESS)
        {
          // We can move close to the changeNumber.
          // Let's move to the previous change.
          if (cursor.getPrev(key, data, LockMode.DEFAULT)
              == OperationStatus.SUCCESS)
        if (cursor.getPrev(key, data, LockMode.DEFAULT) == SUCCESS)
          {
            return getRegularRecord(cursor, key, data);
          }
@@ -487,22 +474,20 @@
        {
          // We could not move the cursor past to the changeNumber
          // Check if the last change is older than changeNumber
          if (cursor.getLast(key, data, LockMode.DEFAULT)
              == OperationStatus.SUCCESS)
        if (cursor.getLast(key, data, LockMode.DEFAULT) == SUCCESS)
          {
            return getRegularRecord(cursor, key, data);
          }
        }
      }
      finally
      {
        closeAndReleaseReadLock(cursor);
      }
    }
    catch (DatabaseException e)
    {
      replicationServer.handleUnexpectedDatabaseException(e);
    }
    finally
    {
      closeAndReleaseReadLock(cursor);
    }
    return null;
  }
@@ -517,7 +502,7 @@
    // There cannot be 2 counter record next to each other,
    // it is safe to return previous record which must exist
    if (cursor.getPrev(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS)
    if (cursor.getPrev(key, data, LockMode.DEFAULT) == SUCCESS)
    {
      return toChangeNumber(key.getData());
    }
@@ -600,31 +585,26 @@
        localCursor = db.openCursor(txn, null);
        if (startingChangeNumber != null)
        {
          if (localCursor.getSearchKey(key, data, LockMode.DEFAULT) !=
            OperationStatus.SUCCESS)
          if (localCursor.getSearchKey(key, data, LockMode.DEFAULT) != SUCCESS)
          {
            // We could not move the cursor to the expected startingChangeNumber
            if (localCursor.getSearchKeyRange(key, data, LockMode.DEFAULT) !=
              OperationStatus.SUCCESS)
            if (localCursor.getSearchKeyRange(key, data, DEFAULT) != SUCCESS)
            {
              // We could not even move the cursor closed to it => failure
              throw new Exception("ChangeNumber not available");
            }
            else
            {
              // We can move close to the startingChangeNumber.
              // Let's create a cursor from that point.
              DatabaseEntry aKey = new DatabaseEntry();
              DatabaseEntry aData = new DatabaseEntry();
              if (localCursor.getPrev(aKey, aData, LockMode.DEFAULT) !=
                OperationStatus.SUCCESS)
            if (localCursor.getPrev(aKey, aData, LockMode.DEFAULT) != SUCCESS)
              {
                localCursor.close();
                localCursor = db.openCursor(txn, null);
              }
            }
          }
        }
        cursor = localCursor;
      }
      catch (Exception e)
@@ -760,8 +740,7 @@
        return null;
      }
      OperationStatus status = cursor.getNext(key, data, LockMode.DEFAULT);
      if (status != OperationStatus.SUCCESS)
      if (cursor.getNext(key, data, LockMode.DEFAULT) != SUCCESS)
      {
        return null;
      }
@@ -785,8 +764,7 @@
      {
        try
        {
          OperationStatus status = cursor.getNext(key, data, LockMode.DEFAULT);
          if (status != OperationStatus.SUCCESS)
          if (cursor.getNext(key, data, LockMode.DEFAULT) != SUCCESS)
          {
            return null;
          }
@@ -898,11 +876,7 @@
   */
  public long count(ChangeNumber start, ChangeNumber stop)
  {
    try
    {
      dbCloseLock.readLock().lock();
      Cursor cursor = null;
      try
      {
        // If the DB has been closed then return immediately.
@@ -920,10 +894,8 @@
        // Step 1 : from the start point, traverse db to the next counter record
        // or to the stop point.
        cursor = db.openCursor(null, null);
        findFirstCounterRecordAfterStartPoint(start, stop, cursor,
            counterValues, distanceToCounterRecords);
        cursor.close();
      findFirstCounterRecordAfterStartPoint(start, stop, counterValues,
          distanceToCounterRecords);
        // cases
        if (counterValues[START] == 0)
@@ -931,34 +903,33 @@
        // Step 2 : from the stop point, traverse db to the next counter record
        // or to the start point.
        cursor = db.openCursor(null, null);
        if (!findFirstCounterRecordBeforeStopPoint(start, stop, cursor,
            counterValues, distanceToCounterRecords))
      if (!findFirstCounterRecordBeforeStopPoint(start, stop, counterValues,
          distanceToCounterRecords))
        {
          // database is empty
          return 0;
        }
        cursor.close();
        // Step 3 : Now consolidates the result
        return computeDistance(counterValues, distanceToCounterRecords);
      }
      finally
      {
        closeAndReleaseReadLock(cursor);
      }
    }
    catch (DatabaseException e)
    {
      replicationServer.handleUnexpectedDatabaseException(e);
    }
    finally
    {
      dbCloseLock.readLock().unlock();
    }
    return 0;
  }
  private void findFirstCounterRecordAfterStartPoint(ChangeNumber start,
      ChangeNumber stop, Cursor cursor, int[] counterValues,
      int[] distanceToCounterRecords)
      ChangeNumber stop, int[] counterValues, int[] distanceToCounterRecords)
  {
    Cursor cursor = db.openCursor(null, null);
    try
  {
    OperationStatus status;
    DatabaseEntry key;
@@ -1002,10 +973,17 @@
      }
    }
  }
    finally
    {
      close(cursor);
    }
  }
  private boolean findFirstCounterRecordBeforeStopPoint(ChangeNumber start,
      ChangeNumber stop, Cursor cursor, int[] counterValues,
      int[] distanceToCounterRecords)
      ChangeNumber stop, int[] counterValues, int[] distanceToCounterRecords)
  {
    Cursor cursor = db.openCursor(null, null);
    try
  {
    DatabaseEntry key = createReplicationKey(stop);
    DatabaseEntry data = new DatabaseEntry();
@@ -1042,6 +1020,11 @@
    }
    return true;
  }
    finally
    {
      close(cursor);
    }
  }
  /**
   * The diagram below shows a visual description of how the distance between