Skip to content

Commit

Permalink
fixing cache iteration
Browse files Browse the repository at this point in the history
small refactoring
  • Loading branch information
Yakov Zhdanov committed Nov 27, 2015
1 parent de08cd5 commit a8edb1e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 92 deletions.
Expand Up @@ -296,7 +296,7 @@ protected GridCacheAdapter() {
*/ */
@SuppressWarnings("OverriddenMethodCallDuringObjectConstruction") @SuppressWarnings("OverriddenMethodCallDuringObjectConstruction")
protected GridCacheAdapter(GridCacheContext<K, V> ctx, int startSize) { protected GridCacheAdapter(GridCacheContext<K, V> ctx, int startSize) {
this(ctx, new GridCacheConcurrentMap(ctx, startSize, 0.75F, null)); this(ctx, new GridCacheConcurrentMap(ctx, startSize, null));
} }


/** /**
Expand Down Expand Up @@ -925,60 +925,25 @@ public GridCacheEntryEx entryEx(KeyCacheObject key, AffinityTopologyVersion topV
*/ */
@Nullable private GridCacheEntryEx entry0(KeyCacheObject key, AffinityTopologyVersion topVer, boolean create, @Nullable private GridCacheEntryEx entry0(KeyCacheObject key, AffinityTopologyVersion topVer, boolean create,
boolean touch) { boolean touch) {
GridCacheMapEntry cur = map.getEntry(key); GridTriple<GridCacheMapEntry> t = map.putEntryIfObsoleteOrAbsent(topVer, key, null, create);


if (cur == null || cur.obsolete()) { GridCacheEntryEx cur = t.get1();
GridTriple<GridCacheMapEntry> t = map.putEntryIfObsoleteOrAbsent( GridCacheEntryEx created = t.get2();
topVer, GridCacheEntryEx doomed = t.get3();
key,
null,
create);


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(); if (created != null) {
GridCacheEntryEx doomed = t.get3(); // 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)) if (touch)
// Event notification. ctx.evicts().touch(cur, topVer);
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);
}
} }


return cur; return cur;
Expand Down
Expand Up @@ -237,6 +237,7 @@ private Segment segmentFor(int hash) {
* @param ctx Cache context. * @param ctx Cache context.
* @param initCap the initial capacity. The implementation * @param initCap the initial capacity. The implementation
* performs internal sizing to accommodate this many elements. * performs internal sizing to accommodate this many elements.
* @param factory Entry factory.
* @param loadFactor the load factor threshold, used to control resizing. * @param loadFactor the load factor threshold, used to control resizing.
* Resizing may be performed when the average number of elements per * Resizing may be performed when the average number of elements per
* bin exceeds this threshold. * bin exceeds this threshold.
Expand All @@ -248,8 +249,13 @@ private Segment segmentFor(int hash) {
* non-positive. * non-positive.
*/ */
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})
protected GridCacheConcurrentMap(GridCacheContext ctx, int initCap, float loadFactor, protected GridCacheConcurrentMap(
int concurrencyLevel) { GridCacheContext ctx,
int initCap,
GridCacheMapEntryFactory factory,
float loadFactor,
int concurrencyLevel
) {
this.ctx = ctx; this.ctx = ctx;


if (!(loadFactor > 0) || initCap < 0 || concurrencyLevel <= 0) if (!(loadFactor > 0) || initCap < 0 || concurrencyLevel <= 0)
Expand Down Expand Up @@ -289,6 +295,8 @@ protected GridCacheConcurrentMap(GridCacheContext ctx, int initCap, float loadFa


for (int i = 0; i < segs.length; ++i) for (int i = 0; i < segs.length; ++i)
segs[i] = new Segment(cap, loadFactor); segs[i] = new Segment(cap, loadFactor);

this.factory = factory;
} }


/** /**
Expand All @@ -298,20 +306,16 @@ protected GridCacheConcurrentMap(GridCacheContext ctx, int initCap, float loadFa
* @param ctx Cache context. * @param ctx Cache context.
* @param initCap The implementation performs internal * @param initCap The implementation performs internal
* sizing to accommodate this many elements. * 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. * @param factory Entries factory.
* @throws IllegalArgumentException if the initial capacity of * @throws IllegalArgumentException if the initial capacity of
* elements is negative or the load factor is non-positive. * elements is negative or the load factor is non-positive.
*/ */
public GridCacheConcurrentMap(GridCacheContext ctx, public GridCacheConcurrentMap(
GridCacheContext ctx,
int initCap, int initCap,
float loadFactor, @Nullable GridCacheMapEntryFactory factory
@Nullable GridCacheMapEntryFactory factory) { ) {
this(ctx, initCap, loadFactor, DFLT_CONCUR_LEVEL); this(ctx, initCap, factory, DFLT_LOAD_FACTOR, DFLT_CONCUR_LEVEL);

this.factory = factory;
} }


/** /**
Expand Down Expand Up @@ -1424,35 +1428,13 @@ boolean hasReads() {
return reads.sum() > 0; return reads.sum() > 0;
} }


/**
* @return Number of reads.
*/
long reads() {
return reads.sum();
}

/** /**
* @return Header ID. * @return Header ID.
*/ */
int id() { int id() {
return 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. * @return Table.
*/ */
Expand Down Expand Up @@ -2108,9 +2090,7 @@ private ValueIterator(
it.next(); it.next();


// Cached value. // Cached value.
V val = it.currentValue(); return it.currentValue();

return val;
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
Expand Down
Expand Up @@ -41,7 +41,7 @@ public class GridNoStorageCacheMap extends GridCacheConcurrentMap {
* @param ctx Cache context. * @param ctx Cache context.
*/ */
public GridNoStorageCacheMap(GridCacheContext ctx) { public GridNoStorageCacheMap(GridCacheContext ctx) {
super(ctx, 0, 0.75f, 1); super(ctx, 0, null, 0.75f, 1);
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
Expand Down Expand Up @@ -119,4 +119,4 @@ public GridNoStorageCacheMap(GridCacheContext ctx) {
@Override public String toString() { @Override public String toString() {
return S.toString(GridNoStorageCacheMap.class, this); return S.toString(GridNoStorageCacheMap.class, this);
} }
} }
Expand Up @@ -123,9 +123,9 @@ protected GridNearCacheAdapter(GridCacheContext<K, V> ctx) {


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public void onReconnected() { @Override public void onReconnected() {
map = new GridCacheConcurrentMap(ctx, map = new GridCacheConcurrentMap(
ctx,
ctx.config().getNearConfiguration().getNearStartSize(), ctx.config().getNearConfiguration().getNearStartSize(),
0.75F,
map.getEntryFactory()); map.getEntryFactory());
} }


Expand Down
Expand Up @@ -362,4 +362,4 @@ public void testEmptyWeakIterator() throws Exception {
assertEquals(0, local().map.iteratorMapSize()); assertEquals(0, local().map.iteratorMapSize());
} }
} }
} }

0 comments on commit a8edb1e

Please sign in to comment.