Remove need for lenient tryParse method

This commit is contained in:
Jason Tedor 2015-09-17 23:07:39 -04:00
parent c281826702
commit f0e20cf594
4 changed files with 12 additions and 121 deletions

View File

@ -31,7 +31,6 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.util.primitives.Integers;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.env.ShardLock;
import org.elasticsearch.gateway.MetaDataStateFormat;
@ -359,12 +358,11 @@ public class MultiDataPathUpgrader {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(indexPath)) {
String currentIndex = indexPath.getFileName().toString();
for (Path shardPath : stream) {
if (Files.isDirectory(shardPath)) {
Integer shardId = Integers.tryParse(shardPath.getFileName().toString());
if (shardId != null) {
ShardId id = new ShardId(currentIndex, shardId);
shardIds.add(id);
}
String fileName = shardPath.getFileName().toString();
if (Files.isDirectory(shardPath) && fileName.chars().allMatch(Character::isDigit)) {
int shardId = Integer.parseInt(fileName);
ShardId id = new ShardId(currentIndex, shardId);
shardIds.add(id);
}
}
}

View File

@ -19,62 +19,10 @@
package org.elasticsearch.common.util.primitives;
import org.elasticsearch.common.Strings;
import java.util.*;
import java.util.Collection;
import java.util.Objects;
public class Integers {
/**
* Tries to parse the given String to an int
*
* @param value the String to try to parse to an int
* @return the parsed value as an int or null if the String can not be parsed to an int
*/
public static Integer tryParse(String value) {
if (Strings.isNullOrEmpty(value)) {
return null;
} else {
boolean negative = value.charAt(0) == '-';
int index = negative ? 1 : 0;
if (index == value.length()) {
return null;
} else {
int digit = digit(value.charAt(index++));
if (digit != -1) {
// so we can accumulate to Integer.MIN_VALUE
int accumulator = -digit;
for (int cap = Integer.MIN_VALUE / 10; index < value.length(); accumulator -= digit) {
digit = digit(value.charAt(index++));
if (digit == -1 || accumulator < cap) {
// non-digit or will overflow
return null;
}
accumulator *= 10;
if (accumulator < Integer.MIN_VALUE + digit) {
// will overflow
return null;
}
}
if (negative) {
return Integer.valueOf(accumulator);
} else if (accumulator == Integer.MIN_VALUE) {
// overflow
return null;
} else {
return Integer.valueOf(-accumulator);
}
} else {
// non-digit encountered
return null;
}
}
}
}
private static int digit(char c) {
return c >= '0' && c <= '9' ? c - '0' : -1;
}
public static int[] toArray(Collection<Integer> ints) {
Objects.requireNonNull(ints);
return ints.stream().mapToInt(s -> s).toArray();

View File

@ -33,7 +33,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.FileSystemUtils;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.primitives.Integers;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.settings.IndexSettings;
import org.elasticsearch.index.shard.ShardId;
@ -693,12 +692,11 @@ public class NodeEnvironment extends AbstractComponent implements Closeable {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(indexPath)) {
String currentIndex = indexPath.getFileName().toString();
for (Path shardPath : stream) {
if (Files.isDirectory(shardPath)) {
Integer shardId = Integers.tryParse(shardPath.getFileName().toString());
if (shardId != null) {
ShardId id = new ShardId(currentIndex, shardId);
shardIds.add(id);
}
String fileName = shardPath.getFileName().toString();
if (Files.isDirectory(shardPath) && fileName.chars().allMatch(Character::isDigit)) {
int shardId = Integer.parseInt(fileName);
ShardId id = new ShardId(currentIndex, shardId);
shardIds.add(id);
}
}
}

View File

@ -1,53 +0,0 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common.util.primitives;
import org.elasticsearch.test.ESTestCase;
public class IntegerTests extends ESTestCase {
public void testTryParse() {
assertTryParse(0, "0");
assertTryParse(0, "-0");
assertTryParse(1, "1");
assertTryParse(-1, "-1");
assertTryParse(12345, "12345");
assertTryParse(-12345, "-12345");
for (int i = 0; i < 1 << 20; i++) {
int value = randomInt();
assertTryParse(value, Integer.toString(value));
}
assertTryParse(Integer.MAX_VALUE, Integer.toString(Integer.MAX_VALUE));
assertTryParse(Integer.MIN_VALUE, Integer.toString(Integer.MIN_VALUE));
assertNull(Integers.tryParse(null));
assertNull(Integers.tryParse(""));
assertNull(Integers.tryParse("-"));
assertNull(Integers.tryParse("9999999999999999"));
assertNull(Integers.tryParse(Long.toString(((long) Integer.MAX_VALUE) + 1)));
assertNull(Integers.tryParse(Long.toString(((long) Integer.MAX_VALUE) * 10)));
assertNull(Integers.tryParse(Long.toString(((long) Integer.MIN_VALUE) - 1)));
assertNull(Integers.tryParse(Long.toString(((long) Integer.MIN_VALUE) * 10)));
assertNull(Integers.tryParse(Long.toString(Long.MAX_VALUE)));
assertNull(Integers.tryParse(Long.toString(Long.MIN_VALUE)));
}
private static void assertTryParse(Integer expected, String value) {
assertEquals(expected, Integers.tryParse(value));
}
}