Skip to content

Commit

Permalink
Chore: Use virtual thread executor for db tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Skullians committed Dec 28, 2024
1 parent e9d96d2 commit 181a233
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 135 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.skullian.skyfactions.common.database;

import org.jooq.DSLContext;

import java.util.concurrent.Executor;

public abstract class AbstractTableManager {

protected final DSLContext ctx;
protected final Executor executor;

public AbstractTableManager(DSLContext ctx, Executor executor) {
this.ctx = ctx;
this.executor = executor;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class DatabaseManager {
Expand All @@ -46,7 +48,10 @@ public class DatabaseManager {
@Getter private FactionsDatabaseManager factionsManager;
@Getter private FactionElectionManager electionManager;

private Executor databaseExecutor;

public void initialise(String type) {
this.databaseExecutor = Executors.newVirtualThreadPerTaskExecutor();
createDataSource(SkyApi.getInstance().getFileAPI().getDatabasePath(), type);
}

Expand Down Expand Up @@ -160,17 +165,17 @@ private void setup() {
if (result.success) {
SLogger.info("Database migrations complete: ({} Migrations completed in {}ms)", result.getSuccessfulMigrations().size(), result.getTotalMigrationTime());

this.currencyManager = new CurrencyDatabaseManager(this.ctx);
this.defencesManager = new DefencesDatabaseManager(this.ctx);
this.notificationManager = new NotificationDatabaseManager(this.ctx);
this.playerIslandManager = new PlayerIslandsDatabaseManager(this.ctx);
this.playerManager = new PlayerDatabaseManager(this.ctx);

this.factionAuditLogManager = new FactionAuditLogDatabaseManager(this.ctx);
this.factionInvitesManager = new FactionInvitesDatabaseManager(this.ctx);
this.factionIslandManager = new FactionIslandDatabaseManager(this.ctx);
this.factionsManager = new FactionsDatabaseManager(this.ctx);
this.electionManager = new FactionElectionManager(this.ctx);
this.currencyManager = new CurrencyDatabaseManager(this.ctx, this.databaseExecutor);
this.defencesManager = new DefencesDatabaseManager(this.ctx, this.databaseExecutor);
this.notificationManager = new NotificationDatabaseManager(this.ctx, this.databaseExecutor);
this.playerIslandManager = new PlayerIslandsDatabaseManager(this.ctx, this.databaseExecutor);
this.playerManager = new PlayerDatabaseManager(this.ctx, this.databaseExecutor);

this.factionAuditLogManager = new FactionAuditLogDatabaseManager(this.ctx, this.databaseExecutor);
this.factionInvitesManager = new FactionInvitesDatabaseManager(this.ctx, this.databaseExecutor);
this.factionIslandManager = new FactionIslandDatabaseManager(this.ctx, this.databaseExecutor);
this.factionsManager = new FactionsDatabaseManager(this.ctx, this.databaseExecutor);
this.electionManager = new FactionElectionManager(this.ctx, this.databaseExecutor);
} else {
ErrorUtil.handleDatabaseSetupError(new Exception("Failed to complete migrations - " + result.getFailedMigrations().size() + " Migrations failed: " + result.getException()));
result.getFailedMigrations().forEach(migration -> SLogger.info("Migration failed: {}", migration.description));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
package net.skullian.skyfactions.common.database.impl;

import net.skullian.skyfactions.common.api.SkyApi;
import net.skullian.skyfactions.common.database.AbstractTableManager;
import org.jooq.DSLContext;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import static net.skullian.skyfactions.common.database.tables.Islands.ISLANDS;
import static net.skullian.skyfactions.common.database.tables.FactionIslands.FACTION_ISLANDS;

public class CurrencyDatabaseManager {
public class CurrencyDatabaseManager extends AbstractTableManager {

private final DSLContext ctx;

public CurrencyDatabaseManager(DSLContext ctx) {
this.ctx = ctx;
public CurrencyDatabaseManager(DSLContext ctx, Executor executor) {
super(ctx, executor);
}

// ------------------ GEMS ------------------ //

public CompletableFuture<Integer> getGems(UUID playerUUID) {


return CompletableFuture.supplyAsync(() -> ctx.select(ISLANDS.GEMS)
.from(ISLANDS)
.where(ISLANDS.UUID.eq(playerUUID.toString()))
.fetchOneInto(Integer.class));
.fetchOneInto(Integer.class), executor);
}

public CompletableFuture<Integer> getGems(String factionName) {
return CompletableFuture.supplyAsync(() -> ctx.select(FACTION_ISLANDS.GEMS)
.from(FACTION_ISLANDS)
.where(FACTION_ISLANDS.FACTIONNAME.eq(factionName))
.fetchOneInto(Integer.class));
.fetchOneInto(Integer.class), executor);
}

public CompletableFuture<Void> modifyGems(UUID playerUUID, int amount, boolean subtract) {
Expand All @@ -44,7 +42,7 @@ public CompletableFuture<Void> modifyGems(UUID playerUUID, int amount, boolean s
.set(ISLANDS.GEMS, (subtract ? current - amount : current + amount))
.where(ISLANDS.UUID.eq(playerUUID.toString()))
.execute();
});
}, executor);
}

public CompletableFuture<Void> modifyGems(String factionName, int amount, boolean subtract) {
Expand All @@ -56,7 +54,7 @@ public CompletableFuture<Void> modifyGems(String factionName, int amount, boolea
.set(FACTION_ISLANDS.GEMS, (subtract ? current - amount : current + amount))
.where(FACTION_ISLANDS.FACTIONNAME.eq(factionName))
.execute();
});
}, executor);
}

// ------------------ RUNES ------------------ //
Expand All @@ -66,14 +64,14 @@ public CompletableFuture<Integer> getRunes(UUID playerUUID) {
return CompletableFuture.supplyAsync(() -> ctx.select(ISLANDS.RUNES)
.from(ISLANDS)
.where(ISLANDS.UUID.eq(playerUUID.toString()))
.fetchOneInto(Integer.class));
.fetchOneInto(Integer.class), executor);
}

public CompletableFuture<Integer> getRunes(String factionName) {
return CompletableFuture.supplyAsync(() -> ctx.select(FACTION_ISLANDS.RUNES)
.from(FACTION_ISLANDS)
.where(FACTION_ISLANDS.FACTIONNAME.eq(factionName))
.fetchOneInto(Integer.class));
.fetchOneInto(Integer.class), executor);
}

public CompletableFuture<Void> modifyRunes(UUID playerUUID, int amount, boolean subtract) {
Expand All @@ -85,7 +83,7 @@ public CompletableFuture<Void> modifyRunes(UUID playerUUID, int amount, boolean
.set(ISLANDS.RUNES, (subtract ? current - amount : current + amount))
.where(ISLANDS.UUID.eq(playerUUID.toString()))
.execute();
});
}, executor);
}

public CompletableFuture<Void> modifyRunes(String factionName, int amount, boolean subtract) {
Expand All @@ -97,7 +95,7 @@ public CompletableFuture<Void> modifyRunes(String factionName, int amount, boole
.set(FACTION_ISLANDS.RUNES, (subtract ? current - amount : current + amount))
.where(FACTION_ISLANDS.FACTIONNAME.eq(factionName))
.execute();
});
}, executor);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.skullian.skyfactions.common.database.impl;

import net.skullian.skyfactions.common.config.types.Settings;
import net.skullian.skyfactions.common.database.AbstractTableManager;
import net.skullian.skyfactions.common.database.tables.records.DefenceLocationsRecord;
import net.skullian.skyfactions.common.util.SkyLocation;
import org.jooq.Condition;
Expand All @@ -11,15 +12,14 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import static net.skullian.skyfactions.common.database.tables.DefenceLocations.DEFENCE_LOCATIONS;

public class DefencesDatabaseManager {
public class DefencesDatabaseManager extends AbstractTableManager {

private final DSLContext ctx;

public DefencesDatabaseManager(DSLContext ctx) {
this.ctx = ctx;
public DefencesDatabaseManager(DSLContext ctx, Executor executor) {
super(ctx, executor);
}

public CompletableFuture<List<SkyLocation>> getDefenceLocations(Condition condition, String type) {
Expand All @@ -41,7 +41,7 @@ public CompletableFuture<List<SkyLocation>> getDefenceLocations(Condition condit
}

return locations;
});
}, executor);
}

public CompletableFuture<Void> registerDefenceLocations(List<SkyLocation> locations, String owner, boolean faction) {
Expand All @@ -54,7 +54,7 @@ public CompletableFuture<Void> registerDefenceLocations(List<SkyLocation> locati
.execute();
}
});
});
}, executor);
}

public CompletableFuture<Void> removeDefenceLocations(List<SkyLocation> locations, String owner, boolean isFaction) {
Expand All @@ -67,15 +67,15 @@ public CompletableFuture<Void> removeDefenceLocations(List<SkyLocation> location
.execute();
}
});
});
}, executor);
}

public CompletableFuture<Void> removeAllDefences(String owner, boolean isFaction) {
return CompletableFuture.runAsync(() -> {
ctx.deleteFrom(DEFENCE_LOCATIONS)
.where((isFaction ? DEFENCE_LOCATIONS.FACTIONNAME.eq(owner) : DEFENCE_LOCATIONS.UUID.eq(owner)), (isFaction ? DEFENCE_LOCATIONS.TYPE.eq("faction") : DEFENCE_LOCATIONS.TYPE.eq("player")))
.execute();
});
}, executor);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.skullian.skyfactions.common.database.impl;

import net.skullian.skyfactions.common.database.AbstractTableManager;
import net.skullian.skyfactions.common.database.tables.records.NotificationsRecord;
import net.skullian.skyfactions.common.notification.NotificationData;
import net.skullian.skyfactions.common.user.SkyUser;
Expand All @@ -12,15 +13,14 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import static net.skullian.skyfactions.common.database.tables.Notifications.NOTIFICATIONS;

public class NotificationDatabaseManager {
public class NotificationDatabaseManager extends AbstractTableManager {

private final DSLContext ctx;

public NotificationDatabaseManager(DSLContext ctx) {
this.ctx = ctx;
public NotificationDatabaseManager(DSLContext ctx, Executor executor) {
super(ctx, executor);
}

public CompletableFuture<Void> createNotifications(List<NotificationData> notifications) {
Expand All @@ -34,7 +34,7 @@ public CompletableFuture<Void> createNotifications(List<NotificationData> notifi
.execute();
}
});
});
}, executor);
}

public CompletableFuture<Void> removeNotifications(List<NotificationData> notifications) {
Expand All @@ -47,7 +47,7 @@ public CompletableFuture<Void> removeNotifications(List<NotificationData> notifi
.execute();
}
});
});
}, executor);
}

public CompletableFuture<List<NotificationData>> getNotifications(SkyUser player) {
Expand All @@ -68,6 +68,6 @@ public CompletableFuture<List<NotificationData>> getNotifications(SkyUser player
}

return data;
});
}, executor);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package net.skullian.skyfactions.common.database.impl;

import net.skullian.skyfactions.common.api.SkyApi;
import net.skullian.skyfactions.common.database.AbstractTableManager;
import net.skullian.skyfactions.common.database.struct.PlayerData;
import net.skullian.skyfactions.common.database.tables.records.PlayerDataRecord;
import org.jooq.DSLContext;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import static net.skullian.skyfactions.common.database.tables.PlayerData.PLAYER_DATA;

public class PlayerDatabaseManager {
public class PlayerDatabaseManager extends AbstractTableManager {

private final DSLContext ctx;

public PlayerDatabaseManager(DSLContext ctx) {
this.ctx = ctx;
public PlayerDatabaseManager(DSLContext ctx, Executor executor) {
super(ctx, executor);
}

public CompletableFuture<Void> registerPlayer(UUID uuid, boolean shouldRegister) {
Expand All @@ -25,7 +25,7 @@ public CompletableFuture<Void> registerPlayer(UUID uuid, boolean shouldRegister)
.columns(PLAYER_DATA.UUID, PLAYER_DATA.DISCORD_ID, PLAYER_DATA.LAST_RAID, PLAYER_DATA.LOCALE)
.values(uuid.toString(), "none", (long) 0, SkyApi.getInstance().getPlayerAPI().getLocale(uuid))
.execute();
});
}, executor);
}

public CompletableFuture<PlayerData> getPlayerData(UUID uuid) {
Expand All @@ -40,7 +40,7 @@ public CompletableFuture<PlayerData> getPlayerData(UUID uuid) {
result.getLastRaid(),
result.getLocale()
) : null;
});
}, executor);
}

public CompletableFuture<Void> registerDiscordLink(UUID playerUUID, String discordID) {
Expand All @@ -51,7 +51,7 @@ public CompletableFuture<Void> registerDiscordLink(UUID playerUUID, String disco
.set(PLAYER_DATA.DISCORD_ID, discordID)
.where(PLAYER_DATA.UUID.eq(playerUUID.toString()))
.execute();
});
}, executor);
}

public CompletableFuture<Void> updateLastRaid(UUID playerUUID, long time) {
Expand All @@ -61,14 +61,14 @@ public CompletableFuture<Void> updateLastRaid(UUID playerUUID, long time) {
.set(PLAYER_DATA.LAST_RAID, time)
.where(PLAYER_DATA.UUID.eq(playerUUID.toString()))
.execute();
});
}, executor);
}

public CompletableFuture<String> getPlayerLocale(UUID uuid) {
return CompletableFuture.supplyAsync(() -> ctx.select(PLAYER_DATA.LOCALE)
.from(PLAYER_DATA)
.where(PLAYER_DATA.UUID.eq(uuid.toString()))
.fetchOneInto(String.class));
.fetchOneInto(String.class), executor);
}

public CompletableFuture<Void> setPlayerLocale(UUID uuid, String locale) {
Expand All @@ -78,7 +78,7 @@ public CompletableFuture<Void> setPlayerLocale(UUID uuid, String locale) {
.set(PLAYER_DATA.LOCALE, locale)
.where(PLAYER_DATA.UUID.eq(uuid.toString()))
.execute();
});
}, executor);
}


Expand Down
Loading

0 comments on commit 181a233

Please sign in to comment.