Sort files before importing them

This commit is contained in:
Brokkonaut
2018-09-05 03:05:41 +02:00
parent df8c8bcdda
commit c390504b70
2 changed files with 46 additions and 2 deletions

View File

@ -6,7 +6,10 @@ import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Comparator;
import java.util.logging.Level;
import java.util.regex.Pattern;
import static de.diddiz.util.Utils.newline;
@ -19,9 +22,10 @@ public class DumpedLogImporter implements Runnable {
@Override
public void run() {
final File[] imports = new File("plugins/LogBlock/import/").listFiles(new ExtensionFilenameFilter("sql"));
final File[] imports = new File(logblock.getDataFolder(), "import").listFiles(new ExtensionFilenameFilter("sql"));
if (imports != null && imports.length > 0) {
logblock.getLogger().info("Found " + imports.length + " imports.");
Arrays.sort(imports, new ImportsComparator());
Connection conn = null;
try {
conn = logblock.getConnection();
@ -77,4 +81,44 @@ public class DumpedLogImporter implements Runnable {
}
}
}
private static class ImportsComparator implements Comparator<File> {
private final Pattern splitPattern = Pattern.compile("[\\-\\.]");
@Override
public int compare(File o1, File o2) {
String[] name1 = splitPattern.split(o1.getName());
String[] name2 = splitPattern.split(o2.getName());
if (name1.length > name2.length) {
return 1;
} else if (name1.length < name2.length) {
return -1;
}
for (int i = 0; i < name1.length; i++) {
String part1 = name1[i];
String part2 = name2[i];
if (part1.length() > 0 && part2.length() > 0) {
char first1 = part1.charAt(0);
char first2 = part2.charAt(0);
if (first1 >= '0' && first1 <= '9' && first2 >= '0' && first2 <= '9') {
try {
long long1 = Long.parseLong(part1);
long long2 = Long.parseLong(part2);
if (long1 == long2) {
continue;
}
return long1 > long2 ? 1 : -1;
} catch (NumberFormatException e) {
// fallthrough to string compare
}
}
}
int compareString = part1.compareTo(part2);
if (compareString != 0) {
return compareString;
}
}
return 0;
}
}
}

View File

@ -197,7 +197,7 @@ public class Utils {
private final String ext;
public ExtensionFilenameFilter(String ext) {
this.ext = ext;
this.ext = "." + ext;
}
@Override