From 8c0b9cbba08dee4c4ad6fe00357a018cdb54e280 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 05 Jun 2014 10:46:07 +0000
Subject: [PATCH] OPENDJ-1453 (CR-3667) Change time heart beat change numbers should be synced with updates

---
 opendj-sdk/opends/src/server/org/opends/server/replication/common/MultiDomainServerState.java |   53 ++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/common/MultiDomainServerState.java b/opendj-sdk/opends/src/server/org/opends/server/replication/common/MultiDomainServerState.java
index f6273b6..f451cab 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/common/MultiDomainServerState.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/common/MultiDomainServerState.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2006-2009 Sun Microsystems, Inc.
- *      Portions Copyright 2011-2013 ForgeRock AS
+ *      Portions Copyright 2011-2014 ForgeRock AS
  */
 package org.opends.server.replication.common;
 
@@ -40,6 +40,8 @@
 import org.opends.server.types.DirectoryException;
 import org.opends.server.types.ResultCode;
 
+import com.forgerock.opendj.util.Pair;
+
 import static org.opends.messages.ReplicationMessages.*;
 
 /**
@@ -98,7 +100,9 @@
   public boolean update(DN baseDN, CSN csn)
   {
     if (csn == null)
+    {
       return false;
+    }
 
     ServerState serverState = list.get(baseDN);
     if (serverState == null)
@@ -242,6 +246,45 @@
   }
 
   /**
+   * Returns the oldest Pair&lt;DN, CSN&gt; held in current object, excluding
+   * the provided CSNs. Said otherwise, the value returned is the oldest
+   * Pair&lt;DN, CSN&gt; included in the current object, that is not part of the
+   * excludedCSNs.
+   *
+   * @param excludedCSNs
+   *          the CSNs that cannot be returned
+   * @return the oldest Pair&lt;DN, CSN&gt; included in the current object that
+   *         is not part of the excludedCSNs, or {@link Pair#EMPTY} if no such
+   *         older CSN exists.
+   */
+  public Pair<DN, CSN> getOldestCSNExcluding(MultiDomainServerState excludedCSNs)
+  {
+    Pair<DN, CSN> oldest = Pair.empty();
+    for (Entry<DN, ServerState> entry : list.entrySet())
+    {
+      final DN baseDN = entry.getKey();
+      final ServerState value = entry.getValue();
+      for (Entry<Integer, CSN> entry2 : value.getServerIdToCSNMap().entrySet())
+      {
+        final CSN csn = entry2.getValue();
+        if (!isReplicaExcluded(excludedCSNs, baseDN, csn)
+            && (oldest == Pair.EMPTY || csn.isOlderThan(oldest.getSecond())))
+        {
+          oldest = Pair.of(baseDN, csn);
+        }
+      }
+    }
+    return oldest;
+  }
+
+  private boolean isReplicaExcluded(MultiDomainServerState excluded, DN baseDN,
+      CSN csn)
+  {
+    return excluded != null
+        && csn.equals(excluded.getCSN(baseDN, csn.getServerId()));
+  }
+
+  /**
    * Removes the mapping to the provided CSN if it is present in this
    * MultiDomainServerState.
    *
@@ -253,12 +296,8 @@
    */
   public boolean removeCSN(DN baseDN, CSN expectedCSN)
   {
-    ServerState ss = list.get(baseDN);
-    if (ss != null)
-    {
-      return ss.removeCSN(expectedCSN);
-    }
-    return false;
+    final ServerState ss = list.get(baseDN);
+    return ss != null && ss.removeCSN(expectedCSN);
   }
 
   /**

--
Gitblit v1.10.0