Cached dumps

This commit is contained in:
Colin McDonald 2016-05-15 00:49:03 -04:00
parent 5b55bc243d
commit 4f8366b966

View File

@ -10,60 +10,100 @@ import spark.Response;
import spark.Route;
import java.util.*;
import java.util.concurrent.TimeUnit;
public final class GETDump implements Route {
private List<UUID> banCache = new ArrayList<>();
private List<UUID> blacklistCache = new ArrayList<>();
private Map<String, List<UUID>> grantCache = new HashMap<>();
public GETDump() {
Thread dumpUpdater = new Thread() {
@Override
public void run() {
int tick = 0;
while (true) {
if (tick == 0 || tick % 2 == 1) {
List<UUID> banCache = new ArrayList<>();
List<UUID> blacklistCache = new ArrayList<>();
APIv3.getDatastore().createQuery(Punishment.class).field("type").in(ImmutableSet.of(
Punishment.PunishmentType.BAN,
Punishment.PunishmentType.BLACKLIST
)).forEach((punishment) -> {
if (!punishment.isActive()) {
return;
}
if (punishment.getType() == Punishment.PunishmentType.BAN) {
banCache.add(punishment.getUser());
} else if (punishment.getType() == Punishment.PunishmentType.BLACKLIST) {
blacklistCache.add(punishment.getUser());
}
});
GETDump.this.banCache = banCache;
GETDump.this.blacklistCache = blacklistCache;
}
if (tick == 0 || tick % 2 == 0) {
Map<String, List<UUID>> grantCache = new HashMap<>();
APIv3.getDatastore().createQuery(Grant.class).forEach((grant) -> {
if (grant.isActive()) {
List<UUID> users = grantCache.get(grant.getRank());
if (users == null) {
users = new ArrayList<>();
grantCache.put(grant.getRank(), users);
}
users.add(grant.getUser());
}
});
GETDump.this.grantCache = grantCache;
}
try {
Thread.sleep(TimeUnit.MINUTES.toMillis(3));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
tick++;
}
}
};
dumpUpdater.setName("APIv3 - Dump Updater");
dumpUpdater.setDaemon(true);
dumpUpdater.start();
}
public Object handle(Request req, Response res) {
String type = req.params("type");
switch (type.toLowerCase()) {
case "blacklist":
case "ban":
case "mute":
case "warn":
List<UUID> effectedUsers = new ArrayList<>();
APIv3.getDatastore().createQuery(Punishment.class).field("type").equal(type.toUpperCase()).forEach((punishment) -> {
if (punishment.isActive()) {
effectedUsers.add(punishment.getUser());
}
});
return effectedUsers;
return banCache;
case "blacklist":
return blacklistCache;
case "accessdeniable": // Lowercase d because we convert to lowercase above
// We have to name it effectedUsers2 because Java's
// scoping in switch statements is really dumb.
List<UUID> effectedUsers2 = new ArrayList<>();
List<UUID> result = new ArrayList<>();
APIv3.getDatastore().createQuery(Punishment.class).field("type").in(ImmutableSet.of(
Punishment.PunishmentType.BAN,
Punishment.PunishmentType.BLACKLIST
)).forEach((punishment) -> {
if (punishment.isActive()) {
effectedUsers2.add(punishment.getUser());
}
});
result.addAll(banCache);
result.addAll(blacklistCache);
return effectedUsers2;
return result;
case "grant":
Map<String, List<UUID>> grantDump = new HashMap<>();
APIv3.getDatastore().createQuery(Grant.class).forEach((grant) -> {
if (grant.isActive()) {
List<UUID> users = grantDump.get(grant.getRank());
if (users == null) {
users = new ArrayList<>();
grantDump.put(grant.getRank(), users);
}
users.add(grant.getUser());
}
});
return grantDump;
return grantCache;
default:
return ErrorUtils.invalidInput(type + " is not a valid type. Not in [blacklist, ban, mute, warn, accessDeniable, grant]");
return ErrorUtils.invalidInput(type + " is not a valid type. Not in [ban, blacklist, accessDeniable, grant]");
}
}