From a8edb1ed042b62fc160b497d2f4febb28abc686d Mon Sep 17 00:00:00 2001 From: Yakov Zhdanov Date: Fri, 27 Nov 2015 11:33:51 +0300 Subject: [PATCH] fixing cache iteration small refactoring --- .../processors/cache/GridCacheAdapter.java | 67 +++++-------------- .../cache/GridCacheConcurrentMap.java | 52 +++++--------- .../dht/GridNoStorageCacheMap.java | 4 +- .../near/GridNearCacheAdapter.java | 4 +- .../cache/GridCacheConcurrentMapSelfTest.java | 2 +- 5 files changed, 37 insertions(+), 92 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java index c7467b2c8cb70..00f65f93b95c1 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java @@ -296,7 +296,7 @@ protected GridCacheAdapter() { */ @SuppressWarnings("OverriddenMethodCallDuringObjectConstruction") protected GridCacheAdapter(GridCacheContext ctx, int startSize) { - this(ctx, new GridCacheConcurrentMap(ctx, startSize, 0.75F, null)); + this(ctx, new GridCacheConcurrentMap(ctx, startSize, null)); } /** @@ -925,60 +925,25 @@ public GridCacheEntryEx entryEx(KeyCacheObject key, AffinityTopologyVersion topV */ @Nullable private GridCacheEntryEx entry0(KeyCacheObject key, AffinityTopologyVersion topVer, boolean create, boolean touch) { - GridCacheMapEntry cur = map.getEntry(key); + GridTriple t = map.putEntryIfObsoleteOrAbsent(topVer, key, null, create); - if (cur == null || cur.obsolete()) { - GridTriple t = map.putEntryIfObsoleteOrAbsent( - topVer, - key, - null, - create); + GridCacheEntryEx cur = t.get1(); + GridCacheEntryEx created = t.get2(); + GridCacheEntryEx doomed = t.get3(); - cur = t.get1(); + if (doomed != null && ctx.events().isRecordable(EVT_CACHE_ENTRY_DESTROYED)) + // Event notification. + ctx.events().addEvent(doomed.partition(), doomed.key(), locNodeId, (IgniteUuid)null, null, + EVT_CACHE_ENTRY_DESTROYED, null, false, null, false, null, null, null, true); - GridCacheEntryEx created = t.get2(); - GridCacheEntryEx doomed = t.get3(); + if (created != null) { + // Event notification. + if (ctx.events().isRecordable(EVT_CACHE_ENTRY_CREATED)) + ctx.events().addEvent(created.partition(), created.key(), locNodeId, (IgniteUuid)null, null, + EVT_CACHE_ENTRY_CREATED, null, false, null, false, null, null, null, true); - if (doomed != null && ctx.events().isRecordable(EVT_CACHE_ENTRY_DESTROYED)) - // Event notification. - ctx.events().addEvent(doomed.partition(), - doomed.key(), - locNodeId, - (IgniteUuid)null, - null, - EVT_CACHE_ENTRY_DESTROYED, - null, - false, - null, - false, - null, - null, - null, - true); - - if (created != null) { - // Event notification. - if (ctx.events().isRecordable(EVT_CACHE_ENTRY_CREATED)) - ctx.events().addEvent(created.partition(), - created.key(), - locNodeId, - (IgniteUuid)null, - null, - EVT_CACHE_ENTRY_CREATED, - null, - false, - null, - false, - null, - null, - null, - true); - - if (touch) - ctx.evicts().touch( - cur, - topVer); - } + if (touch) + ctx.evicts().touch(cur, topVer); } return cur; diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMap.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMap.java index 82930445d0cc2..1c643875d8749 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMap.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMap.java @@ -237,6 +237,7 @@ private Segment segmentFor(int hash) { * @param ctx Cache context. * @param initCap the initial capacity. The implementation * performs internal sizing to accommodate this many elements. + * @param factory Entry factory. * @param loadFactor the load factor threshold, used to control resizing. * Resizing may be performed when the average number of elements per * bin exceeds this threshold. @@ -248,8 +249,13 @@ private Segment segmentFor(int hash) { * non-positive. */ @SuppressWarnings({"unchecked"}) - protected GridCacheConcurrentMap(GridCacheContext ctx, int initCap, float loadFactor, - int concurrencyLevel) { + protected GridCacheConcurrentMap( + GridCacheContext ctx, + int initCap, + GridCacheMapEntryFactory factory, + float loadFactor, + int concurrencyLevel + ) { this.ctx = ctx; if (!(loadFactor > 0) || initCap < 0 || concurrencyLevel <= 0) @@ -289,6 +295,8 @@ protected GridCacheConcurrentMap(GridCacheContext ctx, int initCap, float loadFa for (int i = 0; i < segs.length; ++i) segs[i] = new Segment(cap, loadFactor); + + this.factory = factory; } /** @@ -298,20 +306,16 @@ protected GridCacheConcurrentMap(GridCacheContext ctx, int initCap, float loadFa * @param ctx Cache context. * @param initCap The implementation performs internal * sizing to accommodate this many elements. - * @param loadFactor the load factor threshold, used to control resizing. - * Resizing may be performed when the average number of elements per - * bin exceeds this threshold. * @param factory Entries factory. * @throws IllegalArgumentException if the initial capacity of * elements is negative or the load factor is non-positive. */ - public GridCacheConcurrentMap(GridCacheContext ctx, + public GridCacheConcurrentMap( + GridCacheContext ctx, int initCap, - float loadFactor, - @Nullable GridCacheMapEntryFactory factory) { - this(ctx, initCap, loadFactor, DFLT_CONCUR_LEVEL); - - this.factory = factory; + @Nullable GridCacheMapEntryFactory factory + ) { + this(ctx, initCap, factory, DFLT_LOAD_FACTOR, DFLT_CONCUR_LEVEL); } /** @@ -1424,13 +1428,6 @@ boolean hasReads() { return reads.sum() > 0; } - /** - * @return Number of reads. - */ - long reads() { - return reads.sum(); - } - /** * @return Header ID. */ @@ -1438,21 +1435,6 @@ int id() { return id; } - /** - * @return {@code True} if {@code ID} is even. - */ - boolean even() { - return id % 2 == 0; - } - - /** - * @return {@code True} if {@code ID} is odd. - */ - @SuppressWarnings("BadOddness") - boolean odd() { - return id % 2 == 1; - } - /** * @return Table. */ @@ -2108,9 +2090,7 @@ private ValueIterator( it.next(); // Cached value. - V val = it.currentValue(); - - return val; + return it.currentValue(); } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridNoStorageCacheMap.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridNoStorageCacheMap.java index ef6612bdaba5b..c6b969db87e52 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridNoStorageCacheMap.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridNoStorageCacheMap.java @@ -41,7 +41,7 @@ public class GridNoStorageCacheMap extends GridCacheConcurrentMap { * @param ctx Cache context. */ public GridNoStorageCacheMap(GridCacheContext ctx) { - super(ctx, 0, 0.75f, 1); + super(ctx, 0, null, 0.75f, 1); } /** {@inheritDoc} */ @@ -119,4 +119,4 @@ public GridNoStorageCacheMap(GridCacheContext ctx) { @Override public String toString() { return S.toString(GridNoStorageCacheMap.class, this); } -} \ No newline at end of file +} diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearCacheAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearCacheAdapter.java index b47148647575c..c92e4e8ae52ab 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearCacheAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearCacheAdapter.java @@ -123,9 +123,9 @@ protected GridNearCacheAdapter(GridCacheContext ctx) { /** {@inheritDoc} */ @Override public void onReconnected() { - map = new GridCacheConcurrentMap(ctx, + map = new GridCacheConcurrentMap( + ctx, ctx.config().getNearConfiguration().getNearStartSize(), - 0.75F, map.getEntryFactory()); } diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMapSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMapSelfTest.java index 3381a34081797..1f494c096aaee 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMapSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMapSelfTest.java @@ -362,4 +362,4 @@ public void testEmptyWeakIterator() throws Exception { assertEquals(0, local().map.iteratorMapSize()); } } -} \ No newline at end of file +}