Skip to content

Commit 0b35532

Browse files
committed
remove CompletableFuture from DataStorage
1 parent 540d9c9 commit 0b35532

File tree

7 files changed

+124
-197
lines changed

7 files changed

+124
-197
lines changed

agent/storage/src/main/java/me/hsgamer/topper/agent/storage/StorageAgent.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,24 @@ private void save(boolean urgent) {
6363
}
6464
}
6565

66-
storage.save(finalMap, urgent)
67-
.thenCompose(v -> {
68-
if (removeKeys.isEmpty()) {
69-
return CompletableFuture.completedFuture(null);
70-
} else {
71-
return storage.remove(removeKeys, urgent);
72-
}
73-
})
74-
.whenComplete((v, throwable) -> {
75-
if (throwable != null) {
76-
logger.log(Level.SEVERE, "Failed to save entries for " + holder.getName(), throwable);
77-
} else {
78-
savingMap.set(null);
79-
}
80-
saving.set(false);
81-
});
66+
Runnable saveTask = () -> {
67+
try {
68+
storage.save(finalMap);
69+
if (!removeKeys.isEmpty()) {
70+
storage.remove(removeKeys);
71+
}
72+
savingMap.set(null);
73+
} catch (Throwable t) {
74+
logger.log(Level.SEVERE, "Failed to save entries for " + holder.getName(), t);
75+
}
76+
saving.set(false);
77+
};
78+
79+
if (urgent) {
80+
saveTask.run();
81+
} else {
82+
CompletableFuture.runAsync(saveTask);
83+
}
8284
}
8385

8486
@Override

spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/TopperPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected List<Object> getComponents() {
4949
@Override
5050
public void load() {
5151
MessageUtils.setPrefix(get(MessageConfig.class)::getPrefix);
52-
SpigotDataStorageBuilder.register(this, get(DataStorageBuilder.class));
52+
SpigotDataStorageBuilder.register(get(DataStorageBuilder.class));
5353
}
5454

5555
@Override
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package me.hsgamer.topper.spigot.storage.simple;
22

3-
import io.github.projectunified.minelib.scheduler.global.GlobalScheduler;
43
import me.hsgamer.hscore.bukkit.config.BukkitConfig;
54
import me.hsgamer.topper.spigot.storage.simple.supplier.ConfigStorageSupplier;
65
import me.hsgamer.topper.storage.simple.builder.DataStorageBuilder;
7-
import org.bukkit.plugin.java.JavaPlugin;
8-
9-
import java.util.concurrent.Executor;
106

117
public class SpigotDataStorageBuilder {
12-
public static void register(JavaPlugin plugin, DataStorageBuilder builder) {
13-
Executor mainThreadExecutor = runnable -> GlobalScheduler.get(plugin).run(runnable);
14-
15-
builder.register(setting -> new ConfigStorageSupplier(mainThreadExecutor, name -> name + ".yml", BukkitConfig::new, setting.getBaseFolder()), "config", "yaml", "yml");
16-
builder.register(setting -> new ConfigStorageSupplier(mainThreadExecutor, name -> name + ".json", BukkitConfig::new, setting.getBaseFolder()), "json");
8+
public static void register(DataStorageBuilder builder) {
9+
builder.register(setting -> new ConfigStorageSupplier(name -> name + ".yml", BukkitConfig::new, setting.getBaseFolder()), "config", "yaml", "yml");
10+
builder.register(setting -> new ConfigStorageSupplier(name -> name + ".json", BukkitConfig::new, setting.getBaseFolder()), "json");
1711
}
1812
}

spigot/storage-simple/src/main/java/me/hsgamer/topper/spigot/storage/simple/supplier/ConfigStorageSupplier.java

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,19 @@
1111
import java.util.HashMap;
1212
import java.util.Map;
1313
import java.util.Optional;
14-
import java.util.concurrent.CompletableFuture;
15-
import java.util.concurrent.Executor;
1614
import java.util.function.Function;
1715
import java.util.function.UnaryOperator;
1816

1917
public class ConfigStorageSupplier implements DataStorageSupplier {
20-
private final Executor mainThreadExecutor;
2118
private final UnaryOperator<String> configNameProvider;
2219
private final Function<File, Config> configProvider;
2320
private final File holderBaseFolder;
2421

2522
public ConfigStorageSupplier(
26-
Executor mainThreadExecutor,
2723
UnaryOperator<String> configNameProvider,
2824
Function<File, Config> configProvider,
2925
File holderBaseFolder
3026
) {
31-
this.mainThreadExecutor = mainThreadExecutor;
3227
this.configNameProvider = configNameProvider;
3328
this.configProvider = configProvider;
3429
this.holderBaseFolder = holderBaseFolder;
@@ -56,35 +51,20 @@ public Map<K, V> load() {
5651
}
5752

5853
@Override
59-
public CompletableFuture<Void> save(Map<K, V> map, boolean urgent) {
60-
return CompletableFuture.supplyAsync(
61-
() -> {
62-
map.forEach((key, value) -> config.set(converter.toRawValue(value), converter.toRawKey(key)));
63-
config.save();
64-
return null;
65-
},
66-
urgent ? Runnable::run : mainThreadExecutor
67-
);
54+
public void save(Map<K, V> map) {
55+
map.forEach((key, value) -> config.set(converter.toRawValue(value), converter.toRawKey(key)));
56+
config.save();
6857
}
6958

7059
@Override
71-
public CompletableFuture<Optional<V>> load(K key, boolean urgent) {
72-
return CompletableFuture.supplyAsync(
73-
() -> Optional.ofNullable(config.get(converter.toRawKey(key))).map(String::valueOf).map(converter::toValue),
74-
urgent ? Runnable::run : mainThreadExecutor
75-
);
60+
public Optional<V> load(K key) {
61+
return Optional.ofNullable(config.get(converter.toRawKey(key))).map(String::valueOf).map(converter::toValue);
7662
}
7763

7864
@Override
79-
public CompletableFuture<Void> remove(Collection<K> keys, boolean urgent) {
80-
return CompletableFuture.supplyAsync(
81-
() -> {
82-
keys.forEach(key -> config.remove(converter.toRawKey(key)));
83-
config.save();
84-
return null;
85-
},
86-
urgent ? Runnable::run : mainThreadExecutor
87-
);
65+
public void remove(Collection<K> keys) {
66+
keys.forEach(key -> config.remove(converter.toRawKey(key)));
67+
config.save();
8868
}
8969

9070
@Override

storage/core/src/main/java/me/hsgamer/topper/storage/core/DataStorage.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
import java.util.Collection;
44
import java.util.Map;
55
import java.util.Optional;
6-
import java.util.concurrent.CompletableFuture;
76

87
public interface DataStorage<K, V> {
98
Map<K, V> load();
109

11-
CompletableFuture<Void> save(Map<K, V> map, boolean urgent);
10+
void save(Map<K, V> map);
1211

13-
CompletableFuture<Optional<V>> load(K key, boolean urgent);
12+
Optional<V> load(K key);
1413

15-
CompletableFuture<Void> remove(Collection<K> keys, boolean urgent);
14+
void remove(Collection<K> keys);
1615

1716
default void onRegister() {
1817
// EMPTY

storage/simple/src/main/java/me/hsgamer/topper/storage/simple/supplier/FlatStorageSupplier.java

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import java.io.FileOutputStream;
1313
import java.io.IOException;
1414
import java.util.*;
15-
import java.util.concurrent.CompletableFuture;
16-
import java.util.function.Supplier;
1715

1816
public class FlatStorageSupplier implements DataStorageSupplier {
1917
private final Logger logger = LoggerProvider.getLogger(getClass());
@@ -69,41 +67,20 @@ public Map<K, V> load() {
6967
}
7068

7169
@Override
72-
public CompletableFuture<Void> save(Map<K, V> map, boolean urgent) {
73-
Runnable runnable = () -> {
74-
map.forEach((k, v) -> properties.put(converter.toRawKey(k), converter.toRawValue(v)));
75-
saveRunnable.run();
76-
};
77-
if (urgent) {
78-
runnable.run();
79-
return CompletableFuture.completedFuture(null);
80-
} else {
81-
return CompletableFuture.runAsync(runnable);
82-
}
70+
public void save(Map<K, V> map) {
71+
map.forEach((k, v) -> properties.put(converter.toRawKey(k), converter.toRawValue(v)));
72+
saveRunnable.run();
8373
}
8474

8575
@Override
86-
public CompletableFuture<Optional<V>> load(K key, boolean urgent) {
87-
Supplier<Optional<V>> runnable = () -> Optional.ofNullable(properties.getProperty(converter.toRawKey(key))).map(converter::toValue);
88-
if (urgent) {
89-
return CompletableFuture.completedFuture(runnable.get());
90-
} else {
91-
return CompletableFuture.supplyAsync(runnable);
92-
}
76+
public Optional<V> load(K key) {
77+
return Optional.ofNullable(properties.getProperty(converter.toRawKey(key))).map(converter::toValue);
9378
}
9479

9580
@Override
96-
public CompletableFuture<Void> remove(Collection<K> keys, boolean urgent) {
97-
Runnable runnable = () -> {
98-
keys.forEach(key -> properties.remove(converter.toRawKey(key)));
99-
saveRunnable.run();
100-
};
101-
if (urgent) {
102-
runnable.run();
103-
return CompletableFuture.completedFuture(null);
104-
} else {
105-
return CompletableFuture.runAsync(runnable);
106-
}
81+
public void remove(Collection<K> keys) {
82+
keys.forEach(key -> properties.remove(converter.toRawKey(key)));
83+
saveRunnable.run();
10784
}
10885

10986
@Override

0 commit comments

Comments
 (0)