-
Notifications
You must be signed in to change notification settings - Fork 116
Add loader for PostgresSQL #186
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
Open
sadcenter
wants to merge
1
commit into
InfernalSuite:main
Choose a base branch
from
sadcenter:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| plugins { | ||
| id("asp.base-conventions") | ||
| id("asp.publishing-conventions") | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly(project(":api")) | ||
|
|
||
| api(libs.hikari) | ||
| api("org.postgresql:postgresql:42.7.8") | ||
| } | ||
|
|
||
| publishConfiguration { | ||
| name = "Advanced Slime Paper PostgresSQL Loader" | ||
| description = "PostgresSQL loader for Advanced Slime Paper" | ||
sadcenter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
201 changes: 201 additions & 0 deletions
201
...resql-loader/src/main/java/com/infernalsuite/asp/loaders/postgresql/PostgresqlLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| package com.infernalsuite.asp.loaders.postgresql; | ||
|
|
||
|
|
||
| import com.infernalsuite.asp.api.exceptions.UnknownWorldException; | ||
| import com.infernalsuite.asp.api.loaders.UpdatableLoader; | ||
| import com.zaxxer.hikari.HikariConfig; | ||
| import com.zaxxer.hikari.HikariDataSource; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.IOException; | ||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class PostgresqlLoader extends UpdatableLoader { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(PostgresqlLoader.class); | ||
| private static final int CURRENT_DB_VERSION = 1; | ||
|
|
||
| // Database version handling queries | ||
| private static final String CREATE_VERSIONING_TABLE_QUERY = | ||
| "CREATE TABLE IF NOT EXISTS database_version (" + | ||
| "id SERIAL PRIMARY KEY, " + | ||
| "version INT" + | ||
| ");"; | ||
|
|
||
| private static final String INSERT_VERSION_QUERY = | ||
| "INSERT INTO database_version (id, version) " + | ||
| "VALUES (1, ?) " + | ||
| "ON CONFLICT (id) DO UPDATE SET version = EXCLUDED.version;"; | ||
|
|
||
| private static final String GET_VERSION_QUERY = | ||
| "SELECT version FROM database_version WHERE id = 1;"; | ||
|
|
||
| // World handling queries | ||
| private static final String CREATE_WORLDS_TABLE_QUERY = | ||
| "CREATE TABLE IF NOT EXISTS worlds (" + | ||
| "id SERIAL PRIMARY KEY, " + | ||
| "name VARCHAR(255) UNIQUE, " + | ||
| "locked BIGINT DEFAULT 0 NOT NULL, " + | ||
| "world BYTEA" + | ||
| ");"; | ||
|
|
||
| private static final String SELECT_WORLD_QUERY = | ||
| "SELECT world FROM worlds WHERE name = ?;"; | ||
|
|
||
| private static final String UPDATE_WORLD_QUERY = | ||
| "INSERT INTO worlds (name, world) VALUES (?, ?) " + | ||
| "ON CONFLICT (name) DO UPDATE SET world = EXCLUDED.world;"; | ||
|
|
||
| private static final String DELETE_WORLD_QUERY = | ||
| "DELETE FROM worlds WHERE name = ?;"; | ||
|
|
||
| private static final String LIST_WORLDS_QUERY = | ||
| "SELECT name FROM worlds;"; | ||
|
|
||
| private final HikariDataSource source; | ||
|
|
||
| public PostgresqlLoader(String sqlURL, String host, int port, String database, boolean useSSL, String username, String password) throws SQLException { | ||
| HikariConfig hikariConfig = new HikariConfig(); | ||
|
|
||
| sqlURL = sqlURL.replace("{host}", host) | ||
| .replace("{port}", String.valueOf(port)) | ||
| .replace("{database}", database) | ||
| .replace("{usessl}", String.valueOf(useSSL)); | ||
|
|
||
| hikariConfig.setJdbcUrl(sqlURL); | ||
| hikariConfig.setUsername(username); | ||
| hikariConfig.setPassword(password); | ||
| hikariConfig.setDriverClassName("org.postgresql.Driver"); | ||
|
|
||
| hikariConfig.addDataSourceProperty("cachePrepStmts", "true"); | ||
| hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250"); | ||
| hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); | ||
sadcenter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| source = new HikariDataSource(hikariConfig); | ||
| init(); | ||
| } | ||
|
|
||
| @ApiStatus.Experimental | ||
| public PostgresqlLoader(HikariDataSource hikariDataSource) throws SQLException { | ||
| source = hikariDataSource; | ||
| init(); | ||
| } | ||
|
|
||
| @Override | ||
| public void update() throws IOException, NewerStorageException { | ||
| try (Connection con = source.getConnection()) { | ||
| int version; | ||
| try (PreparedStatement statement = con.prepareStatement(GET_VERSION_QUERY); | ||
| ResultSet set = statement.executeQuery()) { | ||
| version = set.next() ? set.getInt(1) : -1; | ||
| } | ||
|
|
||
| if (version > CURRENT_DB_VERSION) { | ||
| throw new NewerStorageException(CURRENT_DB_VERSION, version); | ||
| } | ||
|
|
||
| if (version < CURRENT_DB_VERSION) { | ||
| LOGGER.warn("Your PostgreSQL database is outdated. Updating now..."); | ||
| try { | ||
| Thread.sleep(5000L); | ||
| } catch (InterruptedException ignored) { | ||
| } | ||
|
|
||
| // Insert/update database version | ||
| try (PreparedStatement statement = con.prepareStatement(INSERT_VERSION_QUERY)) { | ||
| statement.setInt(1, CURRENT_DB_VERSION); | ||
| statement.executeUpdate(); | ||
| } | ||
| } | ||
| } catch (SQLException ex) { | ||
| throw new IOException(ex); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] readWorld(String worldName) throws UnknownWorldException, IOException { | ||
| try (Connection con = source.getConnection(); | ||
| PreparedStatement statement = con.prepareStatement(SELECT_WORLD_QUERY)) { | ||
| statement.setString(1, worldName); | ||
| ResultSet set = statement.executeQuery(); | ||
sadcenter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!set.next()) { | ||
| throw new UnknownWorldException(worldName); | ||
| } | ||
|
|
||
| return set.getBytes("world"); | ||
| } catch (SQLException ex) { | ||
| throw new IOException(ex); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean worldExists(String worldName) throws IOException { | ||
| try (Connection con = source.getConnection(); | ||
| PreparedStatement statement = con.prepareStatement(SELECT_WORLD_QUERY)) { | ||
| statement.setString(1, worldName); | ||
| ResultSet set = statement.executeQuery(); | ||
| return set.next(); | ||
sadcenter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (SQLException ex) { | ||
| throw new IOException(ex); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> listWorlds() throws IOException { | ||
| List<String> worldList = new ArrayList<>(); | ||
| try (Connection con = source.getConnection(); | ||
| PreparedStatement statement = con.prepareStatement(LIST_WORLDS_QUERY)) { | ||
| ResultSet set = statement.executeQuery(); | ||
| while (set.next()) { | ||
| worldList.add(set.getString("name")); | ||
sadcenter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } catch (SQLException ex) { | ||
| throw new IOException(ex); | ||
| } | ||
| return worldList; | ||
| } | ||
|
|
||
| @Override | ||
| public void saveWorld(String worldName, byte[] serializedWorld) throws IOException { | ||
| try (Connection con = source.getConnection(); | ||
| PreparedStatement statement = con.prepareStatement(UPDATE_WORLD_QUERY)) { | ||
| statement.setString(1, worldName); | ||
| statement.setBytes(2, serializedWorld); | ||
| statement.executeUpdate(); | ||
| } catch (SQLException ex) { | ||
| throw new IOException(ex); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteWorld(String worldName) throws IOException, UnknownWorldException { | ||
| try (Connection con = source.getConnection(); | ||
| PreparedStatement statement = con.prepareStatement(DELETE_WORLD_QUERY)) { | ||
| statement.setString(1, worldName); | ||
| if (statement.executeUpdate() == 0) { | ||
| throw new UnknownWorldException(worldName); | ||
| } | ||
| } catch (SQLException ex) { | ||
| throw new IOException(ex); | ||
| } | ||
| } | ||
|
|
||
| private void init() throws SQLException { | ||
| try (Connection con = source.getConnection()) { | ||
| try (PreparedStatement statement = con.prepareStatement(CREATE_WORLDS_TABLE_QUERY)) { | ||
| statement.execute(); | ||
| } | ||
| try (PreparedStatement statement = con.prepareStatement(CREATE_VERSIONING_TABLE_QUERY)) { | ||
| statement.execute(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.