forked from LogBlock/LogBlock
Sort files before importing them
This commit is contained in:
@ -6,7 +6,10 @@ import java.io.*;
|
|||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static de.diddiz.util.Utils.newline;
|
import static de.diddiz.util.Utils.newline;
|
||||||
|
|
||||||
@ -19,9 +22,10 @@ public class DumpedLogImporter implements Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
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) {
|
if (imports != null && imports.length > 0) {
|
||||||
logblock.getLogger().info("Found " + imports.length + " imports.");
|
logblock.getLogger().info("Found " + imports.length + " imports.");
|
||||||
|
Arrays.sort(imports, new ImportsComparator());
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
try {
|
try {
|
||||||
conn = logblock.getConnection();
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,7 +197,7 @@ public class Utils {
|
|||||||
private final String ext;
|
private final String ext;
|
||||||
|
|
||||||
public ExtensionFilenameFilter(String ext) {
|
public ExtensionFilenameFilter(String ext) {
|
||||||
this.ext = ext;
|
this.ext = "." + ext;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Reference in New Issue
Block a user