Skip to content

Commit df71ed9

Browse files
committed
option to load storage on entry create
1 parent 96d46d6 commit df71ed9

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class StorageAgent<K, V> implements Agent, DataEntryAgent<K, V>, Runnable
2727
private final AtomicReference<Map<K, ValueWrapper<V>>> savingMap = new AtomicReference<>();
2828
private final AtomicBoolean saving = new AtomicBoolean(false);
2929
private int maxEntryPerCall = 10;
30+
private boolean lazyLoad = false;
3031

3132
public StorageAgent(DataHolder<K, V> holder, DataStorage<K, V> storage) {
3233
this.holder = holder;
@@ -96,10 +97,12 @@ private void save(boolean urgent) {
9697
@Override
9798
public void start() {
9899
storage.onRegister();
99-
try {
100-
storage.load().forEach((uuid, value) -> holder.getOrCreateEntry(uuid).setValue(value, false));
101-
} catch (Exception e) {
102-
LOGGER.log(LogLevel.ERROR, "Failed to load top entries for " + holder.getName(), e);
100+
if (!lazyLoad) {
101+
try {
102+
storage.load().forEach((uuid, value) -> holder.getOrCreateEntry(uuid).setValue(value, false));
103+
} catch (Exception e) {
104+
LOGGER.log(LogLevel.ERROR, "Failed to load top entries for " + holder.getName(), e);
105+
}
103106
}
104107
}
105108

@@ -113,6 +116,13 @@ public void beforeStop() {
113116
save(true);
114117
}
115118

119+
@Override
120+
public void onCreate(DataEntry<K, V> entry) {
121+
if (lazyLoad) {
122+
storage.load(entry.getKey()).ifPresent(value -> entry.setValue(value, false));
123+
}
124+
}
125+
116126
@Override
117127
public void onUpdate(DataEntry<K, V> entry, V oldValue, V newValue) {
118128
storeMap.get().put(entry.getKey(), new ValueWrapper<>(newValue));
@@ -136,6 +146,10 @@ public void setMaxEntryPerCall(int taskSaveEntryPerTick) {
136146
this.maxEntryPerCall = taskSaveEntryPerTick;
137147
}
138148

149+
public void setLazyLoad(boolean lazyLoad) {
150+
this.lazyLoad = lazyLoad;
151+
}
152+
139153
private static final class ValueWrapper<V> {
140154
private final @Nullable V value;
141155

0 commit comments

Comments
 (0)