-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
335 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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
62 changes: 62 additions & 0 deletions
62
src/main/java/pl/minecodes/mineeconomy/data/database/MongoDbService.java
This file contains 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,62 @@ | ||
package pl.minecodes.mineeconomy.data.database; | ||
|
||
import com.mongodb.MongoClient; | ||
import com.mongodb.MongoCredential; | ||
import com.mongodb.ServerAddress; | ||
import com.mongodb.client.FindIterable; | ||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.client.MongoDatabase; | ||
import com.mongodb.client.model.Filters; | ||
import com.mongodb.client.model.ReplaceOptions; | ||
import eu.okaeri.injector.annotation.Inject; | ||
import org.bson.Document; | ||
import pl.minecodes.mineeconomy.data.configuration.Configuration; | ||
import pl.minecodes.mineeconomy.data.database.element.DatabaseData; | ||
import pl.minecodes.mineeconomy.data.database.element.model.DataService; | ||
import pl.minecodes.mineeconomy.profile.Profile; | ||
|
||
import java.util.Collections; | ||
import java.util.UUID; | ||
import java.util.logging.Logger; | ||
|
||
public class MongoDbService implements DataService { | ||
|
||
@Inject | ||
private Logger logger; | ||
@Inject | ||
private Configuration configuration; | ||
|
||
private MongoCollection<Document> mongoCollection; | ||
|
||
@Override | ||
public Profile loadData(UUID uniqueId) { | ||
FindIterable<Document> findDocument = mongoCollection.find(Filters.eq("uniqueId", uniqueId.toString())); | ||
Document document = findDocument.first(); | ||
if (document == null) return null; | ||
|
||
return new Profile(UUID.fromString(document.getString("uniqueId")), document.getDouble("balance")); | ||
} | ||
|
||
@Override | ||
public void saveData(Profile profile) { | ||
Document document = new Document(); | ||
document.put("uniqueId", profile.getUniqueId().toString()); | ||
document.put("balance", profile.getBalance()); | ||
mongoCollection.replaceOne(Filters.eq("uniqueId", profile.getUniqueId().toString()), document, new ReplaceOptions().upsert(true)); | ||
} | ||
|
||
@Override | ||
public void deleteData(Profile profile) { | ||
mongoCollection.deleteOne(Filters.eq("uniqueId", profile.getUniqueId().toString())); | ||
} | ||
|
||
@Override | ||
public void connect() { | ||
DatabaseData databaseData = this.configuration.getDatabaseData(); | ||
MongoCredential mongoCredential = MongoCredential.createCredential(databaseData.getUsername(), databaseData.getDatabase(), databaseData.getPassword().toCharArray()); | ||
MongoClient mongoClient = new MongoClient(new ServerAddress(databaseData.getHost(), databaseData.getPort()), Collections.singletonList(mongoCredential)); | ||
MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseData.getDatabase()); | ||
mongoCollection = mongoDatabase.getCollection("economyUsers"); | ||
this.logger.info("Successfully connected to MongoDb database."); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
src/main/java/pl/minecodes/mineeconomy/data/database/MySQLService.java
This file contains 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,101 @@ | ||
package pl.minecodes.mineeconomy.data.database; | ||
|
||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import eu.okaeri.injector.annotation.Inject; | ||
import pl.minecodes.mineeconomy.data.configuration.Configuration; | ||
import pl.minecodes.mineeconomy.data.database.element.model.DataService; | ||
import pl.minecodes.mineeconomy.profile.Profile; | ||
|
||
import java.sql.*; | ||
import java.util.UUID; | ||
import java.util.logging.Logger; | ||
|
||
public class MySQLService implements DataService { | ||
|
||
@Inject | ||
private Logger logger; | ||
@Inject | ||
private Configuration configuration; | ||
|
||
private HikariDataSource dataSource; | ||
private Connection connection; | ||
|
||
@Override | ||
public Profile loadData(UUID uniqueId) { | ||
try (Statement statement = this.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE)) { | ||
ResultSet resultSet = statement.executeQuery("SELECT * FROM `economyUsers` WHERE `uniqueId` = '" + uniqueId + "';"); | ||
if (resultSet.first()) { | ||
return new Profile(uniqueId, resultSet.getDouble("balance")); | ||
} | ||
} catch (SQLException exception) { | ||
exception.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void saveData(Profile profile) { | ||
try (PreparedStatement preparedStatement = this.getConnection() | ||
.prepareStatement("INSERT INTO `economyUsers` (`uniqueId`, `balance`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `uniqueId` = VALUES(uniqueId), `balance` = VALUES(balance);")) { | ||
preparedStatement.setString(1, profile.getUniqueId().toString()); | ||
preparedStatement.setDouble(2, profile.getBalance()); | ||
preparedStatement.executeUpdate(); | ||
this.logger.info("Successfully updated profile by uniqueId " + profile.getUniqueId() + " to MySQL data."); | ||
} catch (SQLException exception) { | ||
this.logger.severe("There was an unexpected incident, while trying to save profile with id " + profile.getUniqueId()); | ||
exception.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void deleteData(Profile profile) { | ||
try (PreparedStatement preparedStatement = this.getConnection().prepareStatement("DELETE FROM `economyUsers` WHERE `uniqueId` = ?;")) { | ||
preparedStatement.setString(1, profile.getUniqueId().toString()); | ||
preparedStatement.executeUpdate(); | ||
} catch (SQLException exception) { | ||
this.logger.severe("There was an unexpected incident, while trying to remove plot with id " + profile.getUniqueId()); | ||
} | ||
} | ||
|
||
@Override | ||
public void connect() { | ||
dataSource = new HikariDataSource(this.getHikariConfig()); | ||
this.logger.info("Successfully connected to MySQL database!"); | ||
try (Statement statement = dataSource.getConnection().createStatement()) { | ||
statement.executeUpdate("CREATE TABLE IF NOT EXISTS `economyUsers` (`uniqueId` VARCHAR(64) PRIMARY KEY, `balance` DOUBLE);"); | ||
} catch (SQLException exception) { | ||
exception.printStackTrace(); | ||
this.logger.severe("There was an unexpected incident, while trying to create plots table"); | ||
} | ||
} | ||
|
||
private Connection getConnection() throws SQLException { | ||
if (connection == null || connection.isClosed()) { | ||
connection = dataSource.getConnection(); | ||
} | ||
return connection; | ||
} | ||
|
||
private HikariConfig getHikariConfig() { | ||
HikariConfig hikariConfig = new HikariConfig(); | ||
hikariConfig.setJdbcUrl(String.format("jdbc:mysql://%s:%d/%s", | ||
configuration.getDatabaseData().getHost(), | ||
configuration.getDatabaseData().getPort(), | ||
configuration.getDatabaseData().getDatabase())); | ||
hikariConfig.setUsername(configuration.getDatabaseData().getUsername()); | ||
hikariConfig.setPassword(configuration.getDatabaseData().getPassword()); | ||
|
||
hikariConfig.addDataSourceProperty("cachePrepStmts", "true"); | ||
hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250"); | ||
hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); | ||
hikariConfig.addDataSourceProperty("useServerPrepStmts", "true"); | ||
hikariConfig.addDataSourceProperty("useLocalSessionState", "true"); | ||
hikariConfig.addDataSourceProperty("rewriteBatchedStatements", "true"); | ||
hikariConfig.addDataSourceProperty("cacheResultSetMetadata", "true"); | ||
hikariConfig.addDataSourceProperty("cacheServerConfiguration", "true"); | ||
hikariConfig.addDataSourceProperty("elideSetAutoCommits", "true"); | ||
hikariConfig.addDataSourceProperty("maintainTimeStats", "false"); | ||
return hikariConfig; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/pl/minecodes/mineeconomy/data/database/element/DatabaseData.java
This file contains 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,30 @@ | ||
package pl.minecodes.mineeconomy.data.database.element; | ||
|
||
import eu.okaeri.configs.OkaeriConfig; | ||
import eu.okaeri.configs.annotation.Comment; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
|
||
@EqualsAndHashCode(callSuper = false) | ||
@Data | ||
public class DatabaseData extends OkaeriConfig { | ||
|
||
@Comment("Typ bazy danych: MYSQL, MONGODB") | ||
private DatabaseType databaseType = DatabaseType.MYSQL; | ||
|
||
@Comment("Adres bazy danych") | ||
private String host = "localhost"; | ||
|
||
@Comment("Nazwa bazy") | ||
private String database = "plots"; | ||
|
||
@Comment("Port bazy") | ||
private int port = 2000; | ||
|
||
@Comment("Użytkownik") | ||
private String username = "plots"; | ||
|
||
@Comment("Hasło dostępu") | ||
private String password = "password"; | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/pl/minecodes/mineeconomy/data/database/element/DatabaseType.java
This file contains 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,8 @@ | ||
package pl.minecodes.mineeconomy.data.database.element; | ||
|
||
public enum DatabaseType { | ||
|
||
MYSQL, | ||
MONGODB | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/pl/minecodes/mineeconomy/data/database/element/model/DataService.java
This file contains 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,17 @@ | ||
package pl.minecodes.mineeconomy.data.database.element.model; | ||
|
||
|
||
import pl.minecodes.mineeconomy.profile.Profile; | ||
|
||
import java.util.UUID; | ||
|
||
public interface DataService { | ||
|
||
Profile loadData(UUID uniqueId); | ||
|
||
void saveData(Profile profile); | ||
|
||
void deleteData(Profile profile); | ||
|
||
void connect(); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/pl/minecodes/mineeconomy/data/database/element/object/DatabaseUpdate.java
This file contains 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,13 @@ | ||
package pl.minecodes.mineeconomy.data.database.element.object; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class DatabaseUpdate { | ||
|
||
private long lastUpdate; | ||
private boolean neededUpdate; | ||
|
||
} |
This file contains 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.