-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pablo Herrera <[email protected]>
- Loading branch information
1 parent
c5cbdb3
commit 6d924c8
Showing
24 changed files
with
700 additions
and
272 deletions.
There are no files selected for viewing
This file contains 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 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 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
113 changes: 113 additions & 0 deletions
113
core/src/main/java/tc/oc/pgm/db/SqlUsernameResolver.java
This file contains 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,113 @@ | ||
package tc.oc.pgm.db; | ||
|
||
import com.google.common.collect.Lists; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.time.Instant; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.BiConsumer; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import tc.oc.pgm.util.concurrent.ThreadSafeConnection; | ||
import tc.oc.pgm.util.usernames.AbstractBatchingUsernameResolver; | ||
|
||
public class SqlUsernameResolver extends AbstractBatchingUsernameResolver { | ||
private static final int BATCH_SIZE = 500; | ||
private final SQLDatastore datastore; | ||
|
||
public SqlUsernameResolver(SQLDatastore datastore) { | ||
this.datastore = datastore; | ||
} | ||
|
||
protected void process(UUID uuid, CompletableFuture<UsernameResponse> future) { | ||
datastore.submitQuery(new SingleSelect(uuid, future)).join(); | ||
} | ||
|
||
@Override | ||
protected void process(List<UUID> uuids) { | ||
List<List<UUID>> partitions = Lists.partition(uuids, BATCH_SIZE); | ||
CompletableFuture<?>[] futures = new CompletableFuture[partitions.size()]; | ||
for (int i = 0; i < partitions.size(); i++) { | ||
futures[i] = datastore.submitQuery(new BatchSelect(partitions.get(i), this::complete)); | ||
} | ||
CompletableFuture.allOf(futures).join(); | ||
} | ||
|
||
private static class SingleSelect implements ThreadSafeConnection.Query { | ||
private final UUID uuid; | ||
private final CompletableFuture<UsernameResponse> future; | ||
|
||
public SingleSelect(UUID uuid, CompletableFuture<UsernameResponse> future) { | ||
this.uuid = uuid; | ||
this.future = future; | ||
} | ||
|
||
@Override | ||
public String getFormat() { | ||
return "SELECT name, expires FROM usernames WHERE id = ? LIMIT 1"; | ||
} | ||
|
||
@Override | ||
public void query(PreparedStatement statement) throws SQLException { | ||
statement.setString(1, uuid.toString()); | ||
|
||
try (final ResultSet result = statement.executeQuery()) { | ||
future.complete( | ||
!result.next() | ||
? UsernameResponse.empty() | ||
: UsernameResponse.of( | ||
result.getString(1), | ||
null, | ||
Instant.ofEpochMilli(result.getLong(2)), | ||
SqlUsernameResolver.class)); | ||
} | ||
} | ||
} | ||
|
||
private static class BatchSelect implements ThreadSafeConnection.Query { | ||
private final List<UUID> uuids; | ||
private final BiConsumer<UUID, UsernameResponse> completion; | ||
|
||
public BatchSelect(List<UUID> uuids, BiConsumer<UUID, UsernameResponse> completion) { | ||
this.uuids = uuids; | ||
this.completion = completion; | ||
} | ||
|
||
@Override | ||
public String getFormat() { | ||
return "SELECT id, name, expires FROM usernames WHERE id IN (" | ||
+ Stream.generate(() -> "?").limit(uuids.size()).collect(Collectors.joining(",")) | ||
+ ")"; | ||
} | ||
|
||
@Override | ||
public void query(PreparedStatement statement) throws SQLException { | ||
for (int i = 0; i < uuids.size(); i++) { | ||
statement.setString(i + 1, uuids.get(i).toString()); | ||
} | ||
|
||
try (final ResultSet result = statement.executeQuery()) { | ||
Set<UUID> leftover = new HashSet<>(uuids); | ||
while (result.next()) { | ||
UUID uuid = UUID.fromString(result.getString(1)); | ||
leftover.remove(uuid); | ||
|
||
completion.accept( | ||
uuid, | ||
UsernameResponse.of( | ||
result.getString(2), | ||
null, | ||
Instant.ofEpochMilli(result.getLong(3)), | ||
SqlUsernameResolver.class)); | ||
} | ||
|
||
leftover.forEach(uuid -> completion.accept(uuid, UsernameResponse.empty())); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
Oops, something went wrong.