This commit is contained in:
Alexander Söderberg
2020-05-17 17:26:48 +02:00
parent 792fa1f11d
commit 29f2863cf4
8 changed files with 292 additions and 60 deletions

View File

@ -32,7 +32,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
@ -92,35 +91,58 @@ public class UUIDPipeline {
}
/**
* Asynchronously attempt to fetch the mapping from a given UUID or username
* Asynchronously attempt to fetch the mapping from a list of UUIDs
*
* @param requests UUIDs or usernames
* @return Future that may complete with the mapping
* @param requests UUIDs
* @return Mappings
*/
public CompletableFuture<Collection<UUIDMapping>> get(@NotNull final Collection<Object> requests) {
public CompletableFuture<Collection<UUIDMapping>> getNames(@NotNull final Collection<UUID> requests) {
final List<UUIDService> serviceList = this.getServiceListInstance();
return CompletableFuture.supplyAsync(() -> {
final List<UUIDMapping> mappings = new ArrayList<>(requests.size());
outer: for (final Object request : requests) {
if (!(request instanceof String) && !(request instanceof UUID)) {
throw new IllegalArgumentException("Request has to be either a username or UUID");
final List<UUID> remainingRequests = new ArrayList<>(requests);
for (final UUIDService service : serviceList) {
final List<UUIDMapping> completedRequests = service.getNames(remainingRequests);
for (final UUIDMapping mapping : completedRequests) {
remainingRequests.remove(mapping.getUuid());
}
for (final UUIDService service : serviceList) {
final Optional<?> result = service.get(request);
if (result.isPresent()) {
final String username = request instanceof String ? (String) request
: (String) result.get();
final UUID uuid = request instanceof UUID ? (UUID) request
: (UUID) result.get();
final UUIDMapping mapping = new UUIDMapping(uuid, username);
this.consume(mapping);
mappings.add(mapping);
continue outer;
}
}
throw new ServiceError("End of pipeline");
mappings.addAll(completedRequests);
}
return mappings;
if (mappings.size() == requests.size()) {
return mappings;
}
throw new ServiceError("End of pipeline");
}, this.executor);
}
/**
* Asynchronously attempt to fetch the mapping from a list of names
*
* @param requests Names
* @return Mappings
*/
public CompletableFuture<Collection<UUIDMapping>> getUUIDs(@NotNull final Collection<String> requests) {
final List<UUIDService> serviceList = this.getServiceListInstance();
return CompletableFuture.supplyAsync(() -> {
final List<UUIDMapping> mappings = new ArrayList<>(requests.size());
final List<String> remainingRequests = new ArrayList<>(requests);
for (final UUIDService service : serviceList) {
final List<UUIDMapping> completedRequests = service.getUUIDs(remainingRequests);
for (final UUIDMapping mapping : completedRequests) {
remainingRequests.remove(mapping.getUsername());
}
mappings.addAll(completedRequests);
}
if (mappings.size() == requests.size()) {
return mappings;
}
throw new ServiceError("End of pipeline");
}, this.executor);
}

View File

@ -27,7 +27,7 @@ package com.plotsquared.core.uuid;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.List;
import java.util.UUID;
/**
@ -35,40 +35,22 @@ import java.util.UUID;
*/
public interface UUIDService {
default Optional<?> get(@NotNull final Object request) {
if (request instanceof UUID) {
return get((UUID) request);
} else if (request instanceof String) {
return get((String) request);
} else {
throw new IllegalArgumentException("Request has to be either a username or UUID");
}
}
/**
* Attempt to complete the given requests. Returns the mappings
* that could be created by this server
*
* @param uuids Requests
* @return Completed requests
*/
@NotNull List<UUIDMapping> getNames(@NotNull final List<UUID> uuids);
/**
* Get a stored username from the service if it exists.
* This should <b>not</b> trigger any fetching of
* usernames from other services.
* <p>
* If the username isn't stored in this service,
* this completes with an empty optional.
* Attempt to complete the given requests. Returns the mappings
* that could be created by this server
*
* @param uuid Player UUID
* @return Optional that may contain the username if it exists
* @param usernames Requests
* @return Completed requests
*/
@NotNull Optional<String> get(@NotNull final UUID uuid);
/**
* Get a stored UUID from the service if it exists.
* This should <b>not</b> trigger any fetching of
* UUID from other services.
* <p>
* If the UUID isn't stored in this service,
* this completes with an empty optional.
*
* @param username Player username
* @return Optional that may contain the UUID if it exists
*/
@NotNull Optional<UUID> get(@NotNull final String username);
@NotNull List<UUIDMapping> getUUIDs(@NotNull final List<String> usernames);
}

View File

@ -27,11 +27,14 @@ package com.plotsquared.core.uuid.offline;
import com.google.common.base.Charsets;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.uuid.UUIDMapping;
import com.plotsquared.core.uuid.UUIDService;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.UUID;
/**
@ -46,12 +49,16 @@ public class OfflineModeUUIDService implements UUIDService {
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(Charsets.UTF_8));
}
@Override @NotNull public Optional<String> get(@NotNull final UUID uuid) {
return Optional.empty();
@Override @NotNull public List<UUIDMapping> getNames(@NotNull final List<UUID> uuids) {
return Collections.emptyList();
}
@Override @NotNull public Optional<UUID> get(@NotNull final String username) {
return Optional.of(this.getFromUsername(username));
@Override @NotNull public List<UUIDMapping> getUUIDs(@NotNull List<String> usernames) {
final List<UUIDMapping> mappings = new ArrayList<>(usernames.size());
for (final String username : usernames) {
mappings.add(new UUIDMapping(getFromUsername(username), username));
}
return mappings;
}
}