Full Data source refactoring

This commit is contained in:
James Seibel
2023-07-16 20:56:34 -05:00
parent a7a54598b2
commit a00e1c22c3
3 changed files with 43 additions and 47 deletions
@@ -30,7 +30,6 @@ import java.util.function.Consumer;
import java.util.function.Function;
import static com.seibel.distanthorizons.core.util.FileScanUtil.LOD_FILE_POSTFIX;
import static com.seibel.distanthorizons.core.util.FileScanUtil.RENDER_FILE_POSTFIX;
public class FullDataFileHandler implements IFullDataSourceProvider
{
@@ -40,11 +39,13 @@ public class FullDataFileHandler implements IFullDataSourceProvider
protected static ExecutorService fileHandlerThreadPool;
protected static ConfigChangeListener<Integer> configListener;
private final ConcurrentHashMap<DhSectionPos, File> unloadedFiles = new ConcurrentHashMap<>();
private final ConcurrentHashMap<DhSectionPos, FullDataMetaFile> fileBySectionPos = new ConcurrentHashMap<>();
public void ForEachFile(Consumer<FullDataMetaFile> consumer) { this.fileBySectionPos.values().forEach(consumer); }
private LinkedList<Consumer<IFullDataSource>> onUpdatedListeners = new LinkedList<>();
protected final IDhLevel level;
protected final File saveDir;
/**
@@ -59,6 +60,11 @@ public class FullDataFileHandler implements IFullDataSourceProvider
protected final AtomicInteger topDetailLevel = new AtomicInteger(DhSectionPos.SECTION_REGION_DETAIL_LEVEL);
protected final int minDetailLevel = CompleteFullDataSource.SECTION_SIZE_OFFSET;
//=============//
// constructor //
//=============//
public FullDataFileHandler(IDhLevel level, AbstractSaveStructure saveStructure)
{
this.level = level;
@@ -69,7 +75,9 @@ public class FullDataFileHandler implements IFullDataSourceProvider
}
FileScanUtil.scanFiles(saveStructure, level.getLevelWrapper(), this, null);
}
// constructor helpers //
/**
* Caller must ensure that this method is called only once,
* and that the {@link FullDataFileHandler} is not used before this method is called.
@@ -179,6 +187,9 @@ public class FullDataFileHandler implements IFullDataSourceProvider
}
}
protected FullDataMetaFile getLoadOrMakeFile(DhSectionPos pos, boolean allowCreateFile)
{
FullDataMetaFile metaFile = this.fileBySectionPos.get(pos);
@@ -414,41 +425,19 @@ public class FullDataFileHandler implements IFullDataSourceProvider
}
return metaFile.flushAndSaveAsync();
}
private LinkedList<Consumer<IFullDataSource>> onUpdatedListeners = new LinkedList<>();
@Override
public synchronized void addOnUpdatedListener(Consumer<IFullDataSource> listener)
{
this.onUpdatedListeners.add(listener);
}
// @Override
// public long getCacheVersion(DhSectionPos sectionPos)
// {
// FullDataMetaFile file = this.files.get(sectionPos);
// if (file == null)
// {
// return 0;
// }
// return file.getCacheVersion();
// }
// @Override
// public boolean isCacheVersionValid(DhSectionPos sectionPos, long cacheVersion)
// {
// FullDataMetaFile file = this.files.get(sectionPos);
// if (file == null)
// {
// return cacheVersion >= 0;
// }
// return file.isCacheVersionValid(cacheVersion);
// }
protected IIncompleteFullDataSource makeDataSource(DhSectionPos pos)
protected IIncompleteFullDataSource makeEmptyDataSource(DhSectionPos pos)
{
return pos.sectionDetailLevel <= HighDetailIncompleteFullDataSource.MAX_SECTION_DETAIL ?
HighDetailIncompleteFullDataSource.createEmpty(pos) : LowDetailIncompleteFullDataSource.createEmpty(pos);
HighDetailIncompleteFullDataSource.createEmpty(pos) :
LowDetailIncompleteFullDataSource.createEmpty(pos);
}
protected CompletableFuture<IIncompleteFullDataSource> sampleFromFiles(IIncompleteFullDataSource source, ArrayList<FullDataMetaFile> existingFiles)
@@ -486,7 +475,7 @@ public class FullDataFileHandler implements IFullDataSourceProvider
public CompletableFuture<IFullDataSource> onCreateDataFile(FullDataMetaFile file)
{
DhSectionPos pos = file.pos;
IIncompleteFullDataSource source = this.makeDataSource(pos);
IIncompleteFullDataSource source = this.makeEmptyDataSource(pos);
ArrayList<FullDataMetaFile> existFiles = new ArrayList<>();
ArrayList<DhSectionPos> missing = new ArrayList<>();
this.getDataFilesForPosition(pos, pos, existFiles, missing);
@@ -254,9 +254,9 @@ public class FullDataMetaFile extends AbstractMetaDataContainerFile implements I
}, executorService));
}
private void makeCreateCompletionStage(ExecutorService executorService, CompletableFuture<IFullDataSource> completer)
private void makeCreateCompletionStage(CompletableFuture<IFullDataSource> completer)
{
makeUpdateCompletionStage(completer, this.fullDataSourceProvider.onCreateDataFile(this)
this.makeUpdateCompletionStage(completer, this.fullDataSourceProvider.onCreateDataFile(this)
.thenApply((fullDataSource) ->
{
this.baseMetaData = this._makeBaseMetaData(fullDataSource);
@@ -273,31 +273,39 @@ public class FullDataMetaFile extends AbstractMetaDataContainerFile implements I
CacheQueryResult result = this.getCachedDataSourceAsync();
if (result.needsLoad) {
LodUtil.assertTrue(!inCrit);
inCrit = true;
if (result.needsLoad)
{
LodUtil.assertTrue(!this.inCrit);
this.inCrit = true;
CompletableFuture<IFullDataSource> future = result.future;
// don't continue if the provider has been shut down
ExecutorService executorService = this.fullDataSourceProvider.getIOExecutor();
if (executorService.isTerminated())
{
inCrit = false;
dataSourceLoadFutureRef.set(null);
this.inCrit = false;
this.dataSourceLoadFutureRef.set(null);
future.complete(null);
return future;
}
// create a new Meta file
if (!doesFileExist) {
makeCreateCompletionStage(executorService, future);
if (!this.doesFileExist)
{
this.makeCreateCompletionStage(future);
}
// Otherwise, load and update file
else {
if (this.baseMetaData == null) throw new IllegalStateException("Meta data not loaded!");
makeLoadCompletionStage(executorService, future);
else
{
// Otherwise, load and update file
if (this.baseMetaData == null)
{
throw new IllegalStateException("Meta data not loaded!");
}
this.makeLoadCompletionStage(executorService, future);
}
}
return result.future;
}
@@ -17,7 +17,6 @@ import com.seibel.distanthorizons.core.util.LodUtil;
import org.apache.logging.log4j.Logger;
import javax.annotation.Nullable;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.*;
import java.util.concurrent.*;
@@ -180,8 +179,8 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler
public CompletableFuture<IFullDataSource> onCreateDataFile(FullDataMetaFile file)
{
DhSectionPos pos = file.pos;
IIncompleteFullDataSource data = makeDataSource(pos);
CompletableFuture<IFullDataSource> future = updateDataGenStatus(file, data);
IIncompleteFullDataSource data = makeEmptyDataSource(pos);
// Cant start gen task, so return the data
return future == null ? CompletableFuture.completedFuture(data) : future;
}