-
Notifications
You must be signed in to change notification settings - Fork 1.9k
IGNITE-7986: GridPartitionStateMap.entrySet() optimization. #3659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
8a905fb
76e9540
914962f
31f5a43
51619ee
7bcac5a
226444c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,35 +52,43 @@ 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.isEmpty() ? 0 : (states.length() - 1) / BITS + 1; | ||
|
|
||
| return new Iterator<Entry<Integer, GridDhtPartitionState>>() { | ||
| private int next; | ||
| private int idx; | ||
| private int cur; | ||
|
|
||
| @Override public boolean hasNext() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like has next called twice will change iterarot state |
||
| while(state(next) == null && next < size) | ||
| next++; | ||
| idx = states.nextSetBit(idx); | ||
|
|
||
| return next < size; | ||
| return idx != -1; | ||
| } | ||
|
|
||
| @Override public Entry<Integer, GridDhtPartitionState> next() { | ||
| if (!hasNext()) | ||
| throw new NoSuchElementException(); | ||
|
|
||
| cur = next; | ||
| next++; | ||
| cur = idx / BITS; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to document how it works. It is not totally clear to me after reading the code what is done below |
||
|
|
||
| int bitN = idx % BITS; | ||
|
|
||
| int st = 1 << bitN; | ||
|
|
||
| for (int i = 1; i < BITS - bitN; i++) | ||
| st |= (states.get(idx + i) ? 1 : 0) << i + bitN; | ||
|
|
||
| final int ordinal = st - 1; | ||
|
|
||
| idx += (BITS - bitN); | ||
|
|
||
| return new Entry<Integer, GridDhtPartitionState>() { | ||
| int p = cur; | ||
| int state = ordinal; | ||
|
|
||
| @Override public Integer getKey() { | ||
| return p; | ||
| } | ||
|
|
||
| @Override public GridDhtPartitionState getValue() { | ||
| return state(p); | ||
| return GridDhtPartitionState.fromOrdinal(state); | ||
| } | ||
|
|
||
| @Override public GridDhtPartitionState setValue(GridDhtPartitionState val) { | ||
|
|
@@ -219,4 +227,4 @@ private GridDhtPartitionState state(int part) { | |
| @Override public int hashCode() { | ||
| return 31 * states.hashCode() + size; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,13 +19,16 @@ | |
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Iterator; | ||
| 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; | ||
|
|
||
| import static org.junit.Assert.assertNotEquals; | ||
|
|
||
| /** | ||
| * Grid utils tests. | ||
| */ | ||
|
|
@@ -144,6 +147,27 @@ public void testCopyNoActive() { | |
| assertEquals(1, cp2.size()); | ||
| } | ||
|
|
||
| /** | ||
| * | ||
|
||
| */ | ||
| public void testIteratorNext() { | ||
| GridPartitionStateMap map = new GridPartitionStateMap(); | ||
|
|
||
| initMap(map); | ||
|
|
||
| Iterator<Map.Entry<Integer, GridDhtPartitionState>> iter = map.entrySet().iterator(); | ||
|
|
||
| Map.Entry<Integer, GridDhtPartitionState> entry1 = iter.next(); | ||
| Map.Entry<Integer, GridDhtPartitionState> entry2 = iter.next(); | ||
|
|
||
| iter.remove(); | ||
|
|
||
| assertNotNull(entry1.getValue()); | ||
| assertNotNull(entry2.getValue()); | ||
| assertNotEquals(entry1.getKey(), entry2.getKey()); | ||
|
||
| assertNotEquals(entry1.getValue(), entry2.getValue()); | ||
| } | ||
|
|
||
| /** */ | ||
| private GridPartitionStateMap initMap(GridPartitionStateMap map) { | ||
| map.put(0, GridDhtPartitionState.MOVING); | ||
|
|
@@ -159,4 +183,4 @@ private GridPartitionStateMap initMap(GridPartitionStateMap map) { | |
|
|
||
| return map; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add javadoc for this and other fields