Full Data source refactoring 2
This commit is contained in:
+21
-14
@@ -248,8 +248,9 @@ public class FullDataFileHandler implements IFullDataSourceProvider
|
||||
return null;
|
||||
}
|
||||
|
||||
// This is a CAS with expected null value.
|
||||
this.topDetailLevel.updateAndGet(oldDetailLevel -> Math.max(oldDetailLevel, pos.sectionDetailLevel));
|
||||
|
||||
// This is a CAS with expected null value.
|
||||
FullDataMetaFile metaFileCas = this.fileBySectionPos.putIfAbsent(pos, metaFile);
|
||||
return metaFileCas == null ? metaFile : metaFileCas;
|
||||
}
|
||||
@@ -266,6 +267,7 @@ public class FullDataFileHandler implements IFullDataSourceProvider
|
||||
byte sectionDetail = posAreaToGet.sectionDetailLevel;
|
||||
boolean allEmpty = true;
|
||||
|
||||
// get all existing files for this position
|
||||
outerLoop:
|
||||
while (--sectionDetail >= this.minDetailLevel)
|
||||
{
|
||||
@@ -439,28 +441,33 @@ public class FullDataFileHandler implements IFullDataSourceProvider
|
||||
HighDetailIncompleteFullDataSource.createEmpty(pos) :
|
||||
LowDetailIncompleteFullDataSource.createEmpty(pos);
|
||||
}
|
||||
|
||||
protected CompletableFuture<IIncompleteFullDataSource> sampleFromFiles(IIncompleteFullDataSource source, ArrayList<FullDataMetaFile> existingFiles)
|
||||
|
||||
/** populates the given data source using the given array of files */
|
||||
protected CompletableFuture<IIncompleteFullDataSource> sampleFromFileArray(IIncompleteFullDataSource recipientFullDataSource, ArrayList<FullDataMetaFile> existingFiles)
|
||||
{
|
||||
// read in the existing data
|
||||
final ArrayList<CompletableFuture<Void>> loadDataFutures = new ArrayList<>(existingFiles.size());
|
||||
for (FullDataMetaFile existingFile : existingFiles)
|
||||
{
|
||||
loadDataFutures.add(existingFile.loadOrGetCachedDataSourceAsync()
|
||||
.exceptionally((ex) -> /*Ignore file read errors*/null)
|
||||
.thenAccept((fullDataSource) ->
|
||||
.exceptionally((ex) -> /*Ignore file read errors*/null)
|
||||
.thenAccept((existingFullDataSource) ->
|
||||
{
|
||||
if (existingFullDataSource == null)
|
||||
{
|
||||
if (fullDataSource == null) return;
|
||||
//this.checkIfSectionNeedsAdditionalGeneration(pos, fullDataSource);
|
||||
//LOGGER.info("Merging data from {} into {}", data.getSectionPos(), pos);
|
||||
source.sampleFrom(fullDataSource);
|
||||
})
|
||||
return;
|
||||
}
|
||||
|
||||
//LOGGER.info("Merging data from {} into {}", data.getSectionPos(), pos);
|
||||
recipientFullDataSource.sampleFrom(existingFullDataSource);
|
||||
})
|
||||
);
|
||||
}
|
||||
return CompletableFuture.allOf(loadDataFutures.toArray(new CompletableFuture[0])).thenApply(v -> source);
|
||||
return CompletableFuture.allOf(loadDataFutures.toArray(new CompletableFuture[0])).thenApply(voidObj -> recipientFullDataSource);
|
||||
}
|
||||
|
||||
protected void makeFiles(ArrayList<DhSectionPos> posList, ArrayList<FullDataMetaFile> output) {
|
||||
protected void makeFiles(ArrayList<DhSectionPos> posList, ArrayList<FullDataMetaFile> output)
|
||||
{
|
||||
for (DhSectionPos missingPos : posList)
|
||||
{
|
||||
FullDataMetaFile newFile = this.getLoadOrMakeFile(missingPos, true);
|
||||
@@ -487,8 +494,8 @@ public class FullDataFileHandler implements IFullDataSourceProvider
|
||||
}
|
||||
else
|
||||
{
|
||||
makeFiles(missing, existFiles);
|
||||
return sampleFromFiles(source, existFiles).thenApply(IIncompleteFullDataSource::tryPromotingToCompleteDataSource)
|
||||
this.makeFiles(missing, existFiles);
|
||||
return this.sampleFromFileArray(source, existFiles).thenApply(IIncompleteFullDataSource::tryPromotingToCompleteDataSource)
|
||||
.exceptionally((e) ->
|
||||
{
|
||||
FullDataMetaFile newMetaFile = this.removeCorruptedFile(pos, file, e);
|
||||
|
||||
+26
-20
@@ -120,7 +120,7 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler
|
||||
ArrayList<FullDataMetaFile> existingFiles = new ArrayList<>();
|
||||
byte sectDetailLevel = (byte) (DhSectionPos.SECTION_MINIMUM_DETAIL_LEVEL + maxSectDataDetailLevel);
|
||||
pos.forEachChildAtLevel(sectDetailLevel, p -> existingFiles.add(getLoadOrMakeFile(p, true)));
|
||||
return sampleFromFiles(dataSource, existingFiles).thenApply(this::tryPromoteDataSource)
|
||||
return sampleFromFileArray(dataSource, existingFiles).thenApply(this::tryPromoteDataSource)
|
||||
.exceptionally((e) ->
|
||||
{
|
||||
FullDataMetaFile newMetaFile = removeCorruptedFile(pos, file, e);
|
||||
@@ -152,26 +152,28 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler
|
||||
|
||||
// Try update the gen queue on this data source. If null, then nothing was done.
|
||||
@Nullable
|
||||
private CompletableFuture<IFullDataSource> updateDataGenStatus(FullDataMetaFile file, IIncompleteFullDataSource data)
|
||||
private CompletableFuture<IFullDataSource> updateFromExistingDataSources(FullDataMetaFile file, IIncompleteFullDataSource data)
|
||||
{
|
||||
DhSectionPos pos = file.pos;
|
||||
ArrayList<FullDataMetaFile> existingFiles = new ArrayList<>();
|
||||
ArrayList<DhSectionPos> missingPositions = new ArrayList<>();
|
||||
this.getDataFilesForPosition(pos, pos, existingFiles, missingPositions);
|
||||
|
||||
if (missingPositions.size() == 1) {
|
||||
|
||||
if (missingPositions.size() == 1)
|
||||
{
|
||||
// Only missing myself. I.e. no child file data exists yet.
|
||||
return tryStartGenTask(file, data);
|
||||
return this.tryStartGenTask(file, data);
|
||||
}
|
||||
else {
|
||||
// Has stuff to sample.
|
||||
makeFiles(missingPositions, existingFiles);
|
||||
return sampleFromFiles(data, existingFiles).thenApply(this::tryPromoteDataSource)
|
||||
.exceptionally((e) ->
|
||||
{
|
||||
FullDataMetaFile newMetaFile = removeCorruptedFile(pos, file, e);
|
||||
return null;
|
||||
});
|
||||
else
|
||||
{
|
||||
// There are other data source files to sample from.
|
||||
this.makeFiles(missingPositions, existingFiles);
|
||||
return this.sampleFromFileArray(data, existingFiles).thenApply(this::tryPromoteDataSource)
|
||||
.exceptionally((e) ->
|
||||
{
|
||||
this.removeCorruptedFile(pos, file, e);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,8 +181,8 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler
|
||||
public CompletableFuture<IFullDataSource> onCreateDataFile(FullDataMetaFile file)
|
||||
{
|
||||
DhSectionPos pos = file.pos;
|
||||
CompletableFuture<IFullDataSource> future = updateDataGenStatus(file, data);
|
||||
IIncompleteFullDataSource data = makeEmptyDataSource(pos);
|
||||
CompletableFuture<IFullDataSource> future = updateFromExistingDataSources(file, data);
|
||||
// Cant start gen task, so return the data
|
||||
return future == null ? CompletableFuture.completedFuture(data) : future;
|
||||
}
|
||||
@@ -205,12 +207,16 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler
|
||||
}
|
||||
this.fireOnGenPosSuccessListeners(source.getSectionPos());
|
||||
|
||||
if (source instanceof IIncompleteFullDataSource && !file.genQueueChecked) {
|
||||
if (source instanceof IIncompleteFullDataSource && !file.genQueueChecked)
|
||||
{
|
||||
WorldGenerationQueue worldGenQueue = this.worldGenQueueRef.get();
|
||||
if (worldGenQueue != null) {
|
||||
CompletableFuture<IFullDataSource> future = updateDataGenStatus(file, (IIncompleteFullDataSource) source);
|
||||
if (future != null) {
|
||||
return future.thenApply((newSource) -> {
|
||||
if (worldGenQueue != null)
|
||||
{
|
||||
CompletableFuture<IFullDataSource> future = this.updateFromExistingDataSources(file, (IIncompleteFullDataSource) source);
|
||||
if (future != null)
|
||||
{
|
||||
return future.thenApply((newSource) ->
|
||||
{
|
||||
onUpdated.accept(newSource);
|
||||
return newSource;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user