Search in sources :

Example 1 with YieldIterable

use of com.fastasyncworldedit.core.util.collection.YieldIterable in project FastAsyncWorldEdit by IntellectualSites.

the class RollbackDatabase method getEdits.

public Iterable<Supplier<RollbackOptimizedHistory>> getEdits(UUID uuid, long minTime, BlockVector3 pos1, BlockVector3 pos2, boolean delete, boolean ascending) {
    YieldIterable<Supplier<RollbackOptimizedHistory>> yieldIterable = new YieldIterable<>();
    Future<Integer> future = call(() -> {
        try {
            int count = 0;
            String stmtStr = ascending ? uuid == null ? "SELECT * FROM`" + this.prefix + "edits` WHERE `time`>? AND `x2`>=? AND" + " `x1`<=? AND `z2`>=? AND `z1`<=? AND `y2`>=? AND `y1`<=? ORDER BY `time` , `id`" : "SELECT * FROM`" + this.prefix + "edits` WHERE `time`>? AND" + " `x2`>=? AND `x1`<=? AND `z2`>=? AND `z1`<=? AND `y2`>=? AND `y1`<=? AND `player`=? ORDER BY `time` ASC, `id` ASC" : uuid == null ? "SELECT * FROM`" + this.prefix + "edits` WHERE `time`>? AND `x2`>=? AND `x1`<=? AND `z2`>=? " + "AND `z1`<=? AND `y2`>=? AND `y1`<=? ORDER BY `time` DESC, `id` DESC" : "SELECT * FROM`" + this.prefix + "edits` WHERE `time`>? AND `x2`>=? AND `x1`<=? AND" + " `z2`>=? AND `z1`<=? AND `y2`>=? AND `y1`<=? AND `player`=? ORDER BY `time` DESC, `id` DESC";
            try (PreparedStatement stmt = connection.prepareStatement(stmtStr)) {
                stmt.setInt(1, (int) (minTime / 1000));
                stmt.setInt(2, pos1.getBlockX());
                stmt.setInt(3, pos2.getBlockX());
                stmt.setInt(4, pos1.getBlockZ());
                stmt.setInt(5, pos2.getBlockZ());
                stmt.setByte(6, (byte) (pos1.getBlockY() - 128));
                stmt.setByte(7, (byte) (pos2.getBlockY() - 128));
                if (uuid != null) {
                    byte[] uuidBytes = toBytes(uuid);
                    stmt.setBytes(8, uuidBytes);
                }
                ResultSet result = stmt.executeQuery();
                if (!result.next()) {
                    return 0;
                }
                do {
                    count++;
                    Supplier<RollbackOptimizedHistory> history = create(result);
                    yieldIterable.accept(history);
                } while (result.next());
            }
            if (delete && uuid != null) {
                try (PreparedStatement stmt = connection.prepareStatement("DELETE FROM`" + this.prefix + "edits` WHERE `player`=? AND `time`>? AND `x2`>=? AND `x1`<=? AND `y2`>=? AND `y1`<=? AND `z2`>=? AND `z1`<=?")) {
                    stmt.setInt(1, (int) (minTime / 1000));
                    stmt.setInt(2, pos1.getBlockX());
                    stmt.setInt(3, pos2.getBlockX());
                    stmt.setInt(4, pos1.getBlockZ());
                    stmt.setInt(5, pos2.getBlockZ());
                    stmt.setByte(6, (byte) (pos1.getBlockY() - 128));
                    stmt.setByte(7, (byte) (pos2.getBlockY() - 128));
                    byte[] uuidBytes = ByteBuffer.allocate(16).putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits()).array();
                    stmt.setBytes(8, uuidBytes);
                }
            }
            return count;
        } finally {
            yieldIterable.close();
        }
    });
    yieldIterable.setFuture(future);
    return yieldIterable;
}
Also used : ResultSet(java.sql.ResultSet) Supplier(java.util.function.Supplier) PreparedStatement(java.sql.PreparedStatement) YieldIterable(com.fastasyncworldedit.core.util.collection.YieldIterable) RollbackOptimizedHistory(com.fastasyncworldedit.core.history.RollbackOptimizedHistory)

Aggregations

RollbackOptimizedHistory (com.fastasyncworldedit.core.history.RollbackOptimizedHistory)1 YieldIterable (com.fastasyncworldedit.core.util.collection.YieldIterable)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Supplier (java.util.function.Supplier)1