Skip to content

Commit

Permalink
apply code inspection: for loop replaceable with foreach
Browse files Browse the repository at this point in the history
  • Loading branch information
ikarishinjieva committed Aug 24, 2017
1 parent 02d8585 commit 7423f45
Show file tree
Hide file tree
Showing 52 changed files with 178 additions and 200 deletions.
9 changes: 4 additions & 5 deletions src/main/java/io/mycat/MycatServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -893,12 +893,11 @@ private void performXARecoveryLog() {
// fetch the recovery log
CoordinatorLogEntry[] coordinatorLogEntries = getCoordinatorLogEntries();
// init into in memory cached
for (int i = 0; i < coordinatorLogEntries.length; i++) {
genXidSeq(coordinatorLogEntries[i].getId());
XAStateLog.flushMemoryRepository(coordinatorLogEntries[i].getId(), coordinatorLogEntries[i]);
for (CoordinatorLogEntry coordinatorLogEntry1 : coordinatorLogEntries) {
genXidSeq(coordinatorLogEntry1.getId());
XAStateLog.flushMemoryRepository(coordinatorLogEntry1.getId(), coordinatorLogEntry1);
}
for (int i = 0; i < coordinatorLogEntries.length; i++) {
CoordinatorLogEntry coordinatorLogEntry = coordinatorLogEntries[i];
for (CoordinatorLogEntry coordinatorLogEntry : coordinatorLogEntries) {
boolean needRollback = false;
boolean needCommit = false;
StringBuilder xacmd = new StringBuilder();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/mycat/backend/datasource/PhysicalDBPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -680,10 +680,10 @@ public PhysicalDatasource randomSelect(ArrayList<PhysicalDatasource> okSources)
int offset = random.nextInt(totalWeight);

// 并确定随机值落在哪个片断上
for (int i = 0; i < length; i++) {
offset -= okSources.get(i).getConfig().getWeight();
for (PhysicalDatasource okSource : okSources) {
offset -= okSource.getConfig().getWeight();
if (offset < 0) {
return okSources.get(i);
return okSource;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,7 @@ public void fieldEofResponse(byte[] header, List<byte[]> fields, List<FieldPacke

this.netOutBytes += header.length;
this.netOutBytes += eof.length;
for (int i = 0, len = fields.size(); i < len; ++i) {
byte[] field = fields.get(i);
for (byte[] field : fields) {
this.netOutBytes += field.length;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ protected ByteBuffer allocBuffer() {
public void fieldEofResponse(byte[] header, List<byte[]> fields, List<FieldPacket> fieldPacketsnull, byte[] eof,
boolean isLeft, BackendConnection conn) {
this.netOutBytes += header.length;
for (int i = 0, len = fields.size(); i < len; ++i) {
byte[] field = fields.get(i);
for (byte[] field : fields) {
this.netOutBytes += field.length;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ public void fieldEofResponse(byte[] header, List<byte[]> fields, List<FieldPacke
}
List<FieldPacket> fieldPackets = new ArrayList<FieldPacket>();

for (int i = 0; i < fields.size(); i++) {
for (byte[] field1 : fields) {
FieldPacket field = new FieldPacket();
field.read(fields.get(i));
field.read(field1);
fieldPackets.add(field);
}
nextHandler.fieldEofResponse(null, null, fieldPackets, null, this.isLeft, conn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ public void fieldEofResponse(byte[] headernull, List<byte[]> fieldsnull, final L

this.fieldPackets = fieldPackets;
List<Field> sourceFields = HandlerTool.createFields(this.fieldPackets);
for (int index = 0; index < referedSumFunctions.size(); index++) {
ItemSum sumFunc = referedSumFunctions.get(index);
for (ItemSum sumFunc : referedSumFunctions) {
ItemSum sum = (ItemSum) (HandlerTool.createItem(sumFunc, sourceFields, 0, this.isAllPushDown(),
this.type(), conn.getCharset()));
sums.add(sum);
Expand Down Expand Up @@ -126,8 +125,8 @@ public void fieldEofResponse(byte[] headernull, List<byte[]> fieldsnull, final L
*/
private List<FieldPacket> sendGroupFieldPackets(MySQLConnection conn) {
List<FieldPacket> newFps = new ArrayList<FieldPacket>();
for (int i = 0; i < sums.size(); i++) {
Item sum = sums.get(i);
for (ItemSum sum1 : sums) {
Item sum = sum1;
FieldPacket tmpfp = new FieldPacket();
sum.makeField(tmpfp);
newFps.add(tmpfp);
Expand Down Expand Up @@ -214,8 +213,7 @@ private void sendGroupRowPacket(MySQLConnection conn) {
RowDataPacket row = null;
List<Field> localFields = HandlerTool.createFields(localResultFps);
List<ItemSum> sendSums = new ArrayList<ItemSum>();
for (int i = 0; i < referedSumFunctions.size(); i++) {
ItemSum selSum = referedSumFunctions.get(i);
for (ItemSum selSum : referedSumFunctions) {
ItemSum sum = (ItemSum) HandlerTool.createItem(selSum, localFields, 0, false, HandlerType.GROUPBY,
conn.getCharset());
sendSums.add(sum);
Expand All @@ -238,8 +236,8 @@ private boolean sendGroupRowPacket(MySQLConnection conn, RowDataPacket row, List
* 由于整个语句下发,所以最后生成的rowpacket顺序为
* count(*){groupbyhandler生成的},count(*){下发到各个节点的,不是真实的值}
*/
for (int i = 0; i < sendSums.size(); i++) {
byte[] tmpb = sendSums.get(i).getRowPacketByte();
for (ItemSum sendSum : sendSums) {
byte[] tmpb = sendSum.getRowPacketByte();
newRp.add(tmpb);
}
for (int i = 0; i < row.fieldCount; i++) {
Expand All @@ -253,8 +251,7 @@ private boolean sendGroupRowPacket(MySQLConnection conn, RowDataPacket row, List
*/
private void sendNoRowGroupRowPacket(MySQLConnection conn) {
RowDataPacket newRp = new RowDataPacket(this.fieldPackets.size() + this.sums.size());
for (int i = 0; i < this.sums.size(); i++) {
ItemSum sum = this.sums.get(i);
for (ItemSum sum : this.sums) {
sum.noRowsInResult();
byte[] tmpb = sum.getRowPacketByte();
newRp.add(tmpb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public void fieldEofResponse(byte[] headernull, List<byte[]> fieldsnull, final L
this.pool = MycatServer.getInstance().getBufferPool();
this.fieldPackets = fieldPackets;
List<Field> sourceFields = HandlerTool.createFields(this.fieldPackets);
for (int index = 0; index < referedSumFunctions.size(); index++) {
ItemSum sumFunc = referedSumFunctions.get(index);
for (ItemSum sumFunc : referedSumFunctions) {
ItemSum sum = (ItemSum) (HandlerTool.createItem(sumFunc, sourceFields, 0, this.isAllPushDown(),
this.type(), conn.getCharset()));
sums.add(sum);
Expand All @@ -97,8 +96,8 @@ public void fieldEofResponse(byte[] headernull, List<byte[]> fieldsnull, final L
*/
private void sendGroupFieldPackets(BackendConnection conn) {
List<FieldPacket> newFps = new ArrayList<FieldPacket>();
for (int i = 0; i < sums.size(); i++) {
Item sum = sums.get(i);
for (ItemSum sum1 : sums) {
Item sum = sum1;
FieldPacket tmpfp = new FieldPacket();
sum.makeField(tmpfp);
newFps.add(tmpfp);
Expand Down Expand Up @@ -145,8 +144,8 @@ private void sendGroupRowPacket(MySQLConnection conn) {
* 由于整个语句下发,所以最后生成的rowpacket顺序为
* count(*){groupbyhandler生成的},count(*){下发到各个节点的,不是真实的值}
*/
for (int i = 0; i < this.sums.size(); i++) {
byte[] tmpb = this.sums.get(i).getRowPacketByte();
for (ItemSum sum : this.sums) {
byte[] tmpb = sum.getRowPacketByte();
newRp.add(tmpb);
}
for (int i = 0; i < originRp.fieldCount; i++) {
Expand Down Expand Up @@ -176,8 +175,7 @@ private void sendNoRowGroupRowPacket(MySQLConnection conn) {
RowDataPacket newRp = new RowDataPacket(this.fieldPackets.size() + this.sums.size());
// @bug 1050
// sumfuncs are front
for (int i = 0; i < this.sums.size(); i++) {
ItemSum sum = this.sums.get(i);
for (ItemSum sum : this.sums) {
sum.noRowsInResult();
byte[] tmpb = sum.getRowPacketByte();
newRp.add(tmpb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public void rollback() {
return;
}
if (errConnection != null && errConnection.size() > 0) {
for (int i = 0; i < nodes.length; i++) {
RouteResultsetNode node = nodes[i];
for (RouteResultsetNode node : nodes) {
final BackendConnection conn = session.getTarget(node);
if (errConnection.contains(conn)) {
session.getTargetMap().remove(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public void rollback() {
return;
}
if (errConnection != null && errConnection.size() > 0) {
for (int i = 0; i < nodes.length; i++) {
RouteResultsetNode node = nodes[i];
for (RouteResultsetNode node : nodes) {
final BackendConnection conn = session.getTarget(node);
if (errConnection.contains(conn)) {
session.getTargetMap().remove(node);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/io/mycat/backend/mysql/store/FileStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public void closeSilently() {
public void delete() {
if (!this.fileNames.isEmpty()) {
try {
for (int i = 0; i < this.fileNames.size(); i++) {
String fileName = fileNames.get(i);
for (String fileName : this.fileNames) {
FileUtils.tryDelete(fileName);
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ protected void resetHeap() {
heap = new ArrayMinHeap<TapeItem>(tapes.size(), this.heapCmp);
heap.clear();
// init heap
for (int i = 0; i < tapes.size(); i++) {
heap.add(new TapeItem(tapes.get(i).nextRow(), tapes.get(i)));
for (ResultDiskTape tape : tapes) {
heap.add(new TapeItem(tape.nextRow(), tape));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ public List<FilePath> newDirectoryStream() {
String[] files = f.list();
if (files != null) {
String base = f.getCanonicalPath();
for (int i = 0, len = files.length; i < len; i++) {
list.add(getPath(base + files[i]));
for (String file : files) {
list.add(getPath(base + file));
}
}
return list;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/mycat/backend/mysql/store/fs/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public static List<String> newDirectoryStream(String path) {
List<FilePath> list = FilePath.get(path).newDirectoryStream();
int len = list.size();
List<String> result = new ArrayList<String>(len);
for (int i = 0; i < len; i++) {
result.add(list.get(i).toString());
for (FilePath aList : list) {
result.add(aList.toString());
}
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/mycat/backend/mysql/xa/VersionedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public boolean accept(File dir, String name) {
}
});
if (names != null) {
for (int i = 0; i < names.length; i++) {
long sfx = extractVersion(names[i]);
for (String name : names) {
long sfx = extractVersion(name);
if (version < 0 || sfx < version)
version = sfx;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/mycat/buffer/DirectByteBufferPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public void recycle(ByteBuffer theBuf) {
int chunkCount = theBuf.capacity() / chunkSize;
DirectBuffer parentBuf = (DirectBuffer) thisNavBuf.attachment();
int startChunk = (int) ((thisNavBuf.address() - parentBuf.address()) / this.chunkSize);
for (int i = 0; i < allPages.length; i++) {
if ((recycled = allPages[i].recycleBuffer((ByteBuffer) parentBuf, startChunk, chunkCount))) {
for (ByteBufferPage allPage : allPages) {
if ((recycled = allPage.recycleBuffer((ByteBuffer) parentBuf, startChunk, chunkCount))) {
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/mycat/config/MycatConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,9 @@ public void apply() {
//ignore error
}

for (int i = 0; i < killed.size(); i++) {
if (killed.get(i).getActiveCount() != 0) {
killed.get(i).clearConsByDying();
for (PhysicalDatasource aKilled : killed) {
if (aKilled.getActiveCount() != 0) {
aKilled.clearConsByDying();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/mycat/config/Versions.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public abstract class Versions {
public static final byte PROTOCOL_VERSION = 10;

/**服务器版本**/
public static byte[] serverVersion = "5.6.29-mycat-2.17.08.0-dev-20170824112900".getBytes();
public static byte[] serverVersion = "5.6.29-mycat-2.17.08.0-dev-20170824113541".getBytes();
public static byte[] versionComment = "MyCat Server (OpenCloundDB)".getBytes();
public static final String ANNOTATION_NAME = "mycat:";
public static final String ROOT_PREFIX = "mycat";
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/io/mycat/config/loader/xml/XMLSchemaLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,7 @@ private Map<String, TableConfig> loadTables(Element node, boolean lowerCaseNames
dataNode = dataNode.substring(distPrex.length(), dataNode.length() - 1);
}

for (int j = 0; j < tableNames.length; j++) {
String tableName = tableNames[j];
for (String tableName : tableNames) {
TableConfig table = new TableConfig(tableName, primaryKey, autoIncrement, needAddLimit, tableType,
dataNode, (tableRule != null) ? tableRule.getRule() : null, ruleRequired);
checkDataNodeExists(table.getDataNodes());
Expand Down Expand Up @@ -607,10 +606,8 @@ private void loadDataNodes(Element root) {
*/
private List<String[]> mergerHostDatabase(String[] hostStrings, String[] databases) {
List<String[]> mhdList = new ArrayList<>();
for (int i = 0; i < hostStrings.length; i++) {
String hostString = hostStrings[i];
for (int i1 = 0; i1 < databases.length; i1++) {
String database = databases[i1];
for (String hostString : hostStrings) {
for (String database : databases) {
String[] hd = new String[2];
hd[0] = hostString;
hd[1] = database;
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/io/mycat/config/util/ParameterMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public static void mapping(Object object, Map<String, ?> parameter) throws Illeg
InvocationTargetException {
//获取用于导出clazz这个JavaBean的所有属性的PropertyDescriptor
PropertyDescriptor[] pds = getDescriptors(object.getClass());
for (int i = 0; i < pds.length; i++) {
PropertyDescriptor pd = pds[i];
for (PropertyDescriptor pd : pds) {
Object obj = parameter.get(pd.getName());
Object value = obj;
Class<?> cls = pd.getPropertyType();
Expand Down Expand Up @@ -133,9 +132,9 @@ private static PropertyDescriptor[] getDescriptors(Class<?> clazz) {
pds = beanInfo.getPropertyDescriptors();
list = new ArrayList<PropertyDescriptor>();
//加载每一个类型不为空的property
for (int i = 0; i < pds.length; i++) {
if (null != pds[i].getPropertyType()) {
list.add(pds[i]);
for (PropertyDescriptor pd : pds) {
if (null != pd.getPropertyType()) {
list.add(pd);
}
}
pds2 = new PropertyDescriptor[list.size()];
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/mycat/config/util/ReflectionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public class ReflectionProvider {
public Object newInstance(Class<?> type) {
try {
Constructor<?>[] c = type.getDeclaredConstructors();
for (int i = 0; i < c.length; i++) {
if (c[i].getParameterTypes().length == 0) {
if (!Modifier.isPublic(c[i].getModifiers())) {
c[i].setAccessible(true);
for (Constructor<?> aC : c) {
if (aC.getParameterTypes().length == 0) {
if (!Modifier.isPublic(aC.getModifiers())) {
aC.setAccessible(true);
}
return c[i].newInstance(new Object[0]);
return aC.newInstance(new Object[0]);
}
}
if (Serializable.class.isAssignableFrom(type)) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/io/mycat/manager/response/ShowSysLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public static void execute(ManagerConnection c, int numLines) {
String[] lines = getLinesByLogFile(filename, numLines);

boolean linesIsEmpty = true;
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
for (String line : lines) {
if (line != null) {
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode(line.substring(0, 19), c.getCharset()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ private List<File> createLocalDirs(MycatPropertyConf conf) {

String[] rdir = rootDirs.split(",");
List<File> dirs = new ArrayList<File>();
for (int i = 0; i < rdir.length; i++) {
for (String aRdir : rdir) {
try {
File localDir = JavaUtils.createDirectory(rdir[i], "datenode");
File localDir = JavaUtils.createDirectory(aRdir, "datenode");
dirs.add(localDir);
} catch (Exception e) {
LOG.error("Failed to create local dir in " + rdir[i] + ". Ignoring this directory.");
LOG.error("Failed to create local dir in " + aRdir + ". Ignoring this directory.");
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/main/java/io/mycat/memory/unsafe/types/UTF8String.java
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,9 @@ public UTF8String lpad(int len, UTF8String pad) {
public static UTF8String concat(UTF8String... inputs) {
// Compute the total length of the result.
int totalLength = 0;
for (int i = 0; i < inputs.length; i++) {
if (inputs[i] != null) {
totalLength += inputs[i].numBytes;
for (UTF8String input1 : inputs) {
if (input1 != null) {
totalLength += input1.numBytes;
} else {
return null;
}
Expand All @@ -738,10 +738,10 @@ public static UTF8String concat(UTF8String... inputs) {
// Allocate a new byte array, and copy the inputs one by one into it.
final byte[] result = new byte[totalLength];
int offset = 0;
for (int i = 0; i < inputs.length; i++) {
int len = inputs[i].numBytes;
for (UTF8String input : inputs) {
int len = input.numBytes;
Platform.copyMemory(
inputs[i].base, inputs[i].offset,
input.base, input.offset,
result, Platform.BYTE_ARRAY_OFFSET + offset,
len);
offset += len;
Expand All @@ -760,9 +760,9 @@ public static UTF8String concatWs(UTF8String separator, UTF8String... inputs) {

int numInputBytes = 0; // total number of bytes from the inputs
int numInputs = 0; // number of non-null inputs
for (int i = 0; i < inputs.length; i++) {
if (inputs[i] != null) {
numInputBytes += inputs[i].numBytes;
for (UTF8String input : inputs) {
if (input != null) {
numInputBytes += input.numBytes;
numInputs++;
}
}
Expand Down
Loading

0 comments on commit 7423f45

Please sign in to comment.