-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
Draft
CitralFlo
wants to merge
9
commits into
master
Choose a base branch
from
user-manager-database
base: master
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.
+576
−246
Draft
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
92868fe
Add database for users, add batch fetching, update tests
CitralFlo 1a7dd18
Codestyle fixes
CitralFlo 3c6ad2a
Resolve gemini review
CitralFlo 7d3ee9e
Resolve @sadcenter and @Jakubk15 reviews
CitralFlo 9414f7d
wip
Rollczi b3efc5f
wip2
CitralFlo 2050371
Merge remote-tracking branch 'origin/user-manager-database' into user…
CitralFlo 0cdf8b2
wip
CitralFlo 11813d4
Update methods and failsafe for cache
CitralFlo 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
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
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
71 changes: 61 additions & 10 deletions
71
eternalcore-core/src/main/java/com/eternalcode/core/user/UserManager.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 |
---|---|---|
@@ -1,56 +1,107 @@ | ||
package com.eternalcode.core.user; | ||
|
||
import com.eternalcode.commons.algorithm.BatchProcessor; | ||
import com.eternalcode.core.injector.annotations.Inject; | ||
import com.eternalcode.core.injector.annotations.component.Service; | ||
import com.eternalcode.core.user.database.UserRepository; | ||
import com.eternalcode.core.user.database.UserRepositorySettings; | ||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Consumer; | ||
|
||
@Service | ||
public class UserManager { | ||
|
||
private final Map<UUID, User> usersByUUID = new ConcurrentHashMap<>(); | ||
private final Map<String, User> usersByName = new ConcurrentHashMap<>(); | ||
private final Cache<UUID, User> usersByUUID; | ||
private final Cache<String, User> usersByName; | ||
|
||
private final UserRepository userRepository; | ||
private final UserRepositorySettings userRepositorySettings; | ||
|
||
@Inject | ||
public UserManager(UserRepository userRepository, UserRepositorySettings userRepositorySettings) { | ||
this.userRepositorySettings = userRepositorySettings; | ||
this.usersByUUID = Caffeine.newBuilder().build(); | ||
this.usersByName = Caffeine.newBuilder().build(); | ||
|
||
this.userRepository = userRepository; | ||
|
||
fetchUsers(); | ||
CitralFlo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
public Optional<User> getUser(UUID uuid) { | ||
return Optional.ofNullable(this.usersByUUID.get(uuid)); | ||
return Optional.ofNullable(this.usersByUUID.getIfPresent(uuid)); | ||
} | ||
|
||
public Optional<User> getUser(String name) { | ||
return Optional.ofNullable(this.usersByName.get(name)); | ||
return Optional.ofNullable(this.usersByName.getIfPresent(name)); | ||
} | ||
|
||
public User getOrCreate(UUID uuid, String name) { | ||
User userByUUID = this.usersByUUID.get(uuid); | ||
User userByUUID = this.usersByUUID.getIfPresent(uuid); | ||
|
||
if (userByUUID != null) { | ||
return userByUUID; | ||
} | ||
|
||
User userByName = this.usersByName.get(name); | ||
User userByName = this.usersByName.getIfPresent(name); | ||
|
||
if (userByName != null) { | ||
return userByName; | ||
} | ||
|
||
this.userRepository.saveUser(new User(uuid, name)); | ||
return this.create(uuid, name); | ||
CitralFlo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
public User create(UUID uuid, String name) { | ||
if (this.usersByUUID.containsKey(uuid) || this.usersByName.containsKey(name)) { | ||
if (this.usersByName.getIfPresent(name) != null || this.usersByUUID.getIfPresent(uuid) != null) { | ||
throw new IllegalStateException("User already exists"); | ||
} | ||
|
||
User user = new User(uuid, name); | ||
this.usersByUUID.put(uuid, user); | ||
this.usersByName.put(name, user); | ||
|
||
this.userRepository.saveUser(user); | ||
return user; | ||
} | ||
|
||
public Collection<User> getUsers() { | ||
return Collections.unmodifiableCollection(this.usersByUUID.values()); | ||
return Collections.unmodifiableCollection(this.usersByUUID.asMap().values()); | ||
} | ||
|
||
private void fetchUsers() { | ||
if (this.userRepositorySettings.batchDatabaseFetchSize() <= 0) { | ||
throw new IllegalArgumentException("Value for batchDatabaseFetchSize must be greater than 0!"); | ||
} | ||
|
||
Consumer<Collection<User>> batchSave = users -> | ||
{ | ||
BatchProcessor<User> batchProcessor = new BatchProcessor<>(users, this.userRepositorySettings.batchDatabaseFetchSize()); | ||
|
||
do { | ||
batchProcessor.processNext(user -> { | ||
usersByName.put(user.getName(), user); | ||
usersByUUID.put(user.getUniqueId(), user); | ||
}); | ||
|
||
} while (!batchProcessor.isComplete()); | ||
}; | ||
CitralFlo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
if (this.userRepositorySettings.useBatchDatabaseFetching()) { | ||
this.userRepository.fetchUsersBatch(this.userRepositorySettings.batchDatabaseFetchSize()) | ||
.thenAccept(batchSave); | ||
} | ||
else { | ||
|
||
this.userRepository.fetchAllUsers() | ||
.thenAccept(batchSave); | ||
|
||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
eternalcore-core/src/main/java/com/eternalcode/core/user/database/UserRepository.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,22 @@ | ||
package com.eternalcode.core.user.database; | ||
|
||
import com.eternalcode.core.user.User; | ||
import java.util.Collection; | ||
import java.util.UUID; | ||
import java.util.concurrent.CompletableFuture; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public interface UserRepository { | ||
|
||
@Nullable CompletableFuture<User> getUser(UUID uniqueId); | ||
CitralFlo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
CompletableFuture<Void> saveUser(User player); | ||
|
||
CompletableFuture<User> updateUser(UUID uniqueId, User player); | ||
CitralFlo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
CompletableFuture<Void> deleteUser(UUID uniqueId); | ||
|
||
CompletableFuture<Collection<User>> fetchAllUsers(); | ||
|
||
CompletableFuture<Collection<User>> fetchUsersBatch(int batchSize); | ||
} |
24 changes: 24 additions & 0 deletions
24
eternalcore-core/src/main/java/com/eternalcode/core/user/database/UserRepositoryConfig.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,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; | ||
} |
84 changes: 84 additions & 0 deletions
84
eternalcore-core/src/main/java/com/eternalcode/core/user/database/UserRepositoryOrmLite.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,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.Collection; | ||
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<User> getUser(UUID uniqueId) { | ||
return this.selectSafe(UserTable.class, uniqueId) | ||
.thenApply(optional -> optional.map(userTable -> userTable.toUser()).orElseGet(null)); | ||
} | ||
|
||
@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 dao = this.databaseManager.getDao(UserTable.class); | ||
var users = new java.util.ArrayList<User>(); | ||
|
||
int offset = 0; | ||
while (true) { | ||
var queryBuilder = dao.queryBuilder(); | ||
queryBuilder.limit((long) batchSize); | ||
queryBuilder.offset((long) offset); | ||
|
||
var batch = dao.query(queryBuilder.prepare()); | ||
|
||
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); | ||
} | ||
}); | ||
} | ||
CitralFlo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
@Override | ||
public CompletableFuture<Void> saveUser(User user) { | ||
return this.save(UserTable.class, UserTable.from(user)).thenApply(v -> null); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<User> updateUser(UUID uniqueId, 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); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...nalcore-core/src/main/java/com/eternalcode/core/user/database/UserRepositorySettings.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,8 @@ | ||
package com.eternalcode.core.user.database; | ||
|
||
public interface UserRepositorySettings { | ||
|
||
boolean useBatchDatabaseFetching(); | ||
|
||
int batchDatabaseFetchSize(); | ||
} |
31 changes: 31 additions & 0 deletions
31
eternalcore-core/src/main/java/com/eternalcode/core/user/database/UserTable.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,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()); | ||
} | ||
} |
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.