no need to double abs, keep size as var to not call it each time

This commit is contained in:
kimchy 2011-07-22 19:30:08 +03:00
parent 8c9dffc235
commit 1c2f25dd0f
2 changed files with 13 additions and 10 deletions

View File

@ -195,7 +195,7 @@ public class IndexRoutingTable implements Iterable<IndexShardRoutingTable> {
* An iterator over all shards (including replicas).
*/
public ShardsIterator randomAllShardsIt() {
return new PlainShardsIterator(allShards, Math.abs(counter.incrementAndGet()));
return new PlainShardsIterator(allShards, counter.incrementAndGet());
}
/**

View File

@ -28,7 +28,9 @@ import java.util.NoSuchElementException;
*/
public class PlainShardsIterator implements ShardsIterator {
protected final List<ShardRouting> shards;
private final List<ShardRouting> shards;
private final int size;
private final int origIndex;
@ -42,6 +44,7 @@ public class PlainShardsIterator implements ShardsIterator {
public PlainShardsIterator(List<ShardRouting> shards, int index) {
this.shards = shards;
this.size = shards.size();
this.index = Math.abs(index);
this.origIndex = this.index;
}
@ -57,7 +60,7 @@ public class PlainShardsIterator implements ShardsIterator {
}
@Override public boolean hasNext() {
return counter < size();
return counter < size;
}
@Override public ShardRouting next() throws NoSuchElementException {
@ -73,7 +76,7 @@ public class PlainShardsIterator implements ShardsIterator {
}
@Override public int size() {
return shards.size();
return size;
}
@Override public int sizeActive() {
@ -89,7 +92,7 @@ public class PlainShardsIterator implements ShardsIterator {
@Override public boolean hasNextActive() {
int counter = this.counter;
int index = this.index;
while (counter++ < size()) {
while (counter++ < size) {
ShardRouting shardRouting = shardModulo(index++);
if (shardRouting.active()) {
return true;
@ -109,7 +112,7 @@ public class PlainShardsIterator implements ShardsIterator {
@Override public ShardRouting nextActiveOrNull() {
int counter = this.counter;
int index = this.index;
while (counter++ < size()) {
while (counter++ < size) {
ShardRouting shardRouting = shardModulo(index++);
if (shardRouting.active()) {
this.counter = counter;
@ -135,7 +138,7 @@ public class PlainShardsIterator implements ShardsIterator {
@Override public boolean hasNextAssigned() {
int counter = this.counter;
int index = this.index;
while (counter++ < size()) {
while (counter++ < size) {
ShardRouting shardRouting = shardModulo(index++);
if (shardRouting.assignedToNode()) {
return true;
@ -155,7 +158,7 @@ public class PlainShardsIterator implements ShardsIterator {
@Override public ShardRouting nextAssignedOrNull() {
int counter = this.counter;
int index = this.index;
while (counter++ < size()) {
while (counter++ < size) {
ShardRouting shardRouting = shardModulo(index++);
if (shardRouting.assignedToNode()) {
this.counter = counter;
@ -168,7 +171,7 @@ public class PlainShardsIterator implements ShardsIterator {
return null;
}
ShardRouting shardModulo(int counter) {
return shards.get((counter % size()));
final ShardRouting shardModulo(int counter) {
return shards.get((counter % size));
}
}