Skip to content

Commit

Permalink
Make RunnableAgent a separated Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jun 17, 2024
1 parent cd847e0 commit 7d31d26
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@

import me.hsgamer.topper.core.agent.Agent;

import java.util.function.UnaryOperator;

public abstract class RunnableAgent implements Agent {
private UnaryOperator<Runnable> runTaskFunction;
public abstract class RunnableAgent<A extends Agent & Runnable> implements Agent {
private final A agent;
private Runnable cancelTaskRunnable;

protected abstract Runnable getRunnable();
public RunnableAgent(A agent) {
this.agent = agent;
}

/**
* Set the function to run the task.
* The function provides a {@link Runnable} and should return a {@link Runnable} that cancels the task.
* This method is used to run the task and return a {@link Runnable} to cancel it
*
* @param runTaskFunction the function to run the task
* @param runnable the task to run
* @return the {@link Runnable} to cancel the task
*/
public void setRunTaskFunction(UnaryOperator<Runnable> runTaskFunction) {
this.runTaskFunction = runTaskFunction;
}
protected abstract Runnable run(Runnable runnable);

@Override
public void start() {
if (runTaskFunction == null) {
throw new IllegalStateException("runTaskFunction is null");
}
cancelTaskRunnable = runTaskFunction.apply(getRunnable());
agent.start();
cancelTaskRunnable = run(agent);
}

@Override
public void stop() {
if (cancelTaskRunnable == null) return;
cancelTaskRunnable.run();
if (cancelTaskRunnable != null) {
cancelTaskRunnable.run();
}
agent.stop();
}

public A getAgent() {
return agent;
}
}
8 changes: 0 additions & 8 deletions agent/agent-snapshot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,4 @@
</parent>

<artifactId>topper-agent-snapshot</artifactId>

<dependencies>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>topper-agent-runnable</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.hsgamer.topper.agent.snapshot;

import me.hsgamer.topper.agent.runnable.RunnableAgent;
import me.hsgamer.topper.core.agent.Agent;
import me.hsgamer.topper.core.entry.DataEntry;
import me.hsgamer.topper.core.holder.DataHolder;

Expand All @@ -11,33 +11,31 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class SnapshotAgent<K, V> extends RunnableAgent {
public class SnapshotAgent<K, V> implements Agent, Runnable {
private final AtomicReference<List<DataSnapshot<K, V>>> topSnapshot = new AtomicReference<>(Collections.emptyList());
private final AtomicReference<Map<K, Integer>> indexMap = new AtomicReference<>(Collections.emptyMap());
private final DataHolder<K, V> holder;
private final List<Predicate<DataSnapshot<K, V>>> filters = new ArrayList<>();
private Comparator<V> comparator;

public SnapshotAgent(DataHolder<K, V> holder) {
super();
this.holder = holder;
}

@Override
protected Runnable getRunnable() {
return () -> {
List<DataSnapshot<K, V>> list = getUrgentSnapshot();
topSnapshot.set(getUrgentSnapshot());
public void run() {
List<DataSnapshot<K, V>> list = getUrgentSnapshot();
topSnapshot.set(getUrgentSnapshot());

Map<K, Integer> map = IntStream.range(0, list.size())
.boxed()
.collect(Collectors.toMap(i -> list.get(i).key, i -> i));
indexMap.set(map);
};
Map<K, Integer> map = IntStream.range(0, list.size())
.boxed()
.collect(Collectors.toMap(i -> list.get(i).key, i -> i));
indexMap.set(map);
}

@Override
public void stop() {
super.stop();
topSnapshot.set(Collections.emptyList());
indexMap.set(Collections.emptyMap());
}
Expand All @@ -47,7 +45,7 @@ public List<DataSnapshot<K, V>> getUrgentSnapshot() {
.map(entry -> new DataSnapshot<>(entry.getKey(), entry.getValue().getValue()))
.filter(snapshot -> filters.stream().allMatch(filter -> filter.test(snapshot)));
if (comparator != null) {
stream = stream.sorted(Comparator.<DataSnapshot<K, V>, V>comparing(snapshot -> snapshot.value, comparator));
stream = stream.sorted(Comparator.comparing(snapshot -> snapshot.value, comparator));
}
return stream.collect(Collectors.toList());
}
Expand Down
8 changes: 0 additions & 8 deletions agent/agent-storage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,4 @@
</parent>

<artifactId>topper-agent-storage</artifactId>

<dependencies>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>topper-agent-runnable</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.hsgamer.topper.agent.storage;

import me.hsgamer.topper.agent.runnable.RunnableAgent;
import me.hsgamer.topper.agent.storage.supplier.DataStorage;
import me.hsgamer.topper.core.agent.Agent;
import me.hsgamer.topper.core.entry.DataEntry;
import me.hsgamer.topper.core.flag.EntryTempFlag;
import me.hsgamer.topper.core.holder.DataHolder;
Expand All @@ -14,7 +14,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

public class StorageAgent<K, V> extends RunnableAgent {
public class StorageAgent<K, V> implements Agent, Runnable {
public static final EventState LOAD_EVENT = EventState.newState();
public static final EntryTempFlag NEED_SAVING = new EntryTempFlag("needSaving");
public static final EntryTempFlag IS_SAVING = new EntryTempFlag("isSaving");
Expand All @@ -28,6 +28,7 @@ public class StorageAgent<K, V> extends RunnableAgent {
private boolean urgentLoad = true;

public StorageAgent(Logger logger, DataStorage<K, V> storage) {
super();
this.logger = logger;
this.holder = storage.getHolder();
this.storage = storage;
Expand Down Expand Up @@ -79,12 +80,10 @@ public void start() {
}
holder.getListenerManager().call(LOAD_EVENT);
});
super.start();
}

@Override
public void stop() {
super.stop();
storage.onUnregister();
}

Expand All @@ -94,20 +93,18 @@ public void beforeStop() {
}

@Override
protected Runnable getRunnable() {
return () -> {
List<K> list = new ArrayList<>();
for (int i = 0; i < maxEntryPerCall; i++) {
K k = saveQueue.poll();
if (k == null) break;
DataEntry<K, V> entry = holder.getOrCreateEntry(k);
save(entry);
list.add(k);
}
if (!list.isEmpty()) {
saveQueue.addAll(list);
}
};
public void run() {
List<K> list = new ArrayList<>();
for (int i = 0; i < maxEntryPerCall; i++) {
K k = saveQueue.poll();
if (k == null) break;
DataEntry<K, V> entry = holder.getOrCreateEntry(k);
save(entry);
list.add(k);
}
if (!list.isEmpty()) {
saveQueue.addAll(list);
}
}

public void setMaxEntryPerCall(int maxEntryPerCall) {
Expand Down
8 changes: 0 additions & 8 deletions agent/agent-update/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,4 @@
</parent>

<artifactId>topper-agent-update</artifactId>

<dependencies>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>topper-agent-runnable</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.hsgamer.topper.agent.agent.update;

import me.hsgamer.topper.agent.runnable.RunnableAgent;
import me.hsgamer.topper.core.agent.Agent;
import me.hsgamer.topper.core.entry.DataEntry;
import me.hsgamer.topper.core.flag.EntryTempFlag;
import me.hsgamer.topper.core.holder.DataHolder;
Expand All @@ -13,7 +13,7 @@
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Function;

public class UpdateAgent<K, V> extends RunnableAgent {
public class UpdateAgent<K, V> implements Agent, Runnable {
public static final EntryTempFlag IS_UPDATING = new EntryTempFlag("isUpdating");
public static final EntryTempFlag IGNORE_UPDATE = new EntryTempFlag("ignoreUpdate");
private final Queue<K> updateQueue = new ConcurrentLinkedQueue<>();
Expand All @@ -22,6 +22,7 @@ public class UpdateAgent<K, V> extends RunnableAgent {
private Function<K, CompletableFuture<Optional<V>>> updateFunction;

public UpdateAgent(DataHolder<K, V> holder) {
super();
this.holder = holder;
}

Expand All @@ -34,22 +35,20 @@ public void setUpdateFunction(Function<K, CompletableFuture<Optional<V>>> update
}

@Override
protected Runnable getRunnable() {
return () -> {
List<K> list = new ArrayList<>();
for (int i = 0; i < maxEntryPerCall; i++) {
K k = updateQueue.poll();
if (k == null) {
break;
}
DataEntry<K, V> entry = holder.getOrCreateEntry(k);
updateEntry(entry);
list.add(k);
public void run() {
List<K> list = new ArrayList<>();
for (int i = 0; i < maxEntryPerCall; i++) {
K k = updateQueue.poll();
if (k == null) {
break;
}
if (!list.isEmpty()) {
updateQueue.addAll(list);
}
};
DataEntry<K, V> entry = holder.getOrCreateEntry(k);
updateEntry(entry);
list.add(k);
}
if (!list.isEmpty()) {
updateQueue.addAll(list);
}
}

@Override
Expand All @@ -59,7 +58,6 @@ public void start() {
}
holder.getListenerManager().add(DataHolder.EventStates.CREATE, entry -> updateQueue.add(entry.getKey()));
holder.getListenerManager().add(DataHolder.EventStates.REMOVE, entry -> updateQueue.remove(entry.getKey()));
super.start();
}

private void updateEntry(DataEntry<K, V> entry) {
Expand Down
8 changes: 6 additions & 2 deletions core/src/main/java/me/hsgamer/topper/core/agent/Agent.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package me.hsgamer.topper.core.agent;

public interface Agent {
void start();
default void start() {
// EMPTY
}

void stop();
default void stop() {
// EMPTY
}

default void beforeStop() {
// EMPTY
Expand Down
1 change: 1 addition & 0 deletions spigot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<packaging>pom</packaging>
<modules>
<module>spigot-config</module>
<module>spigot-agent-runnable</module>
<module>spigot-agent-storage-simple</module>
<module>spigot-number</module>
<module>spigot-block</module>
Expand Down
26 changes: 26 additions & 0 deletions spigot/spigot-agent-runnable/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>me.hsgamer</groupId>
<artifactId>topper-spigot</artifactId>
<version>3.0.0-SNAPSHOT</version>
</parent>

<artifactId>topper-spigot-agent-runnable</artifactId>

<dependencies>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>topper-agent-runnable</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.projectunified</groupId>
<artifactId>minelib-scheduler-common</artifactId>
<version>${minelib.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.hsgamer.topper.spigot.agent.runnable;

import io.github.projectunified.minelib.scheduler.common.scheduler.Scheduler;
import io.github.projectunified.minelib.scheduler.common.task.Task;
import me.hsgamer.topper.agent.runnable.RunnableAgent;
import me.hsgamer.topper.core.agent.Agent;

public class SpigotRunnableAgent<A extends Agent & Runnable> extends RunnableAgent<A> {
private final Scheduler scheduler;
private final long interval;

public SpigotRunnableAgent(A agent, Scheduler scheduler, long interval) {
super(agent);
this.scheduler = scheduler;
this.interval = interval;
}

@Override
protected Runnable run(Runnable runnable) {
Task task = scheduler.runTimer(runnable, interval, interval);
return task::cancel;
}
}
5 changes: 5 additions & 0 deletions spigot/spigot-placeholder-leaderboard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@
<artifactId>topper-agent-snapshot</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>topper-spigot-agent-runnable</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.bstats</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ default int getTaskUpdateEntryPerTick() {
return 10;
}

@ConfigPath(value = {"task", "update", "delay"}, priority = 5)
@ConfigPath(value = {"task", "update", "delay"}, priority = 5)
@Comment("How many ticks should the plugin wait before updating the leaderboard")
default int getTaskUpdateDelay() {
return 0;
Expand Down
Loading

0 comments on commit 7d31d26

Please sign in to comment.