From 72679f25485c71bc5891bf9d1a5fd27b309269bd Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 02 Jul 2013 08:23:52 +0000
Subject: [PATCH] DraftCNDB.java, DraftCNDbHandler.java: Code cleanup. Added comments.

---
 opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDB.java        |   52 ++++++++++++-------------
 opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDbHandler.java |   62 ++++++++++++++++--------------
 2 files changed, 58 insertions(+), 56 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDB.java b/opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDB.java
index 26685d8..3df93fd 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDB.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDB.java
@@ -26,12 +26,13 @@
  *      Portions Copyright 2011-2013 ForgeRock AS
  */
 package org.opends.server.replication.server;
-import static org.opends.messages.ReplicationMessages.*;
-import static org.opends.server.loggers.ErrorLogger.logError;
-import static org.opends.server.loggers.debug.DebugLogger.getTracer;
-import static org.opends.server.util.StaticUtils.decodeUTF8;
-import static org.opends.server.util.StaticUtils.stackTraceToSingleLineString;
 
+import static org.opends.messages.ReplicationMessages.*;
+import static org.opends.server.loggers.ErrorLogger.*;
+import static org.opends.server.loggers.debug.DebugLogger.*;
+import static org.opends.server.util.StaticUtils.*;
+
+import java.io.Closeable;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import org.opends.messages.MessageBuilder;
@@ -53,8 +54,10 @@
   private ReplicationDbEnv dbenv = null;
   private ReplicationServer replicationServer;
 
-  // The lock used to provide exclusive access to the thread that
-  // close the db (shutdown or clear).
+  /**
+   * The lock used to provide exclusive access to the thread that close the db
+   * (shutdown or clear).
+   */
   private ReentrantReadWriteLock dbCloseLock;
 
   /**
@@ -198,17 +201,7 @@
   {
     try
     {
-      if (cursor != null)
-      {
-        try
-        {
-          cursor.close();
-        }
-        catch (DatabaseException e)
-        {
-          // Ignore.
-        }
-      }
+      close(cursor);
     }
     finally
     {
@@ -341,13 +334,15 @@
   /**
    * This Class implements a cursor that can be used to browse the database.
    */
-  public class DraftCNDBCursor
+  public class DraftCNDBCursor implements Closeable
   {
     private final Cursor cursor;
 
-    // The transaction that will protect the actions done with the cursor
-    // Will be let null for a read cursor
-    // Will be set non null for a write cursor
+    /**
+     * The transaction that will protect the actions done with the cursor.
+     * Will be let null for a read cursor.
+     * Will be set non null for a write cursor.
+     */
     private final Transaction txn;
     private final DatabaseEntry key;
     private final DatabaseEntry entry;
@@ -499,6 +494,7 @@
     /**
      * Close the ReplicationServer Cursor.
      */
+    @Override
     public void close()
     {
       synchronized (this)
@@ -625,7 +621,7 @@
       try
       {
         String str = decodeUTF8(key.getData());
-        return Integer.valueOf(str);
+        return Integer.parseInt(str);
       }
       catch (Exception e)
       {
@@ -764,10 +760,12 @@
     }
   }
 
-
-
-  //Returns {@code true} if the DB is closed. This method assumes that either
-  // the db read/write lock has been taken.
+  /**
+   * Returns {@code true} if the DB is closed. This method assumes that either
+   * the db read/write lock has been taken.
+   *
+   * @return {@code true} if the DB is closed.
+   */
   private boolean isDBClosed()
   {
     return db == null;
diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDbHandler.java b/opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDbHandler.java
index 9a3aa03..f4ca094 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDbHandler.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDbHandler.java
@@ -26,13 +26,15 @@
  *      Portions Copyright 2011-2013 ForgeRock AS
  */
 package org.opends.server.replication.server;
+
 import static org.opends.messages.ReplicationMessages.*;
-import static org.opends.server.loggers.ErrorLogger.logError;
-import static org.opends.server.loggers.debug.DebugLogger.debugEnabled;
-import static org.opends.server.loggers.debug.DebugLogger.getTracer;
-import static org.opends.server.util.StaticUtils.stackTraceToSingleLineString;
+import static org.opends.server.loggers.ErrorLogger.*;
+import static org.opends.server.loggers.debug.DebugLogger.*;
+import static org.opends.server.util.StaticUtils.*;
 
 import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.locks.ReentrantLock;
 
 import org.opends.messages.MessageBuilder;
@@ -43,6 +45,7 @@
 import org.opends.server.core.DirectoryServer;
 import org.opends.server.loggers.debug.DebugTracer;
 import org.opends.server.replication.common.ChangeNumber;
+import org.opends.server.replication.common.MultiDomainServerState;
 import org.opends.server.replication.common.ServerState;
 import org.opends.server.replication.server.DraftCNDB.DraftCNDBCursor;
 import org.opends.server.types.Attribute;
@@ -50,8 +53,6 @@
 import org.opends.server.types.InitializationException;
 
 import com.sleepycat.je.DatabaseException;
-import java.util.Map;
-import org.opends.server.replication.common.MultiDomainServerState;
 
 /**
  * This class is used for managing the replicationServer database for each
@@ -70,7 +71,7 @@
    * The tracer object for the debug logger.
    */
   private static final DebugTracer TRACER = getTracer();
-  static int NO_KEY = 0;
+  private static int NO_KEY = 0;
 
   private DraftCNDB db;
   private int firstkey = NO_KEY;
@@ -78,16 +79,19 @@
   private DbMonitorProvider dbMonitor = new DbMonitorProvider();
   private boolean shutdown = false;
   private boolean trimDone = false;
-  /*
-  A dedicated thread loops trim().
-  trim()  : deletes from the DB a number of changes that are older than a
-  certain date.
-  */
+  /**
+   * A dedicated thread loops trim().
+   * <p>
+   * trim() : deletes from the DB a number of changes that are older than a
+   * certain date.
+   */
   private DirectoryThread thread;
   /**
    * The trim age in milliseconds. Changes record in the change DB that
    * are older than this age are removed.
    */
+  // FIXME it never gets updated even when the replication server purge delay
+  // is updated
   private long trimAge;
 
   private ReplicationServer replicationServer;
@@ -114,7 +118,7 @@
     lastkey = db.readLastDraftCN();
 
     // Trimming thread
-    thread = new DirectoryThread(this, "Replication DraftCN db ");
+    thread = new DirectoryThread(this, "Replication DraftCN db");
     thread.start();
 
     // Monitoring registration
@@ -131,7 +135,6 @@
    * @param value The associated value.
    * @param serviceID The associated serviceID.
    * @param cn The associated replication change number.
-   *
    */
   public synchronized void add(int key, String value, String serviceID,
       ChangeNumber cn)
@@ -265,6 +268,7 @@
    * Periodically Flushes the ReplicationServerDomain cache from memory to the
    * stable storage and trims the old updates.
    */
+  @Override
   public void run()
   {
     while (!shutdown)
@@ -323,11 +327,15 @@
    * changes from the DB.
    * @throws Exception When an exception occurs while accessing a resource
    * from the DB.
-   *
    */
   public void clear(String serviceIDToClear)
       throws DatabaseException, Exception
   {
+    // FIXME according to JE javadoc, this is a "fairly expensive operation"
+    // It could be faster to:
+    // - open a cursor
+    // - check if there is a next entry
+    // - close the cursor (or reuse it down below)
     if (this.count() == 0)
     {
       return;
@@ -400,10 +408,8 @@
             cnVector = cnStartStates.get(serviceID);
 
             if (debugEnabled())
-              TRACER.debugInfo("DraftCNDBHandler:clear() - ChangeVector:"+
-                      cnVector.toString()+
-                      " -- StartState:"+startState.toString());
-            // cnVector.update(cn);
+              TRACER.debugInfo("DraftCNDBHandler:clear() - ChangeVector:" +
+                      cnVector + " -- StartState:" + startState);
           }
           catch(Exception e)
           {
@@ -419,8 +425,8 @@
           {
             cursor.delete();
             if (debugEnabled())
-              TRACER.debugInfo("DraftCNDBHandler:clear() - deleted "+
-                      cn.toString()+"Not covering startState");
+              TRACER.debugInfo("DraftCNDBHandler:clear() - deleted " +
+                      cn + "Not covering startState");
             continue;
           }
 
@@ -452,9 +458,9 @@
      * {@inheritDoc}
      */
     @Override
-    public ArrayList<Attribute> getMonitorData()
+    public List<Attribute> getMonitorData()
     {
-      ArrayList<Attribute> attributes = new ArrayList<Attribute>();
+      List<Attribute> attributes = new ArrayList<Attribute>();
       attributes.add(Attributes.create("first-draft-changenumber",
           Integer.toString(db.readFirstDraftCN())));
       attributes.add(Attributes.create("last-draft-changenumber",
@@ -490,7 +496,7 @@
   @Override
   public String toString()
   {
-    return("draftCNdb:" + " " + firstkey + " " + lastkey);
+    return "draftCNdb:" + " " + firstkey + " " + lastkey;
   }
 
   /**
@@ -508,7 +514,6 @@
    * changes from the DB.
    * @throws Exception When an exception occurs while accessing a resource
    * from the DB.
-   *
    */
   public void clear() throws DatabaseException, Exception
   {
@@ -530,7 +535,7 @@
 
   /**
    * Takes the lock on this object (blocking until lock can be acquired).
-   * @throws java.lang.InterruptedException If interrupted.
+   * @throws InterruptedException If interrupted.
    */
    public void lock() throws InterruptedException
   {
@@ -567,13 +572,12 @@
           " first=" + db.readFirstDraftCN() +
           " last=" + db.readLastDraftCN() +
           " count=" + db.count() +
-          " exception" + e + " " + e.getMessage());
+          " exception " + e + " " + e.getMessage());
       return null;
     }
     finally
     {
-      if (draftCNDBCursor != null)
-        draftCNDBCursor.close();
+      close(draftCNDBCursor);
     }
     return value;
   }

--
Gitblit v1.10.0