-
-
Couldn't load subscription status.
- Fork 21
GH-1147 Make UserManager use UserRepository #1147
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
92868fe
1a7dd18
3c6ad2a
7d3ee9e
9414f7d
b3efc5f
2050371
0cdf8b2
11813d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,9 @@ public enum DatabaseDriverType { | |
| MARIADB(MARIADB_DRIVER, MARIADB_JDBC_URL), | ||
| POSTGRESQL(POSTGRESQL_DRIVER, POSTGRESQL_JDBC_URL), | ||
| H2(H2_DRIVER, H2_JDBC_URL), | ||
| SQLITE(SQLITE_DRIVER, SQLITE_JDBC_URL); | ||
| SQLITE(SQLITE_DRIVER, SQLITE_JDBC_URL), | ||
|
|
||
| H2_TEST(H2_DRIVER, "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MYSQL"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. raczej bym unikał dodawania kawałków kodu które są tylko dla testów, jaki tutaj był problem? może da się to lepiej rozwiązać? |
||
|
|
||
| private final String driver; | ||
| private final String urlFormat; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.eternalcode.core.user.database; | ||
|
|
||
| import com.eternalcode.core.user.User; | ||
| import java.util.Collection; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| public interface UserRepository { | ||
|
|
||
| CompletableFuture<Optional<User>> getUser(UUID uniqueId); | ||
|
|
||
| CompletableFuture<Void> saveUser(User player); | ||
|
|
||
| CompletableFuture<User> updateUser(User player); | ||
|
|
||
| CompletableFuture<Void> deleteUser(UUID uniqueId); | ||
|
|
||
| CompletableFuture<Collection<User>> fetchAllUsers(); | ||
|
|
||
| CompletableFuture<Collection<User>> fetchUsersBatch(int batchSize); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.eternalcode.core.user.database; | ||
|
|
||
| import eu.okaeri.configs.OkaeriConfig; | ||
| import eu.okaeri.configs.annotation.Comment; | ||
| import lombok.Getter; | ||
| import lombok.experimental.Accessors; | ||
|
|
||
| @Getter | ||
| @Accessors(fluent = true) | ||
| public class UserRepositoryConfig extends OkaeriConfig implements UserRepositorySettings { | ||
|
|
||
| @Comment({ | ||
| "# Should plugin use batches to fetch users from the database?", | ||
| "# We suggest turning this setting to TRUE for servers with more than 10k users", | ||
| "# Set this to false if you are using SQLITE or H2 database (local databases)" | ||
| }) | ||
| public boolean useBatchDatabaseFetching = false; | ||
|
|
||
| @Comment({ | ||
| "# Size of batches querried to the database", | ||
| "# Value must be greater than 0!" | ||
| }) | ||
| public int batchDatabaseFetchSize = 1000; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package com.eternalcode.core.user.database; | ||
|
|
||
| import com.eternalcode.commons.scheduler.Scheduler; | ||
| import com.eternalcode.core.database.AbstractRepositoryOrmLite; | ||
| import com.eternalcode.core.database.DatabaseManager; | ||
| import com.eternalcode.core.injector.annotations.Inject; | ||
| import com.eternalcode.core.injector.annotations.component.Repository; | ||
| import com.eternalcode.core.user.User; | ||
| import com.j256.ormlite.table.TableUtils; | ||
| import java.sql.SQLException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| @Repository | ||
| public class UserRepositoryOrmLite extends AbstractRepositoryOrmLite implements UserRepository { | ||
|
|
||
| @Inject | ||
| public UserRepositoryOrmLite(DatabaseManager databaseManager, Scheduler scheduler) throws SQLException { | ||
| super(databaseManager, scheduler); | ||
| TableUtils.createTableIfNotExists(databaseManager.connectionSource(), UserTable.class); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Optional<User>> getUser(UUID uniqueId) { | ||
| return this.selectSafe(UserTable.class, uniqueId) | ||
| .thenApply(optional -> optional.map(UserTable::toUser)); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Collection<User>> fetchAllUsers() { | ||
| return this.selectAll(UserTable.class) | ||
| .thenApply(userTables -> userTables.stream() | ||
| .map(UserTable::toUser) | ||
| .toList()); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Collection<User>> fetchUsersBatch(int batchSize) { | ||
| return CompletableFuture.supplyAsync(() -> { | ||
|
||
| try { | ||
| var users = new ArrayList<User>(); | ||
|
|
||
| int offset = 0; | ||
| while (true) { | ||
| List<UserTable> batch = this.selectBatch(UserTable.class, offset, batchSize).join(); | ||
|
|
||
| if (batch.isEmpty()) { | ||
| break; | ||
| } | ||
|
|
||
| batch.stream() | ||
| .map(UserTable::toUser) | ||
| .forEach(users::add); | ||
|
|
||
| offset += batchSize; | ||
| } | ||
|
|
||
| return users; | ||
| } catch (Exception exception) { | ||
| throw new RuntimeException("Failed to fetch users in batches", exception); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> saveUser(User user) { | ||
| return this.save(UserTable.class, UserTable.from(user)).thenApply(v -> null); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<User> updateUser(User user) { | ||
| return this.save(UserTable.class, UserTable.from(user)).thenApply(v -> user); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> deleteUser(UUID uniqueId) { | ||
| return this.deleteById(UserTable.class, uniqueId).thenApply(v -> null); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.eternalcode.core.user.database; | ||
|
|
||
| public interface UserRepositorySettings { | ||
|
|
||
| boolean useBatchDatabaseFetching(); | ||
|
|
||
| int batchDatabaseFetchSize(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.eternalcode.core.user.database; | ||
|
|
||
| import com.eternalcode.core.user.User; | ||
| import com.j256.ormlite.field.DatabaseField; | ||
| import com.j256.ormlite.table.DatabaseTable; | ||
| import java.util.UUID; | ||
|
|
||
| @DatabaseTable(tableName = "eternal_core_users") | ||
| public class UserTable { | ||
|
|
||
| @DatabaseField(columnName = "id", id = true) | ||
| private UUID uniqueId; | ||
|
|
||
| @DatabaseField(columnName = "name") | ||
| private String name; | ||
|
|
||
| UserTable() {} | ||
|
|
||
| UserTable(UUID uniqueId, String name) { | ||
| this.uniqueId = uniqueId; | ||
| this.name = name; | ||
| } | ||
|
|
||
| public User toUser() { | ||
| return new User(this.uniqueId, this.name); | ||
| } | ||
|
|
||
| public static UserTable from(User user) { | ||
| return new UserTable(user.getUniqueId(), user.getName()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm czemu tak? jak w testach coś nie gra to settuj fielda jest public