More work

This commit is contained in:
boy0001 2015-04-29 18:42:40 +10:00
parent 0da9db00d0
commit 8c57bc7445
2 changed files with 82 additions and 167 deletions

View File

@ -295,9 +295,9 @@ public class SQLManager implements AbstractDB {
} }
} }
public void createSettings(final ArrayList<Integer> mylist) { @Override
StmtMod<Integer> mod = new StmtMod<Integer>() { public void createSettings(final ArrayList<Integer> myList) {
final StmtMod<Integer> mod = new StmtMod<Integer>() {
@Override @Override
public String getCreateMySQL(int size) { public String getCreateMySQL(int size) {
return getCreateMySQL(size, CREATE_SETTINGS, 1); return getCreateMySQL(size, CREATE_SETTINGS, 1);
@ -305,75 +305,21 @@ public class SQLManager implements AbstractDB {
@Override @Override
public String getCreateSQLite(int size) { public String getCreateSQLite(int size) {
String query = "INSERT INTO `" + prefix + "plot_settings` SELECT ? AS `plot_plot_id`, ? AS `biome`, ? AS `rain`, ? AS `custom_time`, ? AS `time`, ? AS `deny_entry`, ? AS `alias`, ? AS `flags`, ? AS `merged`, ? AS `position` "; return getCreateSQLite(size, "INSERT INTO `" + prefix + "plot_settings` SELECT ? AS `plot_plot_id`, ? AS `biome`, ? AS `rain`, ? AS `custom_time`, ? AS `time`, ? AS `deny_entry`, ? AS `alias`, ? AS `flags`, ? AS `merged`, ? AS `position` ", 10);
int params = 10;
for (int i = 0; i < (ids.size() - 2); i++) {
unionstmt.append("UNION SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ");
}
unionstmt.append("UNION SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ");
} }
@Override @Override
public String getCreateSQL() { public String getCreateSQL() {
// TODO Auto-generated method stub return "INSERT INTO `" + SQLManager.this.prefix + "plot_settings`(`plot_plot_id`) VALUES(?)";
return null;
} }
@Override @Override
public void setMySQL(PreparedStatement stmt, int i, Integer obj) { public void setMySQL(PreparedStatement stmt, int i, Integer id) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setSQLite(PreparedStatement stmt, int i, Integer obj) {
// TODO Auto-generated method stub
}
@Override
public void setSQL(PreparedStatement stmt, Integer obj) {
// TODO Auto-generated method stub
}
};
String create1 = CREATE_SETTINGS;
final int size = mylist.size();
int packet;
if (PlotSquared.getMySQL() != null) {
packet = Math.min(size, 50000);
} else {
packet = Math.min(size, 50);
}
final int amount = size / packet;
for (int j = 0; j <= amount; j++) {
final List<Integer> ids = mylist.subList(j * packet, Math.min(size, (j + 1) * packet));
if (ids.size() == 0) {
return;
}
final StringBuilder statement = new StringBuilder(create1);
for (int i = 0; i < (ids.size() - 1); i++) {
statement.append("(?),");
}
statement.append("(?)");
PreparedStatement stmt = null;
try {
stmt = this.connection.prepareStatement(statement.toString());
for (int i = 0; i < ids.size(); i++) {
final Integer id = ids.get(i);
stmt.setInt((i * 1) + 1, id); stmt.setInt((i * 1) + 1, id);
} }
stmt.executeUpdate();
stmt.close(); @Override
} catch (final Exception e) { public void setSQLite(PreparedStatement stmt, int i, Integer id) throws SQLException {
try {
StringBuilder unionstmt = new StringBuilder("INSERT INTO `" + this.prefix + "plot_settings` SELECT ? AS `plot_plot_id`, ? AS `biome`, ? AS `rain`, ? AS `custom_time`, ? AS `time`, ? AS `deny_entry`, ? AS `alias`, ? AS `flags`, ? AS `merged`, ? AS `position` ");
for (int i = 0; i < (ids.size() - 2); i++) {
unionstmt.append("UNION SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ");
}
unionstmt.append("UNION SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ");
stmt = this.connection.prepareStatement(unionstmt.toString());
for (int i = 0; i < ids.size(); i++) {
Integer id = ids.get(i);
stmt.setInt((i * 10) + 1, id); stmt.setInt((i * 10) + 1, id);
stmt.setNull((i * 10) + 2, 4); stmt.setNull((i * 10) + 2, 4);
stmt.setNull((i * 10) + 3, 4); stmt.setNull((i * 10) + 3, 4);
@ -385,62 +331,45 @@ public class SQLManager implements AbstractDB {
stmt.setNull((i * 10) + 9, 4); stmt.setNull((i * 10) + 9, 4);
stmt.setString((i * 10) + 10, "DEFAULT"); stmt.setString((i * 10) + 10, "DEFAULT");
} }
stmt.executeUpdate();
stmt.close(); @Override
} public void setSQL(PreparedStatement stmt, Integer id) throws SQLException {
catch (Exception e2) {
e2.printStackTrace();
PlotSquared.log("&6[WARN] " + "Could not bulk save. Conversion may be slower...");
try {
for (final Integer id : ids) {
try {
stmt = SQLManager.this.connection.prepareStatement("INSERT INTO `" + SQLManager.this.prefix + "plot_settings`(`plot_plot_id`) VALUES(?)");
stmt.setInt(1, id); stmt.setInt(1, id);
stmt.executeUpdate();
stmt.close();
} catch (final Exception e3) {
PlotSquared.log("&c[ERROR] " + "Failed to save plot setting: " + id);
}
}
} catch (final Exception e4) {
e4.printStackTrace();
PlotSquared.log("&c[ERROR] " + "Failed to save plot settings!");
}
}
} }
};
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
setBulk(myList, mod);
} }
});
} }
/** /**
* Create a plot * Create a plot
* *
* @param mylist list of plots to be created * @param myList list of plots to be created
*/ */
@Override @Override
public void createPlots(final ArrayList<Plot> mylist) { public void createPlots(final ArrayList<Plot> myList) {
final int size = mylist.size(); final StmtMod<Plot> mod = new StmtMod<Plot>() {
int packet; @Override
if (PlotSquared.getMySQL() != null) { public String getCreateMySQL(int size) {
packet = Math.min(size, 50000); return getCreateMySQL(size, CREATE_PLOTS, 4);
} else {
packet = Math.min(size, 100);
} }
final int amount = size / packet;
for (int j = 0; j <= amount; j++) { @Override
final List<Plot> plots = mylist.subList(j * packet, Math.min(size, (j + 1) * packet)); public String getCreateSQLite(int size) {
if (plots.size() == 0) { return getCreateSQLite(size, "INSERT INTO `" + prefix + "plot` SELECT ? AS `id`, ? AS `plot_id_x`, ? AS `plot_id_z`, ? AS `owner`, ? AS `world`, ? AS `timestamp` ", 6);
return;
} }
final StringBuilder statement = new StringBuilder(this.CREATE_PLOTS);
for (int i = 0; i < (plots.size() - 1); i++) { @Override
statement.append("(?,?,?,?),"); public String getCreateSQL() {
return CREATE_PLOT;
} }
statement.append("(?,?,?,?)");
PreparedStatement stmt = null; @Override
try { public void setMySQL(PreparedStatement stmt, int i, Plot plot) throws SQLException {
stmt = this.connection.prepareStatement(statement.toString());
for (int i = 0; i < plots.size(); i++) {
final Plot plot = plots.get(i);
stmt.setInt((i * 4) + 1, plot.id.x); stmt.setInt((i * 4) + 1, plot.id.x);
stmt.setInt((i * 4) + 2, plot.id.y); stmt.setInt((i * 4) + 2, plot.id.y);
try { try {
@ -450,18 +379,9 @@ public class SQLManager implements AbstractDB {
} }
stmt.setString((i * 4) + 4, plot.world); stmt.setString((i * 4) + 4, plot.world);
} }
stmt.executeUpdate();
stmt.close(); @Override
} catch (final Exception e) { public void setSQLite(PreparedStatement stmt, int i, Plot plot) throws SQLException {
try {
StringBuilder unionstmt = new StringBuilder("INSERT INTO `" + this.prefix + "plot` SELECT ? AS `id`, ? AS `plot_id_x`, ? AS `plot_id_z`, ? AS `owner`, ? AS `world`, ? AS `timestamp` ");
for (int i = 0; i < (plots.size() - 2); i++) {
unionstmt.append("UNION SELECT ?, ?, ?, ?, ?, ? ");
}
unionstmt.append("UNION SELECT ?, ?, ?, ?, ?, ? ");
stmt = this.connection.prepareStatement(unionstmt.toString());
for (int i = 0; i < plots.size(); i++) {
final Plot plot = plots.get(i);
stmt.setNull((i * 6) + 1, 4); stmt.setNull((i * 6) + 1, 4);
stmt.setInt((i * 6) + 2, plot.id.x); stmt.setInt((i * 6) + 2, plot.id.x);
stmt.setInt((i * 6) + 3, plot.id.y); stmt.setInt((i * 6) + 3, plot.id.y);
@ -473,27 +393,21 @@ public class SQLManager implements AbstractDB {
stmt.setString((i * 6) + 5, plot.world); stmt.setString((i * 6) + 5, plot.world);
stmt.setTimestamp((i * 6) + 6, new Timestamp(System.currentTimeMillis())); stmt.setTimestamp((i * 6) + 6, new Timestamp(System.currentTimeMillis()));
} }
stmt.executeUpdate();
stmt.close(); @Override
} public void setSQL(PreparedStatement stmt, Plot plot) throws SQLException {
catch (Exception e2) { stmt.setInt(1, plot.id.x);
e2.printStackTrace(); stmt.setInt(2, plot.id.y);
PlotSquared.log("&6[WARN] " + "Could not bulk save. Conversion may be slower..."); stmt.setString(3, plot.owner.toString());
try { stmt.setString(4, plot.world);
for (final Plot plot : plots) {
try {
createPlot(plot);
} catch (final Exception e3) {
PlotSquared.log("&c[ERROR] " + "Failed to save plot: " + plot.id);
}
}
} catch (final Exception e4) {
e4.printStackTrace();
PlotSquared.log("&c[ERROR] " + "Failed to save plots!");
}
}
} }
};
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
setBulk(myList, mod);
} }
});
} }
/** /**

View File

@ -1,6 +1,7 @@
package com.intellectualcrafters.plot.database; package com.intellectualcrafters.plot.database;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -28,7 +29,7 @@ public abstract class StmtMod<T> {
public abstract String getCreateSQLite(int size); public abstract String getCreateSQLite(int size);
public abstract String getCreateSQL(); public abstract String getCreateSQL();
public abstract void setMySQL(PreparedStatement stmt, int i, T obj); public abstract void setMySQL(PreparedStatement stmt, int i, T obj) throws SQLException;
public abstract void setSQLite(PreparedStatement stmt, int i, T obj); public abstract void setSQLite(PreparedStatement stmt, int i, T obj) throws SQLException;
public abstract void setSQL(PreparedStatement stmt, T obj); public abstract void setSQL(PreparedStatement stmt, T obj) throws SQLException;
} }