|
| 1 | +package com.infernalsuite.asp.loaders.postgresql; |
| 2 | + |
| 3 | + |
| 4 | +import com.infernalsuite.asp.api.exceptions.UnknownWorldException; |
| 5 | +import com.infernalsuite.asp.api.loaders.UpdatableLoader; |
| 6 | +import com.zaxxer.hikari.HikariConfig; |
| 7 | +import com.zaxxer.hikari.HikariDataSource; |
| 8 | +import org.jetbrains.annotations.ApiStatus; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.sql.Connection; |
| 14 | +import java.sql.PreparedStatement; |
| 15 | +import java.sql.ResultSet; |
| 16 | +import java.sql.SQLException; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +public class PostgresqlLoader extends UpdatableLoader { |
| 21 | + |
| 22 | + private static final Logger LOGGER = LoggerFactory.getLogger(PostgresqlLoader.class); |
| 23 | + private static final int CURRENT_DB_VERSION = 1; |
| 24 | + |
| 25 | + // Database version handling queries |
| 26 | + private static final String CREATE_VERSIONING_TABLE_QUERY = |
| 27 | + "CREATE TABLE IF NOT EXISTS database_version (" + |
| 28 | + "id SERIAL PRIMARY KEY, " + |
| 29 | + "version INT" + |
| 30 | + ");"; |
| 31 | + |
| 32 | + private static final String INSERT_VERSION_QUERY = |
| 33 | + "INSERT INTO database_version (id, version) " + |
| 34 | + "VALUES (1, ?) " + |
| 35 | + "ON CONFLICT (id) DO UPDATE SET version = EXCLUDED.version;"; |
| 36 | + |
| 37 | + private static final String GET_VERSION_QUERY = |
| 38 | + "SELECT version FROM database_version WHERE id = 1;"; |
| 39 | + |
| 40 | + // World handling queries |
| 41 | + private static final String CREATE_WORLDS_TABLE_QUERY = |
| 42 | + "CREATE TABLE IF NOT EXISTS worlds (" + |
| 43 | + "id SERIAL PRIMARY KEY, " + |
| 44 | + "name VARCHAR(255) UNIQUE, " + |
| 45 | + "locked BIGINT DEFAULT 0 NOT NULL, " + |
| 46 | + "world BYTEA" + |
| 47 | + ");"; |
| 48 | + |
| 49 | + private static final String SELECT_WORLD_QUERY = |
| 50 | + "SELECT world FROM worlds WHERE name = ?;"; |
| 51 | + |
| 52 | + private static final String UPDATE_WORLD_QUERY = |
| 53 | + "INSERT INTO worlds (name, world) VALUES (?, ?) " + |
| 54 | + "ON CONFLICT (name) DO UPDATE SET world = EXCLUDED.world;"; |
| 55 | + |
| 56 | + private static final String DELETE_WORLD_QUERY = |
| 57 | + "DELETE FROM worlds WHERE name = ?;"; |
| 58 | + |
| 59 | + private static final String LIST_WORLDS_QUERY = |
| 60 | + "SELECT name FROM worlds;"; |
| 61 | + |
| 62 | + private final HikariDataSource source; |
| 63 | + |
| 64 | + public PostgresqlLoader(String sqlURL, String host, int port, String database, boolean useSSL, String username, String password) throws SQLException { |
| 65 | + HikariConfig hikariConfig = new HikariConfig(); |
| 66 | + |
| 67 | + sqlURL = sqlURL.replace("{host}", host) |
| 68 | + .replace("{port}", String.valueOf(port)) |
| 69 | + .replace("{database}", database) |
| 70 | + .replace("{usessl}", String.valueOf(useSSL)); |
| 71 | + |
| 72 | + hikariConfig.setJdbcUrl(sqlURL); |
| 73 | + hikariConfig.setUsername(username); |
| 74 | + hikariConfig.setPassword(password); |
| 75 | + hikariConfig.setDriverClassName("org.postgresql.Driver"); |
| 76 | + |
| 77 | + hikariConfig.addDataSourceProperty("cachePrepStmts", "true"); |
| 78 | + hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250"); |
| 79 | + hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); |
| 80 | + |
| 81 | + source = new HikariDataSource(hikariConfig); |
| 82 | + init(); |
| 83 | + } |
| 84 | + |
| 85 | + @ApiStatus.Experimental |
| 86 | + public PostgresqlLoader(HikariDataSource hikariDataSource) throws SQLException { |
| 87 | + source = hikariDataSource; |
| 88 | + init(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void update() throws IOException, NewerStorageException { |
| 93 | + try (Connection con = source.getConnection()) { |
| 94 | + int version; |
| 95 | + try (PreparedStatement statement = con.prepareStatement(GET_VERSION_QUERY); |
| 96 | + ResultSet set = statement.executeQuery()) { |
| 97 | + version = set.next() ? set.getInt(1) : -1; |
| 98 | + } |
| 99 | + |
| 100 | + if (version > CURRENT_DB_VERSION) { |
| 101 | + throw new NewerStorageException(CURRENT_DB_VERSION, version); |
| 102 | + } |
| 103 | + |
| 104 | + if (version < CURRENT_DB_VERSION) { |
| 105 | + LOGGER.warn("Your PostgreSQL database is outdated. Updating now..."); |
| 106 | + try { |
| 107 | + Thread.sleep(5000L); |
| 108 | + } catch (InterruptedException ignored) { |
| 109 | + } |
| 110 | + |
| 111 | + // Insert/update database version |
| 112 | + try (PreparedStatement statement = con.prepareStatement(INSERT_VERSION_QUERY)) { |
| 113 | + statement.setInt(1, CURRENT_DB_VERSION); |
| 114 | + statement.executeUpdate(); |
| 115 | + } |
| 116 | + } |
| 117 | + } catch (SQLException ex) { |
| 118 | + throw new IOException(ex); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public byte[] readWorld(String worldName) throws UnknownWorldException, IOException { |
| 124 | + try (Connection con = source.getConnection(); |
| 125 | + PreparedStatement statement = con.prepareStatement(SELECT_WORLD_QUERY)) { |
| 126 | + statement.setString(1, worldName); |
| 127 | + ResultSet set = statement.executeQuery(); |
| 128 | + |
| 129 | + if (!set.next()) { |
| 130 | + throw new UnknownWorldException(worldName); |
| 131 | + } |
| 132 | + |
| 133 | + return set.getBytes("world"); |
| 134 | + } catch (SQLException ex) { |
| 135 | + throw new IOException(ex); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public boolean worldExists(String worldName) throws IOException { |
| 141 | + try (Connection con = source.getConnection(); |
| 142 | + PreparedStatement statement = con.prepareStatement(SELECT_WORLD_QUERY)) { |
| 143 | + statement.setString(1, worldName); |
| 144 | + ResultSet set = statement.executeQuery(); |
| 145 | + return set.next(); |
| 146 | + } catch (SQLException ex) { |
| 147 | + throw new IOException(ex); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public List<String> listWorlds() throws IOException { |
| 153 | + List<String> worldList = new ArrayList<>(); |
| 154 | + try (Connection con = source.getConnection(); |
| 155 | + PreparedStatement statement = con.prepareStatement(LIST_WORLDS_QUERY)) { |
| 156 | + ResultSet set = statement.executeQuery(); |
| 157 | + while (set.next()) { |
| 158 | + worldList.add(set.getString("name")); |
| 159 | + } |
| 160 | + } catch (SQLException ex) { |
| 161 | + throw new IOException(ex); |
| 162 | + } |
| 163 | + return worldList; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void saveWorld(String worldName, byte[] serializedWorld) throws IOException { |
| 168 | + try (Connection con = source.getConnection(); |
| 169 | + PreparedStatement statement = con.prepareStatement(UPDATE_WORLD_QUERY)) { |
| 170 | + statement.setString(1, worldName); |
| 171 | + statement.setBytes(2, serializedWorld); |
| 172 | + statement.executeUpdate(); |
| 173 | + } catch (SQLException ex) { |
| 174 | + throw new IOException(ex); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public void deleteWorld(String worldName) throws IOException, UnknownWorldException { |
| 180 | + try (Connection con = source.getConnection(); |
| 181 | + PreparedStatement statement = con.prepareStatement(DELETE_WORLD_QUERY)) { |
| 182 | + statement.setString(1, worldName); |
| 183 | + if (statement.executeUpdate() == 0) { |
| 184 | + throw new UnknownWorldException(worldName); |
| 185 | + } |
| 186 | + } catch (SQLException ex) { |
| 187 | + throw new IOException(ex); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + private void init() throws SQLException { |
| 192 | + try (Connection con = source.getConnection()) { |
| 193 | + try (PreparedStatement statement = con.prepareStatement(CREATE_WORLDS_TABLE_QUERY)) { |
| 194 | + statement.execute(); |
| 195 | + } |
| 196 | + try (PreparedStatement statement = con.prepareStatement(CREATE_VERSIONING_TABLE_QUERY)) { |
| 197 | + statement.execute(); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments