Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try with resources clean up #417

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 64 additions & 57 deletions src/main/java/net/querz/mcaselector/io/db/CacheDBController.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,23 @@ public void switchTo(String dbPath, List<Overlay> overlays) throws SQLException
}

public void initTables(List<Overlay> overlays) throws SQLException {
Statement statement = connection.createStatement();
for (Overlay parser : overlays) {
statement.executeUpdate(String.format(
"CREATE TABLE IF NOT EXISTS %s%s (" +
"p BIGINT PRIMARY KEY, " +
"d BLOB);", parser.name(), parser.getMultiValuesID()));
}
try (Statement statement = connection.createStatement();) {
for (Overlay parser : overlays) {
statement.executeUpdate(String.format(
"CREATE TABLE IF NOT EXISTS %s%s (" +
"p BIGINT PRIMARY KEY, " +
"d BLOB);", parser.name(), parser.getMultiValuesID()));
}

statement.executeUpdate("CREATE TABLE IF NOT EXISTS file_times (" +
"p BIGINT PRIMARY KEY, " +
"t BIGINT);");
statement.executeUpdate("CREATE TABLE IF NOT EXISTS file_times (" +
"p BIGINT PRIMARY KEY, " +
"t BIGINT);");

allTables = new ArrayList<>();
ResultSet result = statement.executeQuery("SELECT name FROM sqlite_master WHERE type='table';");
while (result.next()) {
allTables.add(result.getString(1));
allTables = new ArrayList<>();
ResultSet result = statement.executeQuery("SELECT name FROM sqlite_master WHERE type='table';");
while (result.next()) {
allTables.add(result.getString(1));
}
}
}

Expand Down Expand Up @@ -149,71 +150,76 @@ public long getFileTime(Point2i region) throws SQLException {
while (connection == null) {
Thread.onSpinWait();
}
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(String.format("SELECT t FROM file_times WHERE p=%s;", region.asLong()));
if (!result.next()) {
return -1;
try (Statement statement = connection.createStatement();) {
ResultSet result = statement.executeQuery(String.format("SELECT t FROM file_times WHERE p=%s;", region.asLong()));
if (!result.next()) {
return -1;
}
return result.getLong(1);
}
return result.getLong(1);
}

public void setFileTime(Point2i region, long time) throws SQLException {
while (connection == null) {
Thread.onSpinWait();
}
PreparedStatement ps = connection.prepareStatement(
try (PreparedStatement ps = connection.prepareStatement(
"INSERT INTO file_times (p, t) " +
"VALUES (?, ?) " +
"ON CONFLICT(p) DO UPDATE " +
"SET t=?;");
ps.setLong(1, region.asLong());
ps.setLong(2, time);
ps.setLong(3, time);
ps.addBatch();
ps.executeBatch();
"SET t=?;");) {
ps.setLong(1, region.asLong());
ps.setLong(2, time);
ps.setLong(3, time);
ps.addBatch();
ps.executeBatch();
}
}

public int[] getData(Overlay parser, Point2i region) throws IOException, SQLException {
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(String.format(
"SELECT d FROM %s%s WHERE p=%s;", parser.name(), parser.getMultiValuesID(), region.asLong()));
if (!result.next()) {
return null;
}
int[] data = new int[1024];
try (DataInputStream dis = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(result.getBytes(1))))) {
for (int i = 0; i < 1024; i++) {
data[i] = dis.readInt();
try (Statement statement = connection.createStatement();) {
ResultSet result = statement.executeQuery(String.format(
"SELECT d FROM %s%s WHERE p=%s;", parser.name(), parser.getMultiValuesID(), region.asLong()));
if (!result.next()) {
return null;
}
int[] data = new int[1024];
try (DataInputStream dis = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(result.getBytes(1))))) {
for (int i = 0; i < 1024; i++) {
data[i] = dis.readInt();
}
}
return data;
}
return data;
}

public void setData(Overlay parser, Point2i region, int[] data) throws IOException, SQLException {
PreparedStatement ps = connection.prepareStatement(String.format(
try (PreparedStatement ps = connection.prepareStatement(String.format(
"INSERT INTO %s%s (p, d) " +
"VALUES (?, ?) " +
"ON CONFLICT(p) DO UPDATE " +
"SET d=?;", parser.name(), parser.getMultiValuesID()));
ps.setLong(1, region.asLong());
ByteArrayOutputStream baos;
try (DataOutputStream dos = new DataOutputStream(new GZIPOutputStream(baos = new ByteArrayOutputStream()))) {
for (int i = 0; i < 1024; i++) {
dos.writeInt(data[i]);
"SET d=?;", parser.name(), parser.getMultiValuesID()));) {
ps.setLong(1, region.asLong());
ByteArrayOutputStream baos;
try (DataOutputStream dos = new DataOutputStream(new GZIPOutputStream(baos = new ByteArrayOutputStream()))) {
for (int i = 0; i < 1024; i++) {
dos.writeInt(data[i]);
}
}
byte[] gzipped = baos.toByteArray();
ps.setBytes(2, gzipped);
ps.setBytes(3, gzipped);
ps.addBatch();
ps.executeBatch();
}
byte[] gzipped = baos.toByteArray();
ps.setBytes(2, gzipped);
ps.setBytes(3, gzipped);
ps.addBatch();
ps.executeBatch();
}

public void deleteData(Overlay parser, Point2i region) throws SQLException {
PreparedStatement ps = connection.prepareStatement(String.format(
"DELETE FROM %s%s WHERE p=?;", parser.name(), parser.getMultiValuesID()));
ps.setLong(1, region.asLong());
ps.execute();
try (PreparedStatement ps = connection.prepareStatement(String.format(
"DELETE FROM %s%s WHERE p=?;", parser.name(), parser.getMultiValuesID()));) {
ps.setLong(1, region.asLong());
ps.execute();
}
}

public void deleteData(Point2i region) throws SQLException {
Expand All @@ -222,10 +228,11 @@ public void deleteData(Point2i region) throws SQLException {
return;
}
for (String table : allTables) {
PreparedStatement ps = connection.prepareStatement(String.format(
"DELETE FROM %s WHERE p=?;", table));
ps.setLong(1, region.asLong());
ps.execute();
try (PreparedStatement ps = connection.prepareStatement(String.format(
"DELETE FROM %s WHERE p=?;", table));) {
ps.setLong(1, region.asLong());
ps.execute();
}
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/main/java/net/querz/mcaselector/io/mca/MCAFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,15 @@ public void deFragment(File dest) throws IOException {
source.seek(offsets[i] * 4096L);
rafTmp.seek(globalOffset * 4096L);

DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(source.getFD()), sectors * 4096));
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(rafTmp.getFD()), sectors * 4096));

byte[] data = new byte[sectors * 4096];
dis.read(data);
dos.write(data);
offsets[i] = globalOffset; // always keep MCAFile information up to date
globalOffset += sectors;
try (DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(source.getFD()), sectors * 4096));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This closes the RandomAccessFile's FileDescriptor as well, which is reused in this loop. Closing it prematurely will throw an IOException in the next iteration.

DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(rafTmp.getFD()), sectors * 4096));) {

byte[] data = new byte[sectors * 4096];
dis.read(data);
dos.write(data);
offsets[i] = globalOffset; // always keep MCAFile information up to date
globalOffset += sectors;
}
}
}

Expand Down