Skip to content

Commit

Permalink
feat(controller): add clear world data if
Browse files Browse the repository at this point in the history
  • Loading branch information
Xzavier0722 committed Jan 16, 2024
1 parent fc2c523 commit 002d0eb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ protected void scheduleReadTask(Runnable run) {
readExecutor.submit(run);
}

protected void scheduleWriteTask(Runnable run) {
checkDestroy();
writeExecutor.submit(run);
}

protected List<RecordSet> getData(RecordKey key) {
return getData(key, false);
}
Expand All @@ -180,6 +185,13 @@ protected void deleteData(RecordKey key) {
dataAdapter.deleteData(key);
}

protected void abortScopeTask(ScopeKey key) {
var task = scheduledWriteTasks.remove(key);
if (task != null) {
task.abort();
}
}

public final DataType getDataType() {
return dataType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,56 @@ public Set<SlimefunChunkData> getAllLoadedChunkData() {
return new HashSet<>(loadedChunk.values());
}

public void clearWorldData(World world) {
// 1. remove cache
public void removeAllDataInWorld(World world) {
// 1. remove block cache
var loadedBlockData = new HashSet<SlimefunBlockData>();
for (var chunkData : getAllLoadedChunkData(world)) {
loadedBlockData.addAll(chunkData.getAllBlockData());
chunkData.removeAllCacheInternal();
}

// 2. remove ticker and delayed tasks
for (var blockData : loadedBlockData) {
if (Slimefun.getRegistry().getTickerBlocks().contains(blockData.getSfId())) {
Slimefun.getTickerTask().disableTicker(blockData.getLocation());
}

var scopeKey = new LocationKey(DataScope.NONE, blockData.getLocation());
removeDelayedBlockDataUpdates(scopeKey);
abortScopeTask(scopeKey);
}

// 3. remove from database
var prefix = world.getName() + ";";
var condition = prefix + "%";
var req = new RecordKey(DataScope.BLOCK_DATA);
req.addCondition(FieldKey.CHUNK, condition);
deleteData(req);

req = new RecordKey(DataScope.CHUNK_DATA);
req.addCondition(FieldKey.CHUNK, condition);
deleteData(req);

// 4. remove chunk cache
loadedChunk.entrySet().removeIf(entry -> entry.getKey().startsWith(prefix));
}

public void removeAllDataInWorldAsync(World world, Runnable onFinishedCallback) {
scheduleWriteTask(() -> {
removeAllDataInWorld(world);
onFinishedCallback.run();
});
}

public Set<SlimefunChunkData> getAllLoadedChunkData(World world) {
var prefix = world.getName() + ";";
var re = new HashSet<SlimefunChunkData>();
loadedChunk.forEach((k, v) -> {
if (k.startsWith(prefix)) {
re.add(v);
}
});
return re;
}

private void scheduleDelayedBlockInvUpdate(SlimefunBlockData blockData, int slot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ Set<SlimefunBlockData> getAllCacheInternal() {
return re;
}

void removeAllCacheInternal() {
sfBlocks.clear();
}

boolean hasBlockCache(String lKey) {
return sfBlocks.containsKey(lKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ public class QueuedWriteTask implements Runnable {
private final Queue<RecordKey> queue = new LinkedList<>();
private final Map<RecordKey, Runnable> tasks = new HashMap<>();
private volatile boolean done = false;
private volatile boolean aborted = false;

@Override
public final void run() {
if (aborted) {
return;
}

var task = next();
while (task != null) {
while (!aborted && task != null) {
try {
task.run();
} catch (Throwable e) {
Expand All @@ -35,7 +40,7 @@ protected void onSuccess() {}
protected void onError(Throwable e) {}

public synchronized boolean queue(RecordKey key, Runnable next) {
if (done) {
if (done || aborted) {
return false;
}

Expand All @@ -45,6 +50,10 @@ public synchronized boolean queue(RecordKey key, Runnable next) {
return true;
}

public void abort() {
aborted = true;
}

private synchronized Runnable next() {
var key = queue.poll();
if (key == null) {
Expand Down

0 comments on commit 002d0eb

Please sign in to comment.