Skip to content
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

Fix EcoProfile read method to be asynchronous #390

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -84,18 +82,11 @@ public final void save() {
* @param <T> The type of the key.
* @return The value, or null if not found.
*/
@Nullable
public final <T> T read(@NotNull final UUID uuid,
@NotNull final PersistentDataKey<T> key) {
@NotNull
public final <T> CompletableFuture<T> read(@NotNull final UUID uuid,
@NotNull final PersistentDataKey<T> key) {
DataTypeSerializer<T> serializer = key.getType().getSerializer(this);
Future<T> future = executor.submit(() -> serializer.readAsync(uuid, key));

try {
return future.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return null;
}
return CompletableFuture.supplyAsync(() -> serializer.readAsync(uuid, key), executor);
}

/**
Expand Down Expand Up @@ -126,7 +117,7 @@ public final SerializedProfile serializeProfile(@NotNull final UUID uuid,
Map<PersistentDataKey<?>, CompletableFuture<Object>> futures = keys.stream()
.collect(Collectors.toMap(
key -> key,
key -> CompletableFuture.supplyAsync(() -> read(uuid, key), executor)
key -> CompletableFuture.supplyAsync(() -> read(uuid, key).join(), executor)
));

Map<PersistentDataKey<?>, Object> data = futures.entrySet().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ abstract class EcoProfile(
return this.data[key] as T
}

this.data[key] = if (key.isSavedLocally) {
val future = if (key.isSavedLocally) {
handler.localHandler.read(uuid, key)
} else {
handler.defaultHandler.read(uuid, key)
} ?: key.defaultValue
}

this.data[key] = future.thenApply { it ?: key.defaultValue }.join()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But here you are joining again. Is this intentional? Because when another plugin calls the read method on an EcoProfile (such as EcoSkills), it is still blocking, isn’t it?


return read(key)
}
Expand Down
Loading