Optimize allocations in GridPartitionStateMap

Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/e1ce7c97
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/e1ce7c97
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/e1ce7c97

Branch: refs/heads/ignite-gg-12389
Commit: e1ce7c972cc128b7aa82eb9baab50623292dc202
Parents: f202e0f
Author: Alexei Scherbakov <alexey.scherbak...@gmail.com>
Authored: Mon Jul 3 13:49:52 2017 +0300
Committer: Alexey Goncharuk <alexey.goncha...@gmail.com>
Committed: Mon Jul 3 13:49:52 2017 +0300

----------------------------------------------------------------------
 .../GridCachePartitionExchangeManager.java      |   8 +-
 .../dht/GridClientPartitionTopology.java        |   5 +-
 .../dht/GridDhtPartitionTopologyImpl.java       |   7 +-
 .../dht/preloader/GridDhtPartitionMap.java      |  13 +-
 .../internal/util/GridPartitionStateMap.java    |  60 ++++++-
 .../CacheLateAffinityAssignmentTest.java        |   8 +-
 .../testsuites/IgniteUtilSelfTestSuite.java     |   3 +
 .../ignite/util/GridPartitionMapSelfTest.java   | 162 +++++++++++++++++++
 8 files changed, 240 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/e1ce7c97/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCachePartitionExchangeManager.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCachePartitionExchangeManager.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCachePartitionExchangeManager.java
index 85719b7..0e541c8 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCachePartitionExchangeManager.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCachePartitionExchangeManager.java
@@ -64,7 +64,6 @@ import 
org.apache.ignite.internal.pagemem.snapshot.StartSnapshotOperationAckDisc
 import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
 import 
org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache;
 import 
org.apache.ignite.internal.processors.cache.distributed.dht.GridClientPartitionTopology;
-import 
org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionState;
 import 
org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionTopology;
 import 
org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTopologyFuture;
 import 
org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionDemandMessage;
@@ -86,6 +85,7 @@ import 
org.apache.ignite.internal.processors.query.schema.SchemaNodeLeaveExchang
 import org.apache.ignite.internal.processors.timeout.GridTimeoutObject;
 import org.apache.ignite.internal.util.GridListSet;
 import org.apache.ignite.internal.util.future.GridCompoundFuture;
+import org.apache.ignite.internal.util.GridPartitionStateMap;
 import org.apache.ignite.internal.util.future.GridFutureAdapter;
 import org.apache.ignite.internal.util.lang.IgnitePair;
 import org.apache.ignite.internal.util.tostring.GridToStringExclude;
@@ -1039,7 +1039,7 @@ public class GridCachePartitionExchangeManager<K, V> 
extends GridCacheSharedMana
             cctx.versions().last(),
             true);
 
-        Map<Object, T2<Integer,Map<Integer, GridDhtPartitionState>>> dupData = 
new HashMap<>();
+        Map<Object, T2<Integer, GridPartitionStateMap>> dupData = new 
HashMap<>();
 
         for (CacheGroupContext grp : cctx.cache().cacheGroups()) {
             if (!grp.isLocal()) {
@@ -1086,7 +1086,7 @@ public class GridCachePartitionExchangeManager<K, V> 
extends GridCacheSharedMana
      * @param affKey Cache affinity key.
      */
     private void addPartitionMap(GridDhtPartitionsSingleMessage m,
-        Map<Object, T2<Integer, Map<Integer, GridDhtPartitionState>>> dupData,
+        Map<Object, T2<Integer, GridPartitionStateMap>> dupData,
         boolean compress,
         Integer cacheId,
         GridDhtPartitionMap map,
@@ -1094,7 +1094,7 @@ public class GridCachePartitionExchangeManager<K, V> 
extends GridCacheSharedMana
         Integer dupDataCache = null;
 
         if (compress) {
-            T2<Integer, Map<Integer, GridDhtPartitionState>> state0 = 
dupData.get(affKey);
+            T2<Integer, GridPartitionStateMap> state0 = dupData.get(affKey);
 
             if (state0 != null && state0.get2().equals(map.map())) {
                 dupDataCache = state0.get1();

http://git-wip-us.apache.org/repos/asf/ignite/blob/e1ce7c97/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridClientPartitionTopology.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridClientPartitionTopology.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridClientPartitionTopology.java
index 26d12d6..9e9f4fb 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridClientPartitionTopology.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridClientPartitionTopology.java
@@ -43,6 +43,7 @@ import 
org.apache.ignite.internal.processors.cache.distributed.dht.preloader.Gri
 import 
org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture;
 import org.apache.ignite.internal.util.F0;
 import org.apache.ignite.internal.util.GridAtomicLong;
+import org.apache.ignite.internal.util.GridPartitionStateMap;
 import org.apache.ignite.internal.util.tostring.GridToStringExclude;
 import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.T2;
@@ -396,7 +397,7 @@ public class GridClientPartitionTopology implements 
GridDhtPartitionTopology {
 
         try {
             return new GridDhtPartitionMap(cctx.localNodeId(), 
updateSeq.get(), topVer,
-                Collections.<Integer, GridDhtPartitionState>emptyMap(), true);
+                GridPartitionStateMap.EMPTY, true);
         }
         finally {
             lock.readLock().unlock();
@@ -874,7 +875,7 @@ public class GridClientPartitionTopology implements 
GridDhtPartitionTopology {
 
         if (map == null)
             node2part.put(nodeId, map = new GridDhtPartitionMap(nodeId, 
updateSeq, topVer,
-                Collections.<Integer, GridDhtPartitionState>emptyMap(), 
false));
+                GridPartitionStateMap.EMPTY, false));
 
         map.updateSequence(updateSeq, topVer);
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/e1ce7c97/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtPartitionTopologyImpl.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtPartitionTopologyImpl.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtPartitionTopologyImpl.java
index 1eab708..c205c3b 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtPartitionTopologyImpl.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtPartitionTopologyImpl.java
@@ -49,6 +49,7 @@ import 
org.apache.ignite.internal.processors.cache.distributed.dht.preloader.Gri
 import 
org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture;
 import org.apache.ignite.internal.util.F0;
 import org.apache.ignite.internal.util.GridAtomicLong;
+import org.apache.ignite.internal.util.GridPartitionStateMap;
 import org.apache.ignite.internal.util.StripedCompositeReadWriteLock;
 import org.apache.ignite.internal.util.tostring.GridToStringExclude;
 import org.apache.ignite.internal.util.typedef.F;
@@ -813,7 +814,7 @@ public class GridDhtPartitionTopologyImpl implements 
GridDhtPartitionTopology {
 
     /** {@inheritDoc} */
     @Override public GridDhtPartitionMap localPartitionMap() {
-        Map<Integer, GridDhtPartitionState> map = new HashMap<>();
+        GridPartitionStateMap map = new 
GridPartitionStateMap(locParts.length());
 
         lock.readLock().lock();
 
@@ -830,7 +831,7 @@ public class GridDhtPartitionTopologyImpl implements 
GridDhtPartitionTopology {
             return new GridDhtPartitionMap(ctx.localNodeId(),
                 updateSeq.get(),
                 topVer,
-                Collections.unmodifiableMap(map),
+                map,
                 true);
         }
         finally {
@@ -1897,7 +1898,7 @@ public class GridDhtPartitionTopologyImpl implements 
GridDhtPartitionTopology {
                 map = new GridDhtPartitionMap(locNodeId,
                     updateSeq,
                     topVer,
-                    Collections.<Integer, GridDhtPartitionState>emptyMap(),
+                    GridPartitionStateMap.EMPTY,
                     false);
 
                 node2part.put(locNodeId, map);

http://git-wip-us.apache.org/repos/asf/ignite/blob/e1ce7c97/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionMap.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionMap.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionMap.java
index 0b44d3c..cfd4400 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionMap.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionMap.java
@@ -73,7 +73,7 @@ public class GridDhtPartitionMap implements 
Comparable<GridDhtPartitionMap>, Ext
     public GridDhtPartitionMap(UUID nodeId,
         long updateSeq,
         AffinityTopologyVersion top,
-        Map<Integer, GridDhtPartitionState> m,
+        GridPartitionStateMap m,
         boolean onlyActive) {
         assert nodeId != null;
         assert updateSeq > 0;
@@ -82,14 +82,7 @@ public class GridDhtPartitionMap implements 
Comparable<GridDhtPartitionMap>, Ext
         this.updateSeq = updateSeq;
         this.top = top;
 
-        map = new GridPartitionStateMap(m.size());
-
-        for (Map.Entry<Integer, GridDhtPartitionState> e : m.entrySet()) {
-            GridDhtPartitionState state = e.getValue();
-
-            if (!onlyActive || state.active())
-                put(e.getKey(), state);
-        }
+        map = new GridPartitionStateMap(m, onlyActive);
     }
 
     /**
@@ -185,7 +178,7 @@ public class GridDhtPartitionMap implements 
Comparable<GridDhtPartitionMap>, Ext
     /**
      * @return Underlying map.
      */
-    public Map<Integer, GridDhtPartitionState> map() {
+    public GridPartitionStateMap map() {
         return map;
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/e1ce7c97/modules/core/src/main/java/org/apache/ignite/internal/util/GridPartitionStateMap.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/util/GridPartitionStateMap.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/util/GridPartitionStateMap.java
index 72f8469..173ddbe 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/util/GridPartitionStateMap.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/util/GridPartitionStateMap.java
@@ -17,14 +17,12 @@
 
 package org.apache.ignite.internal.util;
 
-import java.io.Externalizable;
 import java.io.Serializable;
 import java.util.AbstractMap;
 import java.util.AbstractSet;
 import java.util.BitSet;
-import java.util.HashSet;
 import java.util.Iterator;
-import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.Set;
 import 
org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionState;
 
@@ -34,6 +32,9 @@ import 
org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartit
  * Null values are prohibited.
  */
 public class GridPartitionStateMap extends AbstractMap<Integer, 
GridDhtPartitionState> implements Serializable {
+    /** Empty map. */
+    public static final GridPartitionStateMap EMPTY = new 
GridPartitionStateMap(0);
+
     /** */
     private static final long serialVersionUID = 0L;
 
@@ -51,7 +52,7 @@ public class GridPartitionStateMap extends 
AbstractMap<Integer, GridDhtPartition
     @Override public Set<Entry<Integer, GridDhtPartitionState>> entrySet() {
         return new AbstractSet<Entry<Integer, GridDhtPartitionState>>() {
             @Override public Iterator<Entry<Integer, GridDhtPartitionState>> 
iterator() {
-                final int size = states.length() == 0 ? 0 : (states.length() - 
1)/ BITS + 1;
+                final int size = states.isEmpty() ? 0 : (states.length() - 1)/ 
BITS + 1;
 
                 return new Iterator<Entry<Integer, GridDhtPartitionState>>() {
                     private int next;
@@ -65,6 +66,9 @@ public class GridPartitionStateMap extends 
AbstractMap<Integer, GridDhtPartition
                     }
 
                     @Override public Entry<Integer, GridDhtPartitionState> 
next() {
+                        if (!hasNext())
+                            throw new NoSuchElementException();
+
                         cur = next;
                         next++;
 
@@ -111,6 +115,32 @@ public class GridPartitionStateMap extends 
AbstractMap<Integer, GridDhtPartition
         states = new BitSet(parts);
     }
 
+    /**
+     * Creates map copy.
+     * @param from Source map.
+     * @param onlyActive Retains only active partitions.
+     */
+    public GridPartitionStateMap(GridPartitionStateMap from, boolean 
onlyActive) {
+        size = from.size();
+
+        states = (BitSet)from.states.clone();
+
+        if (onlyActive) {
+            int part = 0;
+
+            int maxPart = states.size() / BITS;
+
+            while (part < maxPart) {
+                GridDhtPartitionState state = from.state(part);
+
+                if (state != null && !state.active())
+                    remove(part);
+
+                part++;
+            }
+        }
+    }
+
     /** {@inheritDoc} */
     @Override public GridDhtPartitionState put(Integer key, 
GridDhtPartitionState val) {
         assert val != null;
@@ -169,6 +199,24 @@ public class GridPartitionStateMap extends 
AbstractMap<Integer, GridDhtPartition
         for (int i = 0; i < BITS; i++)
             st |= ((states.get(off + i) ? 1 : 0) << i);
 
-        return st == 0 ? null : GridDhtPartitionState.values()[st - 1];
+        return st == 0 ? null : GridDhtPartitionState.fromOrdinal(st - 1);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean equals(Object o) {
+        if (this == o)
+            return true;
+
+        if (o == null || getClass() != o.getClass())
+            return false;
+
+        GridPartitionStateMap map = (GridPartitionStateMap)o;
+
+        return size == map.size && states.equals(map.states);
+    }
+
+    /** {@inheritDoc} */
+    @Override public int hashCode() {
+        return 31 * states.hashCode() + size;
     }
-}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/e1ce7c97/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/CacheLateAffinityAssignmentTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/CacheLateAffinityAssignmentTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/CacheLateAffinityAssignmentTest.java
index 6fac920..f1ecfa4 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/CacheLateAffinityAssignmentTest.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/CacheLateAffinityAssignmentTest.java
@@ -48,6 +48,7 @@ import 
org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
 import org.apache.ignite.cluster.ClusterNode;
 import org.apache.ignite.configuration.CacheConfiguration;
 import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.MemoryConfiguration;
 import org.apache.ignite.events.DiscoveryEvent;
 import org.apache.ignite.internal.GridKernalContext;
 import org.apache.ignite.internal.GridNodeOrderComparator;
@@ -55,7 +56,6 @@ import org.apache.ignite.internal.IgniteInternalFuture;
 import org.apache.ignite.internal.IgniteKernal;
 import org.apache.ignite.internal.TestRecordingCommunicationSpi;
 import 
org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException;
-import org.apache.ignite.internal.managers.communication.GridIoMessage;
 import org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage;
 import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
 import 
org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl;
@@ -180,6 +180,12 @@ public class CacheLateAffinityAssignmentTest extends 
GridCommonAbstractTest {
             discoSpi.setJoinTimeout(30_000);
         }
 
+        MemoryConfiguration cfg1 = new MemoryConfiguration();
+
+        cfg1.setDefaultMemoryPolicySize(50 * 1024 * 1024L);
+
+        cfg.setMemoryConfiguration(cfg1);
+
         cfg.setClientMode(client);
 
         return cfg;

http://git-wip-us.apache.org/repos/asf/ignite/blob/e1ce7c97/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
 
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
index 64a94fd..1298a75 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
@@ -42,6 +42,7 @@ import org.apache.ignite.thread.IgniteThreadPoolSizeTest;
 import org.apache.ignite.util.GridIntListSelfTest;
 import org.apache.ignite.util.GridLongListSelfTest;
 import org.apache.ignite.util.GridMessageCollectionTest;
+import org.apache.ignite.util.GridPartitionMapSelfTest;
 import org.apache.ignite.util.GridQueueSelfTest;
 import org.apache.ignite.util.GridSpinReadWriteLockSelfTest;
 import org.apache.ignite.util.GridStringBuilderFactorySelfTest;
@@ -99,6 +100,8 @@ public class IgniteUtilSelfTestSuite extends TestSuite {
         GridTestUtils.addTestIfNeeded(suite, GridNioSslSelfTest.class, 
ignoredTests);
         suite.addTestSuite(GridNioDelimitedBufferSelfTest.class);
 
+        suite.addTestSuite(GridPartitionMapSelfTest.class);
+
         //dbx
         suite.addTestSuite(PageIdUtilsSelfTest.class);
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/e1ce7c97/modules/core/src/test/java/org/apache/ignite/util/GridPartitionMapSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/util/GridPartitionMapSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/util/GridPartitionMapSelfTest.java
new file mode 100644
index 0000000..ebb4cd7
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/util/GridPartitionMapSelfTest.java
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.util;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import 
org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionState;
+import org.apache.ignite.internal.util.GridPartitionStateMap;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.apache.ignite.testframework.junits.common.GridCommonTest;
+
+/**
+ * Grid utils tests.
+ */
+@GridCommonTest(group = "Utils")
+public class GridPartitionMapSelfTest extends GridCommonAbstractTest {
+    /** */
+    public void testPartitionStateMap() {
+        GridPartitionStateMap map = initMap(new GridPartitionStateMap());
+
+        Set<Map.Entry<Integer, GridDhtPartitionState>> entries = 
map.entrySet();
+
+        assertEquals(10, map.size());
+
+        for (Map.Entry<Integer, GridDhtPartitionState> entry : entries)
+            entry.setValue(GridDhtPartitionState.OWNING);
+
+        assertEquals(10, map.size());
+
+        for (GridDhtPartitionState state : map.values())
+            assertEquals(GridDhtPartitionState.OWNING, state);
+
+        Set<Map.Entry<Integer, GridDhtPartitionState>> tmp = new HashSet<>();
+
+        for (Map.Entry<Integer, GridDhtPartitionState> entry : entries) {
+            tmp.add(entry);
+
+            entry.setValue(GridDhtPartitionState.LOST);
+        }
+
+        for (Map.Entry<Integer, GridDhtPartitionState> entry : tmp)
+            entry.setValue(GridDhtPartitionState.LOST);
+
+        for (GridDhtPartitionState state : map.values())
+            assertEquals(GridDhtPartitionState.LOST, state);
+
+        assertFalse(map.containsKey(10));
+
+        assertNull(map.remove(10));
+
+        assertEquals(10, map.size());
+
+        assertEquals(GridDhtPartitionState.LOST, map.put(9, 
GridDhtPartitionState.EVICTED));
+
+        assertEquals(10, map.size());
+
+        assertEquals(GridDhtPartitionState.EVICTED, map.put(9, 
GridDhtPartitionState.EVICTED));
+
+        assertEquals(10, map.size());
+
+        map.remove(5);
+
+        assertEquals(9, map.size());
+        assertEquals(9, map.keySet().size());
+        assertEquals(9, map.values().size());
+
+        map.clear();
+
+        assertEquals(0, map.size());
+        assertEquals(0, map.keySet().size());
+        assertEquals(0, map.values().size());
+    }
+
+    /** */
+    public void testEqualsAndHashCode() {
+        GridPartitionStateMap map1 = initMap(new GridPartitionStateMap());
+
+        GridPartitionStateMap map2 = initMap(new GridPartitionStateMap());
+
+        assertEquals(map1, map2);
+
+        assertEquals(map1.hashCode(), map2.hashCode());
+
+        assertFalse(map1.equals(new HashMap()));
+
+        assertFalse(map1.equals(null));
+    }
+
+    /**
+     *
+     */
+    public void testCopy() {
+        GridPartitionStateMap map1 = initMap(new GridPartitionStateMap());
+
+        GridPartitionStateMap cp1 = new GridPartitionStateMap(map1, false);
+
+        assertEquals(map1, cp1);
+
+        GridPartitionStateMap map2 = new GridPartitionStateMap();
+        map2.put(0, GridDhtPartitionState.MOVING);
+        map2.put(1, GridDhtPartitionState.RENTING);
+        map2.put(2, GridDhtPartitionState.LOST);
+        map2.put(3, GridDhtPartitionState.OWNING);
+        map2.put(5, GridDhtPartitionState.MOVING);
+        map2.put(6, GridDhtPartitionState.RENTING);
+        map2.put(7, GridDhtPartitionState.LOST);
+        map2.put(8, GridDhtPartitionState.OWNING);
+
+        GridPartitionStateMap cp2 = new GridPartitionStateMap(map1, true);
+
+        assertEquals(map2, cp2);
+    }
+
+    /**
+     *
+     */
+    public void testCopyNoActive() {
+        GridPartitionStateMap map2 = new GridPartitionStateMap();
+
+        map2.put(100, GridDhtPartitionState.EVICTED);
+        map2.put(101, GridDhtPartitionState.EVICTED);
+        map2.put(102, GridDhtPartitionState.EVICTED);
+        map2.put(103, GridDhtPartitionState.OWNING);
+
+        GridPartitionStateMap cp2 = new GridPartitionStateMap(map2, true);
+
+        assertEquals(1, cp2.size());
+    }
+
+    /** */
+    private GridPartitionStateMap initMap(GridPartitionStateMap map) {
+        map.put(0, GridDhtPartitionState.MOVING);
+        map.put(1, GridDhtPartitionState.RENTING);
+        map.put(2, GridDhtPartitionState.LOST);
+        map.put(3, GridDhtPartitionState.OWNING);
+        map.put(4, GridDhtPartitionState.EVICTED);
+        map.put(5, GridDhtPartitionState.MOVING);
+        map.put(6, GridDhtPartitionState.RENTING);
+        map.put(7, GridDhtPartitionState.LOST);
+        map.put(8, GridDhtPartitionState.OWNING);
+        map.put(9, GridDhtPartitionState.EVICTED);
+
+        return map;
+    }
+}
\ No newline at end of file

Reply via email to