From 5eb89147746f7bd91f29f3e45d3e85ca04bba9db Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Fri, 09 Sep 2016 13:21:34 +0000
Subject: [PATCH] OPENDJ-3281 Move SmallSet into its own file

---
 opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/AttrHistoricalMultiple.java |   98 +++++++++++++++++-------------------------------
 1 files changed, 35 insertions(+), 63 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/AttrHistoricalMultiple.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/AttrHistoricalMultiple.java
index 463a382..6b70441 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/AttrHistoricalMultiple.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/AttrHistoricalMultiple.java
@@ -17,8 +17,6 @@
 package org.opends.server.replication.plugin;
 
 import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.Map;
 import java.util.Set;
 
 import org.forgerock.opendj.ldap.ByteString;
@@ -30,6 +28,8 @@
 import org.opends.server.types.Entry;
 import org.opends.server.types.Modification;
 
+import com.forgerock.opendj.util.SmallSet;
+
 /**
  * This class is used to store historical information for multiple valued attributes.
  * One object of this type is created for each attribute that was changed in the entry.
@@ -42,16 +42,8 @@
   private CSN deleteTime;
   /** Last time the attribute was modified. */
   private CSN lastUpdateTime;
-  /**
-   * Change history for the values of this attribute.
-   * <p>
-   * We are using a LinkedHashMap here because we want:
-   * <ol>
-   * <li>Fast access for removing/adding a AttrValueHistorical keyed by the attribute value => Use a Map</li>
-   * <li>Ordering changes according to the CSN of each changes => Use a LinkedHashMap</li>
-   * </ol>
-   */
-  private final Map<AttrValueHistorical, AttrValueHistorical> valuesHist = new LinkedHashMap<>();
+  /** Change history for the values of this attribute. */
+  private final SmallSet<AttrValueHistorical> valuesHist = new SmallSet<>();
 
    /**
     * Create a new object from the provided information.
@@ -59,15 +51,13 @@
     * @param updateTime the last time this attribute was updated
     * @param valuesHist the new attribute values when updated.
     */
-   public AttrHistoricalMultiple(CSN deleteTime,
-       CSN updateTime,
-       Map<AttrValueHistorical,AttrValueHistorical> valuesHist)
+   AttrHistoricalMultiple(CSN deleteTime, CSN updateTime, Set<AttrValueHistorical> valuesHist)
    {
      this.deleteTime = deleteTime;
      this.lastUpdateTime = updateTime;
      if (valuesHist != null)
      {
-       this.valuesHist.putAll(valuesHist);
+      this.valuesHist.addAll(valuesHist);
      }
    }
 
@@ -82,7 +72,7 @@
     * Returns the last time when the attribute was updated.
     * @return the last time when the attribute was updated
     */
-   private CSN getLastUpdateTime()
+   CSN getLastUpdateTime()
    {
      return lastUpdateTime;
    }
@@ -94,18 +84,6 @@
    }
 
    /**
-    * Duplicate an object. CSNs are duplicated by references.
-    * <p>
-    * Method only called in tests
-    *
-    * @return the duplicated object.
-    */
-   AttrHistoricalMultiple duplicate()
-   {
-     return new AttrHistoricalMultiple(this.deleteTime, this.lastUpdateTime, this.valuesHist);
-   }
-
-   /**
     * Delete all historical information that is older than the provided CSN for
     * this attribute type.
     * Add the delete attribute state information
@@ -115,8 +93,7 @@
    {
      // iterate through the values in the valuesInfo and suppress all the values
      // that have not been added after the date of this delete.
-     Iterator<AttrValueHistorical> it = valuesHist.keySet().iterator();
-     while (it.hasNext())
+     for (Iterator<AttrValueHistorical> it = valuesHist.iterator(); it.hasNext();)
      {
        AttrValueHistorical info = it.next();
        if (csn.isNewerThanOrEqualTo(info.getValueUpdateTime()) &&
@@ -185,39 +162,34 @@
     }
   }
 
-   /**
-     * Update the historical information when a value is added.
-     *
-     * @param addedValue
-     *          values that was added
-     * @param attrType
-     * @param csn
-     *          time when the value was added
-     */
-   void add(ByteString addedValue, AttributeType attrType, CSN csn)
-   {
-     update(csn, new AttrValueHistorical(addedValue, attrType, csn, null));
-   }
+  /**
+   * Update the historical information when a value is added.
+   *
+   * @param addedValue
+   *          the added value
+   * @param attrType
+   *          the attribute type of the added value
+   * @param csn
+   *          time when the value was added
+   */
+  void add(ByteString addedValue, AttributeType attrType, CSN csn)
+  {
+    update(csn, new AttrValueHistorical(addedValue, attrType, csn, null));
+  }
 
   private void update(CSN csn, AttrValueHistorical valInfo)
   {
-    updateValInfo(valInfo, valInfo);
+    valuesHist.addOrReplace(valInfo);
     if (csn.isNewerThan(lastUpdateTime))
     {
       lastUpdateTime = csn;
     }
   }
 
-  private void updateValInfo(AttrValueHistorical oldValInfo, AttrValueHistorical newValInfo)
-  {
-    valuesHist.remove(oldValInfo);
-    valuesHist.put(newValInfo, newValInfo);
-  }
-
   @Override
   public Set<AttrValueHistorical> getValuesHistorical()
   {
-    return valuesHist.keySet();
+    return valuesHist;
   }
 
   @Override
@@ -394,7 +366,7 @@
       m.setModificationType(ModificationType.REPLACE);
       AttributeBuilder builder = new AttributeBuilder(modAttr.getAttributeDescription());
 
-      for (Iterator<AttrValueHistorical> it = valuesHist.keySet().iterator(); it.hasNext();)
+      for (Iterator<AttrValueHistorical> it = valuesHist.iterator(); it.hasNext();)
       {
         AttrValueHistorical valInfo = it.next();
 
@@ -440,7 +412,11 @@
         // update historical information
         AttrValueHistorical valInfo = new AttrValueHistorical(val, attrType, null, csn);
         AttrValueHistorical oldValInfo = valuesHist.get(valInfo);
-        if (oldValInfo != null)
+        if (oldValInfo == null)
+        {
+          valuesHist.add(valInfo);
+        }
+        else
         {
           // this value already exist in the historical information
           if (csn.equals(oldValInfo.getValueUpdateTime()))
@@ -452,17 +428,13 @@
           if (csn.isNewerThanOrEqualTo(oldValInfo.getValueDeleteTime()) &&
               csn.isNewerThanOrEqualTo(oldValInfo.getValueUpdateTime()))
           {
-            updateValInfo(oldValInfo, valInfo);
+            valuesHist.addOrReplace(valInfo);
           }
           else if (oldValInfo.isUpdate())
           {
             deleteIt = false;
           }
         }
-        else
-        {
-          updateValInfo(oldValInfo, valInfo);
-        }
 
         /* if the attribute value is not to be deleted
          * or if attribute value is not present suppress it from the
@@ -536,7 +508,7 @@
          * add it in the historical information
          * let the operation process normally
          */
-        valuesHist.put(valInfo, valInfo);
+        valuesHist.add(valInfo);
       }
       else
       {
@@ -549,7 +521,7 @@
            */
           if (csn.isNewerThan(oldValInfo.getValueUpdateTime()))
           {
-            updateValInfo(oldValInfo, valInfo);
+            valuesHist.addOrReplace(valInfo);
           }
           builder.remove(addVal);
         }
@@ -560,7 +532,7 @@
            */
           if (csn.isNewerThanOrEqualTo(oldValInfo.getValueDeleteTime()))
           {
-            updateValInfo(oldValInfo, valInfo);
+            valuesHist.addOrReplace(valInfo);
           }
           else
           {
@@ -642,7 +614,7 @@
       }
       sb.append("lastUpdateTime=").append(lastUpdateTime);
     }
-    sb.append(", valuesHist=").append(valuesHist.keySet());
+    sb.append(", valuesHist=").append(valuesHist);
     sb.append(")");
     return sb.toString();
   }

--
Gitblit v1.10.0