9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-29 11:39:14 +00:00

Version data via the database implementation

This commit is contained in:
William
2022-07-04 14:53:26 +01:00
parent d78dd42b72
commit f2d4bec138
5 changed files with 20 additions and 20 deletions

View File

@@ -135,7 +135,7 @@ public abstract class Database {
public abstract CompletableFuture<List<VersionedUserData>> getUserData(@NotNull User user);
/**
* Prune user data records for a given user to the maximum value as configured
* <b>(Internal)</b> Prune user data records for a given user to the maximum value as configured
*
* @param user The user to prune data for
* @return A future returning void when complete
@@ -147,10 +147,11 @@ public abstract class Database {
* This will remove the oldest data for the user if the amount of data exceeds the limit as configured
*
* @param user The user to add data for
* @param userData The uniquely versioned data to add as a {@link VersionedUserData}
* @param userData The {@link UserData} to set. The implementation should version it with a random UUID and the current timestamp during insertion.
* @return A future returning void when complete
* @see VersionedUserData#version(UserData)
*/
public abstract CompletableFuture<Void> setUserData(@NotNull User user, @NotNull VersionedUserData userData);
public abstract CompletableFuture<Void> setUserData(@NotNull User user, @NotNull UserData userData);
/**
* Close the database connection

View File

@@ -289,18 +289,16 @@ public class MySqlDatabase extends Database {
}
@Override
public CompletableFuture<Void> setUserData(@NotNull User user, @NotNull VersionedUserData userData) {
public CompletableFuture<Void> setUserData(@NotNull User user, @NotNull UserData userData) {
return CompletableFuture.runAsync(() -> {
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
INSERT INTO `%data_table%`
(`player_uuid`,`version_uuid`,`timestamp`,`data`)
VALUES (?,?,?,?);"""))) {
VALUES (?,UUID(),NOW(),?);"""))) {
statement.setString(1, user.uuid.toString());
statement.setString(2, userData.versionUUID().toString());
statement.setTimestamp(3, Timestamp.from(userData.versionTimestamp().toInstant()));
statement.setBlob(4, new ByteArrayInputStream(Snappy
.compress(userData.userData().toJson().getBytes(StandardCharsets.UTF_8))));
statement.setBlob(2, new ByteArrayInputStream(Snappy
.compress(userData.toJson().getBytes(StandardCharsets.UTF_8))));
statement.executeUpdate();
}
} catch (SQLException | IOException e) {