refactoring

This commit is contained in:
James Seibel
2023-03-11 21:10:42 -06:00
parent 586c550c80
commit 6056d7f7d1
3 changed files with 28 additions and 20 deletions
@@ -469,7 +469,7 @@ public class FullDataMetaFile extends AbstractMetaDataContainerFile
// wait for the tracker to be garbage collected(?)
while (phantom != null)
{
LOGGER.info("Full Data at pos: "+phantom.pos+" has been freed. "+lifeCycleDebugSet.size()+" Full Data files remaining.");
//LOGGER.info("Full Data at pos: "+phantom.pos+" has been freed. "+lifeCycleDebugSet.size()+" Full Data files remaining.");
phantom.close();
phantom = (DataObjTracker) lifeCycleDebugQueue.poll();
}
@@ -82,10 +82,17 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler
// confirm the quad tree has at least one node in it
LodUtil.assertTrue(!missingPositions.isEmpty() || !existingFiles.isEmpty());
// determine the type of dataSource that should be used for this position
IIncompleteFullDataSource dataSource = pos.sectionDetailLevel <= SparseFullDataSource.MAX_SECTION_DETAIL ?
SparseFullDataSource.createEmpty(pos) :
SingleChunkFullDataSource.createEmpty(pos);
IIncompleteFullDataSource incompleteFullDataSource;
if (pos.sectionDetailLevel <= SparseFullDataSource.MAX_SECTION_DETAIL)
{
incompleteFullDataSource = SparseFullDataSource.createEmpty(pos);
}
else
{
incompleteFullDataSource = SingleChunkFullDataSource.createEmpty(pos);
}
if (missingPositions.size() == 1 && existingFiles.isEmpty() && missingPositions.get(0).equals(pos))
@@ -96,13 +103,13 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler
if (worldGenQueue != null)
{
// queue this section to be generated
GenTask genTask = new GenTask(pos, new WeakReference<>(dataSource));
worldGenQueue.submitGenTask(dataSource.getSectionPos().getSectionBBoxPos(), dataSource.getDataDetail(), genTask)
GenTask genTask = new GenTask(pos, new WeakReference<>(incompleteFullDataSource));
worldGenQueue.submitGenTask(incompleteFullDataSource.getSectionPos().getSectionBBoxPos(), incompleteFullDataSource.getDataDetail(), genTask)
.whenComplete((genTaskCompleted, ex) -> this.onWorldGenTaskComplete(genTaskCompleted, ex, genTask, pos));
}
// return the empty dataSource (it will be populated later)
return CompletableFuture.completedFuture(dataSource);
return CompletableFuture.completedFuture(incompleteFullDataSource);
}
else
{
@@ -121,23 +128,24 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler
LOGGER.debug("Creating {} from sampling {} files: {}", pos, existingFiles.size(), existingFiles);
// read in the existing data
final ArrayList<CompletableFuture<Void>> futures = new ArrayList<>(existingFiles.size());
final ArrayList<CompletableFuture<Void>> loadDataFutures = new ArrayList<>(existingFiles.size());
for (FullDataMetaFile existingFile : existingFiles)
{
futures.add(existingFile.loadOrGetCachedAsync()
loadDataFutures.add(existingFile.loadOrGetCachedAsync()
.exceptionally((ex) -> /*Ignore file read errors*/null)
.thenAccept((data) ->
{
if (data != null)
{
//LOGGER.info("Merging data from {} into {}", data.getSectionPos(), pos);
dataSource.sampleFrom(data);
incompleteFullDataSource.sampleFrom(data);
}
})
);
}
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.thenApply((voidValue) -> dataSource.trySelfPromote());
return CompletableFuture.allOf(loadDataFutures.toArray(new CompletableFuture[0]))
.thenApply((voidValue) -> incompleteFullDataSource.trySelfPromote());
}
}
@@ -186,19 +186,19 @@ public class MovableGridRingList<T> extends ArrayList<T> implements List<T>
/** see {@link MovableGridRingList#clear(Consumer)} for full documentation */
@Override
public void clear() { this.clear(null); }
/** @param consumer the consumer run on each item before it is removed from the list */
public void clear(Consumer<? super T> consumer)
/** @param removedItemConsumer the consumer run on each item before it is removed from the list */
public void clear(Consumer<? super T> removedItemConsumer)
{
this.moveLock.writeLock().lock();
try
{
if (consumer != null)
if (removedItemConsumer != null)
{
super.forEach((item) ->
{
if (item != null)
{
consumer.accept(item);
removedItemConsumer.accept(item);
}
});
}
@@ -222,7 +222,7 @@ public class MovableGridRingList<T> extends ArrayList<T> implements List<T>
/** see {@link MovableGridRingList#moveTo(int, int, Consumer)} for full documentation */
public boolean moveTo(int newCenterX, int newCenterY) { return this.moveTo(newCenterX, newCenterY, null); }
/** Returns true if the grid was successfully moved, false otherwise */
public boolean moveTo(int newCenterX, int newCenterY, Consumer<? super T> consumer)
public boolean moveTo(int newCenterX, int newCenterY, Consumer<? super T> removedItemConsumer)
{
Pos2D cPos = this.minPosRef.get();
int newMinX = newCenterX - this.halfSize;
@@ -248,7 +248,7 @@ public class MovableGridRingList<T> extends ArrayList<T> implements List<T>
// and update the pos
if (Math.abs(deltaX) >= this.size || Math.abs(deltaY) >= this.size)
{
this.clear(consumer);
this.clear(removedItemConsumer);
}
else
{
@@ -262,9 +262,9 @@ public class MovableGridRingList<T> extends ArrayList<T> implements List<T>
|| y - deltaY >= this.size)
{
T item = this._swapUnsafe(x+cPos.x, y+cPos.y, null);
if (item != null && consumer != null)
if (item != null && removedItemConsumer != null)
{
consumer.accept(item);
removedItemConsumer.accept(item);
}
}
}