Skip to content

Commit

Permalink
feat(controller): add remove all data in chunk if
Browse files Browse the repository at this point in the history
  • Loading branch information
Xzavier0722 committed Jan 16, 2024
1 parent da6f41e commit 21b8c38
Showing 1 changed file with 41 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,23 @@ public Set<SlimefunChunkData> getAllLoadedChunkData() {
return new HashSet<>(loadedChunk.values());
}

public void removeAllDataInChunk(Chunk chunk) {
var cKey = LocationUtils.getChunkKey(chunk);
var cache = loadedChunk.remove(cKey);

if (cache != null && cache.isDataLoaded()) {
cache.getAllBlockData().forEach(this::clearBlockCacheAndTasks);
}
deleteChunkAndBlockDataDirectly(cKey);
}

public void removeAllDataInChunkAsync(Chunk chunk, Runnable onFinishedCallback) {
scheduleWriteTask(() -> {
removeAllDataInChunk(chunk);
onFinishedCallback.run();
});
}

public void removeAllDataInWorld(World world) {
// 1. remove block cache
var loadedBlockData = new HashSet<SlimefunBlockData>();
Expand All @@ -549,29 +566,11 @@ public void removeAllDataInWorld(World world) {
}

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

var scopeKey = new LocationKey(DataScope.NONE, l);
removeDelayedBlockDataUpdates(scopeKey);
abortScopeTask(scopeKey);
}
loadedBlockData.forEach(this::clearBlockCacheAndTasks);

// 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);
deleteChunkAndBlockDataDirectly(prefix + "%");

// 4. remove chunk cache
loadedChunk.entrySet().removeIf(entry -> entry.getKey().startsWith(prefix));
Expand Down Expand Up @@ -729,4 +728,26 @@ private SlimefunChunkData getChunkDataCache(Chunk chunk, boolean createOnNotExis
})
: loadedChunk.get(LocationUtils.getChunkKey(chunk));
}

private void deleteChunkAndBlockDataDirectly(String cKey) {
var req = new RecordKey(DataScope.BLOCK_DATA);
req.addCondition(FieldKey.CHUNK, cKey);
deleteData(req);

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

private void clearBlockCacheAndTasks(SlimefunBlockData blockData) {
var l = blockData.getLocation();
if (blockData.isDataLoaded() && Slimefun.getRegistry().getTickerBlocks().contains(blockData.getSfId())) {
Slimefun.getTickerTask().disableTicker(l);
}
Slimefun.getNetworkManager().updateAllNetworks(l);

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

0 comments on commit 21b8c38

Please sign in to comment.