From 2a49e131247f9490c10d8f691657e86eb0e1e534 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Wed, 07 Aug 2013 08:51:08 +0000
Subject: [PATCH] DbHandler.java: In getChanges(), simplified the code. In flush(), simplified the code and better explicited the role of the loop. Extracted encode() method. ArrayList => List. Other general code cleanups.

---
 opendj-sdk/opends/src/server/org/opends/server/replication/server/DbHandler.java |   75 +++++++++++++++++--------------------
 1 files changed, 35 insertions(+), 40 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/server/DbHandler.java b/opendj-sdk/opends/src/server/org/opends/server/replication/server/DbHandler.java
index 6d6abe0..8231012 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/server/DbHandler.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/server/DbHandler.java
@@ -180,10 +180,12 @@
     synchronized (msgQueue)
     {
       int size = msgQueue.size();
-      if ((size > queueHimark) || (queueByteSize > queueHimarkBytes))
+      if (size > queueHimark || queueByteSize > queueHimarkBytes)
+      {
         msgQueue.notify();
+      }
 
-      while ((size > queueMaxSize) || (queueByteSize > queueMaxBytes))
+      while (size > queueMaxSize || queueByteSize > queueMaxBytes)
       {
         try
         {
@@ -216,20 +218,11 @@
    */
   private List<UpdateMsg> getChanges(int number)
   {
-    int current = 0;
-    LinkedList<UpdateMsg> changes = new LinkedList<UpdateMsg>();
-
     synchronized (msgQueue)
     {
-      int size = msgQueue.size();
-      while ((current < number) && (current < size))
-      {
-        UpdateMsg msg = msgQueue.get(current);
-        current++;
-        changes.add(msg);
-      }
+      final int minAvailableNb = Math.min(number, msgQueue.size());
+      return new LinkedList<UpdateMsg>(msgQueue.subList(0, minAvailableNb));
     }
-    return changes;
   }
 
   /**
@@ -302,14 +295,14 @@
     synchronized (msgQueue)
     {
       int current = 0;
-      while ((current < number) && (!msgQueue.isEmpty()))
+      while (current < number && !msgQueue.isEmpty())
       {
         UpdateMsg msg = msgQueue.remove(); // remove first
         queueByteSize -= msg.size();
         current++;
       }
-      if ((msgQueue.size() < queueLowmark)
-          && (queueByteSize < queueLowmarkBytes))
+      if (msgQueue.size() < queueLowmark
+          && queueByteSize < queueLowmarkBytes)
       {
         msgQueue.notifyAll();
       }
@@ -338,7 +331,7 @@
       {
         try
         {
-          this.wait();
+          wait();
         } catch (Exception e)
         { /* do nothing */}
       }
@@ -370,8 +363,8 @@
 
         synchronized (msgQueue)
         {
-          if ((msgQueue.size() < queueLowmark) &&
-              (queueByteSize < queueLowmarkBytes))
+          if (msgQueue.size() < queueLowmark
+              && queueByteSize < queueLowmarkBytes)
           {
             try
             {
@@ -396,7 +389,9 @@
           done = true;
         }
         if (replicationServer != null)
+        {
           replicationServer.shutdown();
+        }
         break;
       }
     }
@@ -407,7 +402,7 @@
     synchronized (this)
     {
       done = true;
-      this.notifyAll();
+      notifyAll();
     }
   }
 
@@ -466,8 +461,8 @@
               return;
             }
 
-            if ((!changeNumber.equals(lastChange))
-                && (changeNumber.older(trimDate)))
+            if (!changeNumber.equals(lastChange)
+                && changeNumber.older(trimDate))
             {
               cursor.delete();
             }
@@ -501,7 +496,7 @@
   public void flush()
   {
     int size;
-    int chunksize = (500 < queueMaxSize ? 500 : queueMaxSize);
+    int chunksize = Math.min(queueMaxSize, 500);
 
     do
     {
@@ -512,8 +507,10 @@
         List<UpdateMsg> changes = getChanges(chunksize);
 
         // if no more changes to save exit immediately.
-        if ((changes == null) || ((size = changes.size()) == 0))
+        if (changes == null || (size = changes.size()) == 0)
+        {
           return;
+        }
 
         // save the change to the stable storage.
         db.addEntries(changes);
@@ -522,7 +519,8 @@
         // (remove from the beginning of the queue)
         clearQueue(changes.size());
       }
-    } while (size >= chunksize);
+      // loop while there are more changes in the queue
+    } while (size == chunksize);
   }
 
   /**
@@ -535,34 +533,32 @@
      * {@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("replicationServer-database",
           String.valueOf(serverId)));
       attributes.add(Attributes.create("domain-name", baseDn));
       if (firstChange != null)
       {
-        Date firstTime = new Date(firstChange.getTime());
-        attributes.add(Attributes.create("first-change", firstChange
-            .toString()
-            + " " + firstTime.toString()));
+        attributes.add(Attributes.create("first-change", encode(firstChange)));
       }
       if (lastChange != null)
       {
-        Date lastTime = new Date(lastChange.getTime());
-        attributes.add(Attributes.create("last-change", lastChange
-            .toString()
-            + " " + lastTime.toString()));
+        attributes.add(Attributes.create("last-change", encode(lastChange)));
       }
       attributes.add(
           Attributes.create("queue-size", String.valueOf(msgQueue.size())));
       attributes.add(
           Attributes.create("queue-size-bytes", String.valueOf(queueByteSize)));
-
       return attributes;
     }
 
+    private String encode(ChangeNumber changeNumber)
+    {
+      return changeNumber + " " + new Date(changeNumber.getTime());
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -592,7 +588,7 @@
   @Override
   public String toString()
   {
-    return(baseDn + " " + serverId + " " + firstChange + " " + lastChange);
+    return baseDn + " " + serverId + " " + firstChange + " " + lastChange;
   }
 
   /**
@@ -610,7 +606,6 @@
    * changes from the DB.
    * @throws Exception When an exception occurs while accessing a resource
    * from the DB.
-   *
    */
   public void clear() throws DatabaseException, Exception
   {
@@ -626,7 +621,7 @@
   }
 
   /**
-   * Getter fot the serverID of the server for which this database is managed.
+   * Getter for the serverID of the server for which this database is managed.
    *
    * @return the serverId.
    */
@@ -667,7 +662,7 @@
     // Now that we always keep the last ChangeNumber in the DB to avoid
     // expiring cookies too quickly, we need to check if the "to"
     // is older than the trim date.
-    if ((to == null) || !to.older(new ChangeNumber(latestTrimDate, 0, 0)))
+    if (to == null || !to.older(new ChangeNumber(latestTrimDate, 0, 0)))
     {
       flush();
       return db.count(from, to);

--
Gitblit v1.10.0