Skip to content

GH-979 GH-1049 2.0/postgres ready and persister change #1050

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

Open
wants to merge 3 commits 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
@@ -1,7 +1,6 @@
package com.eternalcode.core.database.wrapper;
package com.eternalcode.core.database;

import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.database.DatabaseManager;
import com.j256.ormlite.dao.Dao;
import panda.std.function.ThrowingFunction;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void connect() throws SQLException {

case POSTGRESQL -> {
this.dataSource.setDriverClassName("org.postgresql.Driver");
this.dataSource.setJdbcUrl("jdbc:postgresql://" + this.config.database.hostname + ":" + this.config.database.port + "/");
this.dataSource.setJdbcUrl("jdbc:postgresql://" + this.config.database.hostname + ":" + this.config.database.port + "/" + this.config.database.database);
}

default -> throw new SQLException("SQL type '" + databaseType + "' not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Object javaToSqlArg(FieldType fieldType, Object javaObject) {
worldName = loc.getWorld().getName();
}

return worldName + "/" + loc.getX() + "/" + loc.getY() + "/" + loc.getZ() + "/" + loc.getYaw() + "/" + loc.getPitch();
return worldName + "/" + loc.getBlockX() + "/" + loc.getBlockY() + "/" + loc.getBlockZ() + "/" + loc.getYaw() + "/" + loc.getPitch();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.database.DatabaseManager;
import com.eternalcode.core.database.wrapper.AbstractRepositoryOrmLite;
import com.eternalcode.core.database.AbstractRepositoryOrmLite;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Repository;
import com.j256.ormlite.stmt.Where;
Expand Down Expand Up @@ -30,17 +30,17 @@ public CompletableFuture<Set<UUID>> findReceivers(Set<UUID> onlineUniqueIds) {
return CompletableFuture.completedFuture(onlineUniqueIds);
}

CompletableFuture<List<AutoMessageTable>> wrapperList =
CompletableFuture<List<AutoMessageTable>> tableList =
this.action(
AutoMessageTable.class, dao -> {
Where<AutoMessageTable, Object> where = dao.queryBuilder().where();
where.in("unique_id", onlineUniqueIds);
return where.query();
});

return wrapperList.thenApply(ignores -> {
return tableList.thenApply(ignores -> {
Set<UUID> ignoredIds = ignores.stream()
.map(wrapper -> wrapper.uniqueId)
.map(autoMessageTable -> autoMessageTable.uniqueId)
.collect(Collectors.toSet());

return onlineUniqueIds.stream()
Expand All @@ -62,8 +62,8 @@ public CompletableFuture<Boolean> switchReceiving(UUID uniqueId) {
.thenApply(result -> false);
}

AutoMessageTable wrapper = optional.get();
return this.delete(AutoMessageTable.class, wrapper).thenApply(state -> true);
AutoMessageTable autoMessageTable = optional.get();
return this.delete(AutoMessageTable.class, autoMessageTable).thenApply(state -> true);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.database.DatabaseManager;
import com.eternalcode.core.database.wrapper.AbstractRepositoryOrmLite;
import com.eternalcode.core.database.AbstractRepositoryOrmLite;
import com.eternalcode.core.feature.home.Home;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Repository;
Expand All @@ -25,50 +25,50 @@ class HomeRepositoryOrmLite extends AbstractRepositoryOrmLite implements HomeRep
@Inject
private HomeRepositoryOrmLite(DatabaseManager databaseManager, Scheduler scheduler) throws SQLException {
super(databaseManager, scheduler);
TableUtils.createTableIfNotExists(databaseManager.connectionSource(), HomeWrapper.class);
TableUtils.createTableIfNotExists(databaseManager.connectionSource(), HomeTable.class);
}

@Override
public CompletableFuture<Optional<Home>> getHome(UUID playerUniqueId) {
return this.select(HomeWrapper.class, playerUniqueId)
return this.select(HomeTable.class, playerUniqueId)
.thenApply(Optional::of)
.thenApply(home -> home.map(HomeWrapper::toHome));
.thenApply(home -> home.map(HomeTable::toHome));
}

@Override
public CompletableFuture<Optional<Home>> getHome(User user, String homeName) {
return this.action(HomeWrapper.class, dao -> Optional.ofNullable(dao.queryBuilder()
return this.action(HomeTable.class, dao -> Optional.ofNullable(dao.queryBuilder()
.where()
.eq(OWNER_COLUMN, user.getUniqueId())
.and()
.eq(NAME_COLUMN, homeName)
.queryForFirst()).map(HomeWrapper::toHome));
.queryForFirst()).map(HomeTable::toHome));
}

@Override
public CompletableFuture<Optional<Home>> getHome(UUID playerUniqueId, String homeName) {
return this.action(HomeWrapper.class, dao -> Optional.ofNullable(dao.queryBuilder()
return this.action(HomeTable.class, dao -> Optional.ofNullable(dao.queryBuilder()
.where()
.eq(OWNER_COLUMN, playerUniqueId)
.and()
.eq(NAME_COLUMN, homeName)
.queryForFirst()).map(HomeWrapper::toHome));
.queryForFirst()).map(HomeTable::toHome));
}

@Override
public CompletableFuture<Void> saveHome(Home home) {
return this.save(HomeWrapper.class, HomeWrapper.from(home)).thenApply(result -> null);
return this.save(HomeTable.class, HomeTable.from(home)).thenApply(result -> null);
}

@Override
public CompletableFuture<Integer> deleteHome(UUID playerUniqueId) {
return this.deleteById(HomeWrapper.class, playerUniqueId);
return this.deleteById(HomeTable.class, playerUniqueId);
}

@Override
public CompletableFuture<Integer> deleteHome(User user, String homeName) {
return this.action(HomeWrapper.class, dao -> {
DeleteBuilder<HomeWrapper, Object> builder = dao.deleteBuilder();
return this.action(HomeTable.class, dao -> {
DeleteBuilder<HomeTable, Object> builder = dao.deleteBuilder();
builder.where()
.eq(OWNER_COLUMN, user.getUniqueId())
.and()
Expand All @@ -79,8 +79,8 @@ public CompletableFuture<Integer> deleteHome(User user, String homeName) {

@Override
public CompletableFuture<Integer> deleteHome(UUID playerUniqueId, String homeName) {
return this.action(HomeWrapper.class, dao -> {
DeleteBuilder<HomeWrapper, Object> builder = dao.deleteBuilder();
return this.action(HomeTable.class, dao -> {
DeleteBuilder<HomeTable, Object> builder = dao.deleteBuilder();
builder.where()
.eq(OWNER_COLUMN, playerUniqueId)
.and()
Expand All @@ -91,27 +91,27 @@ public CompletableFuture<Integer> deleteHome(UUID playerUniqueId, String homeNam

@Override
public CompletableFuture<Set<Home>> getHomes() {
return this.selectAll(HomeWrapper.class)
.thenApply(homeOrmLites -> homeOrmLites.stream().map(HomeWrapper::toHome).collect(Collectors.toSet()));
return this.selectAll(HomeTable.class)
.thenApply(homeOrmLites -> homeOrmLites.stream().map(HomeTable::toHome).collect(Collectors.toSet()));
}

@Override
public CompletableFuture<Set<Home>> getHomes(User user) {
return this.action(HomeWrapper.class, dao -> dao.queryBuilder()
return this.action(HomeTable.class, dao -> dao.queryBuilder()
.where()
.eq(OWNER_COLUMN, user.getUniqueId())
.query()).thenApply(homes -> homes.stream()
.map(HomeWrapper::toHome)
.map(HomeTable::toHome)
.collect(Collectors.toSet()));
}

@Override
public CompletableFuture<Set<Home>> getHomes(UUID playerUniqueId) {
return this.action(HomeWrapper.class, dao -> dao.queryBuilder()
return this.action(HomeTable.class, dao -> dao.queryBuilder()
.where()
.eq(OWNER_COLUMN, playerUniqueId)
.query()).thenApply(homes -> homes.stream()
.map(HomeWrapper::toHome)
.map(HomeTable::toHome)
.collect(Collectors.toSet()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.bukkit.Location;

@DatabaseTable(tableName = "eternal_core_homes")
class HomeWrapper {
class HomeTable {

@DatabaseField(columnName = "id", id = true)
private UUID uuid;
Expand All @@ -23,9 +23,9 @@ class HomeWrapper {
@DatabaseField(columnName = "location", persisterClass = LocationPersister.class)
private Location location;

HomeWrapper() {}
HomeTable() {}

HomeWrapper(UUID uuid, UUID owner, String name, Location location) {
HomeTable(UUID uuid, UUID owner, String name, Location location) {
this.uuid = uuid;
this.owner = owner;
this.name = name;
Expand All @@ -36,8 +36,8 @@ Home toHome() {
return new HomeImpl(this.uuid, this.owner, this.name, this.location);
}

static HomeWrapper from(Home home) {
return new HomeWrapper(home.getUuid(), home.getOwner(), home.getName(), home.getLocation());
static HomeTable from(Home home) {
return new HomeTable(home.getUuid(), home.getOwner(), home.getName(), home.getLocation());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.database.DatabaseManager;
import com.eternalcode.core.database.wrapper.AbstractRepositoryOrmLite;
import com.eternalcode.core.database.AbstractRepositoryOrmLite;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Repository;
import com.google.common.cache.CacheBuilder;
Expand All @@ -29,19 +29,19 @@ class IgnoreRepositoryOrmLite extends AbstractRepositoryOrmLite implements Ignor

private static final UUID IGNORE_ALL = UUID.nameUUIDFromBytes("*".getBytes());

private final Dao<IgnoreWrapper, Long> cachedDao;
private final Dao<IgnoreTable, Long> cachedDao;
private final LoadingCache<UUID, Set<UUID>> ignores;

@Inject
private IgnoreRepositoryOrmLite(DatabaseManager databaseManager, Scheduler scheduler) throws SQLException {
super(databaseManager, scheduler);
this.cachedDao = databaseManager.getDao(IgnoreWrapper.class);
this.cachedDao = databaseManager.getDao(IgnoreTable.class);

this.ignores = CacheBuilder.newBuilder()
.expireAfterAccess(Duration.ofMinutes(15))
.refreshAfterWrite(Duration.ofMinutes(3))
.build(new IgnoreLoader());
TableUtils.createTableIfNotExists(databaseManager.connectionSource(), IgnoreWrapper.class);
TableUtils.createTableIfNotExists(databaseManager.connectionSource(), IgnoreTable.class);
}

@Override
Expand All @@ -65,7 +65,7 @@ public CompletableFuture<Void> ignore(UUID by, UUID target) {
Set<UUID> uuids = this.ignores.get(by);

if (!uuids.contains(target)) {
this.save(IgnoreWrapper.class, new IgnoreWrapper(by, target))
this.save(IgnoreTable.class, new IgnoreTable(by, target))
.thenRun(() -> this.ignores.refresh(by));
}
}
Expand All @@ -82,8 +82,8 @@ public CompletableFuture<Void> ignoreAll(UUID by) {

@Override
public CompletableFuture<Void> unIgnore(UUID by, UUID target) {
return this.action(IgnoreWrapper.class, dao -> {
DeleteBuilder<IgnoreWrapper, Object> builder = dao.deleteBuilder();
return this.action(IgnoreTable.class, dao -> {
DeleteBuilder<IgnoreTable, Object> builder = dao.deleteBuilder();

builder.where()
.eq("player_id", by)
Expand All @@ -97,8 +97,8 @@ public CompletableFuture<Void> unIgnore(UUID by, UUID target) {

@Override
public CompletableFuture<Void> unIgnoreAll(UUID by) {
return this.action(IgnoreWrapper.class, dao -> {
DeleteBuilder<IgnoreWrapper, Object> builder = dao.deleteBuilder();
return this.action(IgnoreTable.class, dao -> {
DeleteBuilder<IgnoreTable, Object> builder = dao.deleteBuilder();

builder.where()
.eq("player_id", by);
Expand All @@ -111,14 +111,14 @@ public CompletableFuture<Void> unIgnoreAll(UUID by) {
@ApiStatus.Internal
@Override
public CompletableFuture<Void> purgeAll() {
return this.deleteAll(IgnoreWrapper.class)
return this.deleteAll(IgnoreTable.class)
.thenRun(this.ignores::invalidateAll);
}

@DatabaseTable(tableName = "eternal_core_ignores")
private static class IgnoreWrapper {
private static class IgnoreTable {

@DatabaseField(generatedId = true)
@DatabaseField(id = true)
Long id;

@DatabaseField(columnName = "player_id", uniqueCombo = true)
Expand All @@ -127,9 +127,9 @@ private static class IgnoreWrapper {
@DatabaseField(columnName = "ignored_id", uniqueCombo = true)
UUID ignoredUuid;

IgnoreWrapper() {}
IgnoreTable() {}

IgnoreWrapper(UUID playerUuid, UUID ignoredUuid) {
IgnoreTable(UUID playerUuid, UUID ignoredUuid) {
this.playerUuid = playerUuid;
this.ignoredUuid = ignoredUuid;
}
Expand All @@ -143,7 +143,7 @@ private class IgnoreLoader extends CacheLoader<UUID, Set<UUID>> {
.where().eq("player_id", key)
.query()
.stream()
.map(ignoreWrapper -> ignoreWrapper.ignoredUuid)
.map(ignoreTable -> ignoreTable.ignoredUuid)
.collect(Collectors.toSet());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.database.DatabaseManager;
import com.eternalcode.core.database.wrapper.AbstractRepositoryOrmLite;
import com.eternalcode.core.database.AbstractRepositoryOrmLite;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Repository;

Expand All @@ -22,49 +22,49 @@ class PrisonerRepositoryOrmLite extends AbstractRepositoryOrmLite implements Pri
@Inject
private PrisonerRepositoryOrmLite(DatabaseManager databaseManager, Scheduler scheduler) throws SQLException {
super(databaseManager, scheduler);
TableUtils.createTableIfNotExists(databaseManager.connectionSource(), PrisonerWrapper.class);
TableUtils.createTableIfNotExists(databaseManager.connectionSource(), PrisonerTable.class);
}

@Override
public CompletableFuture<Optional<JailedPlayer>> getPrisoner(UUID uuid) {
return this.selectSafe(PrisonerWrapper.class, uuid)
.thenApply(optional -> optional.map(prisonerWrapper -> prisonerWrapper.toPrisoner()));
return this.selectSafe(PrisonerTable.class, uuid)
.thenApply(optional -> optional.map(prisonerTable -> prisonerTable.toPrisoner()));
}

@Override
public CompletableFuture<Set<JailedPlayer>> getPrisoners() {
return this.selectAll(PrisonerWrapper.class)
.thenApply(prisonerWrappers -> prisonerWrappers.stream()
.map(PrisonerWrapper::toPrisoner)
return this.selectAll(PrisonerTable.class)
.thenApply(prisonerTables -> prisonerTables.stream()
.map(PrisonerTable::toPrisoner)
.collect(Collectors.toSet()));
}

@Override
public void deletePrisoner(UUID uuid) {
this.deleteById(PrisonerWrapper.class, uuid);
this.deleteById(PrisonerTable.class, uuid);
}

@Override
public void editPrisoner(JailedPlayer jailedPlayer) {
this.save(PrisonerWrapper.class, PrisonerWrapper.from(jailedPlayer));
this.save(PrisonerTable.class, PrisonerTable.from(jailedPlayer));
}

@Override
public void deleteAllPrisoners() {
this.delete(PrisonerWrapper.class, new PrisonerWrapper());
this.delete(PrisonerTable.class, new PrisonerTable());
}

@Override
public CompletableFuture<List<JailedPlayer>> getAllPrisoners() {
return this.selectAll(PrisonerWrapper.class)
.thenApply(prisonerWrappers -> prisonerWrappers.stream()
.map(PrisonerWrapper::toPrisoner)
return this.selectAll(PrisonerTable.class)
.thenApply(prisonerTables -> prisonerTables.stream()
.map(PrisonerTable::toPrisoner)
.toList());
}

@Override
public void savePrisoner(JailedPlayer jailedPlayer) {
this.save(PrisonerWrapper.class, PrisonerWrapper.from(jailedPlayer));
this.save(PrisonerTable.class, PrisonerTable.from(jailedPlayer));
}

}
Loading