Skip to content

Commit

Permalink
refactor storage agent
Browse files Browse the repository at this point in the history
always load entries on register
  • Loading branch information
HSGamer committed Sep 26, 2024
1 parent 165c4e7 commit 6fabd03
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import me.hsgamer.topper.agent.storage.simple.config.DatabaseConfig;
import me.hsgamer.topper.agent.storage.simple.converter.FlatEntryConverter;
import me.hsgamer.topper.agent.storage.simple.converter.SqlEntryConverter;
import me.hsgamer.topper.agent.storage.simple.supplier.DataStorageSupplier;
import me.hsgamer.topper.agent.storage.simple.supplier.FlatStorageSupplier;
import me.hsgamer.topper.agent.storage.simple.supplier.MySqlStorageSupplier;
import me.hsgamer.topper.agent.storage.simple.supplier.SqliteStorageSupplier;
import me.hsgamer.topper.agent.storage.supplier.DataStorageSupplier;

import java.io.File;
import java.util.concurrent.Executor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.hsgamer.topper.agent.storage.supplier;
package me.hsgamer.topper.agent.storage.simple.supplier;

import me.hsgamer.topper.agent.storage.DataStorage;
import me.hsgamer.topper.core.DataHolder;

public interface DataStorageSupplier<K, V> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package me.hsgamer.topper.agent.storage.simple.supplier;

import me.hsgamer.hscore.config.Config;
import me.hsgamer.topper.agent.storage.DataStorage;
import me.hsgamer.topper.agent.storage.simple.converter.FlatEntryConverter;
import me.hsgamer.topper.agent.storage.simple.util.AutoSaveConfig;
import me.hsgamer.topper.agent.storage.supplier.DataStorage;
import me.hsgamer.topper.agent.storage.supplier.DataStorageSupplier;
import me.hsgamer.topper.core.DataHolder;

import java.io.File;
Expand Down Expand Up @@ -46,19 +45,17 @@ public DataStorage<K, V> getStorage(DataHolder<K, V> holder) {
private final AutoSaveConfig config = new AutoSaveConfig(configProvider.apply(new File(holderBaseFolder, configNameProvider.apply(holder.getName()))), runTaskFunction);

@Override
public CompletableFuture<Map<K, V>> load() {
public Map<K, V> load() {
Map<String[], Object> values = config.getValues(false);
return CompletableFuture.supplyAsync(() -> {
Map<K, V> map = new HashMap<>();
values.forEach((path, value) -> {
V finalValue = converter.toValue(value);
if (finalValue != null) {
K finalKey = converter.toKey(path[0]);
map.put(finalKey, finalValue);
}
});
return map;
Map<K, V> map = new HashMap<>();
values.forEach((path, value) -> {
V finalValue = converter.toValue(value);
if (finalValue != null) {
K finalKey = converter.toKey(path[0]);
map.put(finalKey, finalValue);
}
});
return map;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import me.hsgamer.hscore.logger.common.LogLevel;
import me.hsgamer.hscore.logger.common.Logger;
import me.hsgamer.hscore.logger.provider.LoggerProvider;
import me.hsgamer.topper.agent.storage.DataStorage;
import me.hsgamer.topper.agent.storage.simple.converter.SqlEntryConverter;
import me.hsgamer.topper.agent.storage.supplier.DataStorage;
import me.hsgamer.topper.agent.storage.supplier.DataStorageSupplier;
import me.hsgamer.topper.core.DataHolder;

import java.sql.Connection;
Expand Down Expand Up @@ -38,22 +37,20 @@ public Connection getAndCreateTable(String name) throws SQLException {
public DataStorage<K, V> getStorage(DataHolder<K, V> holder) {
return new DataStorage<K, V>(holder) {
@Override
public CompletableFuture<Map<K, V>> load() {
public Map<K, V> load() {
String name = holder.getName();
return CompletableFuture.supplyAsync(() -> {
Connection connection = null;
try {
connection = getAndCreateTable(name);
return converter.selectAll(connection, name).querySafe(converter::getMap).orElseGet(Collections::emptyMap);
} catch (SQLException e) {
logger.log(LogLevel.ERROR, "Failed to load top holder", e);
return Collections.emptyMap();
} finally {
if (connection != null) {
flushConnection(connection);
}
Connection connection = null;
try {
connection = getAndCreateTable(name);
return converter.selectAll(connection, name).querySafe(converter::getMap).orElseGet(Collections::emptyMap);
} catch (SQLException e) {
logger.log(LogLevel.ERROR, "Failed to load top holder", e);
return Collections.emptyMap();
} finally {
if (connection != null) {
flushConnection(connection);
}
});
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.hsgamer.topper.agent.storage.supplier;
package me.hsgamer.topper.agent.storage;

import me.hsgamer.topper.core.DataHolder;

Expand All @@ -13,7 +13,7 @@ protected DataStorage(DataHolder<K, V> holder) {
this.holder = holder;
}

public abstract CompletableFuture<Map<K, V>> load();
public abstract Map<K, V> load();

public abstract CompletableFuture<Void> save(K key, V value, boolean urgent);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.hsgamer.topper.agent.storage;

import me.hsgamer.topper.agent.core.Agent;
import me.hsgamer.topper.agent.storage.supplier.DataStorage;
import me.hsgamer.topper.core.DataEntry;
import me.hsgamer.topper.core.DataHolder;

Expand All @@ -19,7 +18,6 @@ public class StorageAgent<K, V> implements Agent<K, V>, Runnable {
private final Logger logger;
private final DataHolder<K, V> holder;
private final DataStorage<K, V> storage;
private final List<Runnable> loadListeners = new ArrayList<>();
private int maxEntryPerCall = 10;
private boolean urgentSave = false;
private boolean loadOnCreate = false;
Expand Down Expand Up @@ -50,23 +48,23 @@ private void load(DataEntry<K, V> entry) {
});
}

private void loadAll() {
try {
storage.load()
.forEach((uuid, value) -> holder.getOrCreateEntry(uuid).setValue(value, false));
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to load top entries for " + holder.getName(), e);
}
}

public void loadIfExist(K key) {
holder.getEntry(key).ifPresent(this::load);
}

@Override
public void start() {
storage.onRegister();
storage.load()
.whenComplete((entries, throwable) -> {
if (throwable != null) {
logger.log(Level.SEVERE, "Failed to load top entries", throwable);
}
if (entries != null) {
entries.forEach((uuid, value) -> holder.getOrCreateEntry(uuid).setValue(value, false));
}
loadListeners.forEach(Runnable::run);
});
loadAll();
}

@Override
Expand Down Expand Up @@ -124,8 +122,4 @@ public void setUrgentLoad(boolean urgentLoad) {
public void setLoadOnCreate(boolean loadOnCreate) {
this.loadOnCreate = loadOnCreate;
}

public void addLoadListener(Runnable runnable) {
loadListeners.add(runnable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,24 @@ public NumberTopHolder(TopperPlaceholderLeaderboard instance, String name, Value

this.storageAgent = new StorageAgent<>(instance.getLogger(), instance.get(TopManager.class).getStorageSupplier().getStorage(this));
storageAgent.setMaxEntryPerCall(instance.get(MainConfig.class).getTaskSaveEntryPerTick());
storageAgent.addLoadListener(() -> {
if (instance.get(MainConfig.class).isLoadAllOfflinePlayers()) {
GlobalScheduler.get(instance).run(() -> {
for (OfflinePlayer player : instance.getServer().getOfflinePlayers()) {
getOrCreateEntry(player.getUniqueId());
}
});
}
});
addAgent(new SpigotRunnableAgent<>(storageAgent, AsyncScheduler.get(instance), instance.get(MainConfig.class).getTaskSaveDelay()));

this.snapshotAgent = new SnapshotAgent<>(this);
snapshotAgent.setComparator(Comparator.reverseOrder());
addAgent(new SpigotRunnableAgent<>(snapshotAgent, AsyncScheduler.get(instance), 20L));

addAgent(new Agent<UUID, Double>() {
@Override
public void start() {
if (instance.get(MainConfig.class).isLoadAllOfflinePlayers()) {
GlobalScheduler.get(instance).run(() -> {
for (OfflinePlayer player : instance.getServer().getOfflinePlayers()) {
getOrCreateEntry(player.getUniqueId());
}
});
}
}

@Override
public void onUpdate(DataEntry<UUID, Double> entry) {
UpdateNotificationManager.notifyConsumers(name, entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.hsgamer.topper.placeholderleaderboard.manager;

import io.github.projectunified.minelib.plugin.base.Loadable;
import me.hsgamer.topper.agent.storage.supplier.DataStorageSupplier;
import me.hsgamer.topper.agent.storage.simple.supplier.DataStorageSupplier;
import me.hsgamer.topper.placeholderleaderboard.TopperPlaceholderLeaderboard;
import me.hsgamer.topper.placeholderleaderboard.config.MainConfig;
import me.hsgamer.topper.placeholderleaderboard.holder.NumberTopHolder;
Expand Down

0 comments on commit 6fabd03

Please sign in to comment.