Minor file handler refactor

This commit is contained in:
James Seibel
2024-02-10 18:08:08 -06:00
parent 89ea4c5172
commit 9ccbc792b4
3 changed files with 47 additions and 24 deletions
@@ -17,6 +17,7 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Timer;
import java.util.TimerTask;
@@ -187,8 +188,6 @@ public abstract class AbstractDataSourceHandler<TDataSource extends IDataSource<
@Override
public CompletableFuture<Void> updateDataSourcesWithChunkDataAsync(ChunkSizedFullDataAccessor chunkDataView)
{
DhSectionPos pos = chunkDataView.getSectionPos().convertNewToDetailLevel(DhSectionPos.SECTION_MINIMUM_DETAIL_LEVEL);
ThreadPoolExecutor executor = ThreadPools.getFileHandlerExecutor();
if (executor == null || executor.isTerminated())
{
@@ -199,7 +198,15 @@ public abstract class AbstractDataSourceHandler<TDataSource extends IDataSource<
try
{
// run file handling on a separate thread
return CompletableFuture.runAsync(() -> this.updateDataSourcesRecursively(pos, chunkDataView), executor);
return CompletableFuture.runAsync(() ->
{
DhSectionPos bottomPos = chunkDataView.getSectionPos().convertNewToDetailLevel(DhSectionPos.SECTION_MINIMUM_DETAIL_LEVEL);
bottomPos.forEachPosUpToDetailLevel(
this.topSectionDetailLevelRef.byteValue(),
(pos) -> this.updateDataSourceAtPos(pos, chunkDataView) );
}, executor);
}
catch (RejectedExecutionException ignore)
{
@@ -207,26 +214,6 @@ public abstract class AbstractDataSourceHandler<TDataSource extends IDataSource<
return CompletableFuture.completedFuture(null);
}
}
/** Updates every data source from this position up to {@link AbstractDataSourceHandler#topSectionDetailLevelRef} */
private void updateDataSourcesRecursively(DhSectionPos pos, ChunkSizedFullDataAccessor chunkDataView)
{
// update up until we reach the highest available data source
if (pos.getDetailLevel() > this.topSectionDetailLevelRef.get())
{
return;
}
DhSectionPos chunkSectionPos = chunkDataView.getSectionPos();
LodUtil.assertTrue(chunkSectionPos.overlapsExactly(pos), "Update failed, chunk [" + chunkSectionPos + "] does not overlap section [" + pos + "].");
// update this pos
this.updateDataSourceAtPos(pos, chunkDataView);
// recursively update the parent pos
DhSectionPos parentPos = pos.getParentPos();
this.updateDataSourcesRecursively(parentPos, chunkDataView);
}
protected void updateDataSourceAtPos(DhSectionPos pos, ChunkSizedFullDataAccessor chunkData)
{
// a lock is necessary to prevent two threads from writing to the same position at once,
@@ -89,7 +89,11 @@ public class FullDataFileHandler extends AbstractDataSourceHandler<IFullDataSour
protected IFullDataSource createNewDataSourceFromExistingDtos(DhSectionPos pos)
{
IIncompleteFullDataSource newFullDataSource = this.makeEmptyDataSource(pos);
return this.updateFromDataSourceFromExistingDtos(newFullDataSource);
}
protected IFullDataSource updateFromDataSourceFromExistingDtos(IIncompleteFullDataSource newFullDataSource)
{
DhSectionPos pos = newFullDataSource.getSectionPos();
boolean showFullDataFileSampling = Config.Client.Advanced.Debugging.DebugWireframe.showFullDataFileStatus.get();
if (showFullDataFileSampling)
@@ -163,6 +167,8 @@ public class FullDataFileHandler extends AbstractDataSourceHandler<IFullDataSour
return newFullDataSource.tryPromotingToCompleteDataSource();
}
@Override
protected IIncompleteFullDataSource makeEmptyDataSource(DhSectionPos pos)
{
@@ -25,6 +25,7 @@ import com.seibel.distanthorizons.core.util.LodUtil;
import org.jetbrains.annotations.Nullable;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* The position object used to define LOD objects in the quad trees. <br><br>
@@ -314,6 +315,21 @@ public class DhSectionPos
}
}
/** Applies the given consumer to all children of the position at the given section detail level. */
public void forEachChildDownToDetailLevel(byte minSectionDetailLevel, Function<DhSectionPos, Boolean> callback)
{
boolean stop = callback.apply(this);
if (stop || minSectionDetailLevel == this.detailLevel)
{
return;
}
for (int i = 0; i < 4; i++)
{
this.getChildByIndex(i).forEachChildDownToDetailLevel(minSectionDetailLevel, callback);
}
}
/** Applies the given consumer to all children of the position at the given section detail level. */
public void forEachChildAtLevel(byte sectionDetailLevel, Consumer<DhSectionPos> callback)
{
@@ -329,6 +345,20 @@ public class DhSectionPos
}
}
/** Applies the given consumer to all children of the position at the given section detail level. */
public void forEachPosUpToDetailLevel(byte maxSectionDetailLevel, Consumer<DhSectionPos> callback)
{
callback.accept(this);
if (maxSectionDetailLevel == this.detailLevel)
{
return;
}
this.getParentPos().forEachPosUpToDetailLevel(maxSectionDetailLevel, callback);
}
//===============//