use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.
the class SQLRegionDatabase method saveChanges.
@Override
public void saveChanges(RegionDifference difference) throws DifferenceSaveException, StorageException {
checkNotNull(difference);
initialize();
Closer closer = Closer.create();
DataUpdater updater;
try {
try {
updater = new DataUpdater(this, closer.register(getConnection()));
} catch (SQLException e) {
throw new StorageException("Failed to get a connection to the database", e);
}
try {
updater.saveChanges(difference.getChanged(), difference.getRemoved());
} catch (SQLException e) {
throw new StorageException("Failed to save the region data to the database", e);
}
} finally {
closer.closeQuietly();
}
}
use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.
the class TableCache method fetch.
/**
* Fetch from the database rows that match the given entries, otherwise
* create new entries and assign them an ID.
*
* @param entries a list of entries
* @throws SQLException thrown on SQL error
*/
public void fetch(Collection<V> entries) throws SQLException {
synchronized (LOCK) {
// Lock across all cache instances
checkNotNull(entries);
// Get a list of missing entries
List<V> fetchList = new ArrayList<>();
for (V entry : entries) {
if (!cache.containsKey(toKey(entry))) {
fetchList.add(entry);
}
}
if (fetchList.isEmpty()) {
// Nothing to do
return;
}
// Search for entries
for (List<V> partition : Lists.partition(fetchList, MAX_NUMBER_PER_QUERY)) {
Closer closer = Closer.create();
try {
PreparedStatement statement = closer.register(conn.prepareStatement(String.format("SELECT id, " + fieldName + " " + "FROM `" + config.getTablePrefix() + tableName + "` " + "WHERE " + fieldName + " IN (%s)", StatementUtils.preparePlaceHolders(partition.size()))));
String[] values = new String[partition.size()];
int i = 0;
for (V entry : partition) {
values[i] = fromType(entry);
i++;
}
StatementUtils.setValues(statement, values);
ResultSet results = closer.register(statement.executeQuery());
while (results.next()) {
cache.put(toKey(toType(results.getString(fieldName))), results.getInt("id"));
}
} finally {
closer.closeQuietly();
}
}
List<V> missing = new ArrayList<>();
for (V entry : fetchList) {
if (!cache.containsKey(toKey(entry))) {
missing.add(entry);
}
}
// Insert entries that are missing
if (!missing.isEmpty()) {
Closer closer = Closer.create();
try {
PreparedStatement statement = closer.register(conn.prepareStatement("INSERT INTO `" + config.getTablePrefix() + tableName + "` (id, " + fieldName + ") VALUES (null, ?)", Statement.RETURN_GENERATED_KEYS));
for (V entry : missing) {
statement.setString(1, fromType(entry));
statement.execute();
ResultSet generatedKeys = statement.getGeneratedKeys();
if (generatedKeys.next()) {
cache.put(toKey(entry), generatedKeys.getInt(1));
} else {
log.warning("Could not get the database ID for entry " + entry);
}
}
} finally {
closer.closeQuietly();
}
}
}
}
use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.
the class DataLoader method loadCuboids.
private void loadCuboids() throws SQLException {
Closer closer = Closer.create();
try {
PreparedStatement stmt = closer.register(conn.prepareStatement("SELECT g.min_z, g.min_y, g.min_x, " + " g.max_z, g.max_y, g.max_x, " + " r.id, r.priority, p.id AS parent " + "FROM " + config.getTablePrefix() + "region_cuboid AS g " + "LEFT JOIN " + config.getTablePrefix() + "region AS r " + " ON (g.region_id = r.id AND g.world_id = r.world_id) " + "LEFT JOIN " + config.getTablePrefix() + "region AS p " + " ON (r.parent = p.id AND r.world_id = p.world_id) " + "WHERE r.world_id = " + worldId));
ResultSet rs = closer.register(stmt.executeQuery());
while (rs.next()) {
BlockVector3 pt1 = BlockVector3.at(rs.getInt("min_x"), rs.getInt("min_y"), rs.getInt("min_z"));
BlockVector3 pt2 = BlockVector3.at(rs.getInt("max_x"), rs.getInt("max_y"), rs.getInt("max_z"));
BlockVector3 min = pt1.getMinimum(pt2);
BlockVector3 max = pt1.getMaximum(pt2);
ProtectedRegion region = new ProtectedCuboidRegion(rs.getString("id"), min, max);
region.setPriority(rs.getInt("priority"));
loaded.put(rs.getString("id"), region);
String parentId = rs.getString("parent");
if (parentId != null) {
parentSets.put(region, parentId);
}
}
} finally {
closer.closeQuietly();
}
}
use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.
the class DataLoader method loadPolygons.
private void loadPolygons() throws SQLException {
ListMultimap<String, BlockVector2> pointsCache = ArrayListMultimap.create();
// First get all the vertices and store them in memory
Closer closer = Closer.create();
try {
PreparedStatement stmt = closer.register(conn.prepareStatement("SELECT region_id, x, z " + "FROM " + config.getTablePrefix() + "region_poly2d_point " + "WHERE world_id = " + worldId));
ResultSet rs = closer.register(stmt.executeQuery());
while (rs.next()) {
pointsCache.put(rs.getString("region_id"), BlockVector2.at(rs.getInt("x"), rs.getInt("z")));
}
} finally {
closer.closeQuietly();
}
// Now we pull the regions themselves
closer = Closer.create();
try {
PreparedStatement stmt = closer.register(conn.prepareStatement("SELECT g.min_y, g.max_y, r.id, r.priority, p.id AS parent " + "FROM " + config.getTablePrefix() + "region_poly2d AS g " + "LEFT JOIN " + config.getTablePrefix() + "region AS r " + " ON (g.region_id = r.id AND g.world_id = r.world_id) " + "LEFT JOIN " + config.getTablePrefix() + "region AS p " + " ON (r.parent = p.id AND r.world_id = p.world_id) " + "WHERE r.world_id = " + worldId));
ResultSet rs = closer.register(stmt.executeQuery());
while (rs.next()) {
String id = rs.getString("id");
// Get the points from the cache
List<BlockVector2> points = pointsCache.get(id);
if (points.size() < 3) {
log.log(Level.WARNING, "Invalid polygonal region '" + id + "': region has " + points.size() + " point(s) (less than the required 3). Skipping this region.");
continue;
}
Integer minY = rs.getInt("min_y");
Integer maxY = rs.getInt("max_y");
ProtectedRegion region = new ProtectedPolygonalRegion(id, points, minY, maxY);
region.setPriority(rs.getInt("priority"));
loaded.put(id, region);
String parentId = rs.getString("parent");
if (parentId != null) {
parentSets.put(region, parentId);
}
}
} finally {
closer.closeQuietly();
}
}
use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.
the class DataUpdater method getExistingRegions.
private Map<String, String> getExistingRegions() throws SQLException {
Map<String, String> existing = new HashMap<>();
Closer closer = Closer.create();
try {
PreparedStatement stmt = closer.register(conn.prepareStatement("SELECT id, type " + "FROM " + config.getTablePrefix() + "region " + "WHERE world_id = " + worldId));
ResultSet resultSet = closer.register(stmt.executeQuery());
while (resultSet.next()) {
existing.put(resultSet.getString("id"), resultSet.getString("type"));
}
return existing;
} finally {
closer.closeQuietly();
}
}
Aggregations