diff --git a/core/src/main/java/com/seibel/lod/core/file/fullDatafile/FullDataFileHandler.java b/core/src/main/java/com/seibel/lod/core/file/fullDatafile/FullDataFileHandler.java index 4aab8f52e..74477d343 100644 --- a/core/src/main/java/com/seibel/lod/core/file/fullDatafile/FullDataFileHandler.java +++ b/core/src/main/java/com/seibel/lod/core/file/fullDatafile/FullDataFileHandler.java @@ -98,7 +98,9 @@ public class FullDataFileHandler implements IFullDataSourceProvider FullDataMetaFile fileToUse; if (metaFiles.size() > 1) { - fileToUse = Collections.max(metaFiles, Comparator.comparingLong(a -> a.metaData.dataVersion.get())); +// fileToUse = Collections.max(metaFiles, Comparator.comparingLong(a -> a.metaData.dataVersion.get())); + + fileToUse = Collections.max(metaFiles, Comparator.comparingLong(fullDataMetaFile -> fullDataMetaFile.path.lastModified())); { StringBuilder sb = new StringBuilder(); sb.append("Multiple files with the same pos: "); @@ -324,27 +326,27 @@ public class FullDataFileHandler implements IFullDataSourceProvider return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); } - @Override - public long getCacheVersion(DhSectionPos sectionPos) - { - FullDataMetaFile file = this.files.get(sectionPos); - if (file == null) - { - return 0; - } - return file.getCacheVersion(); - } +// @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); - } +// @Override +// public boolean isCacheVersionValid(DhSectionPos sectionPos, long cacheVersion) +// { +// FullDataMetaFile file = this.files.get(sectionPos); +// if (file == null) +// { +// return cacheVersion >= 0; +// } +// return file.isCacheVersionValid(cacheVersion); +// } @Override public CompletableFuture onCreateDataFile(FullDataMetaFile file) @@ -415,10 +417,10 @@ public class FullDataFileHandler implements IFullDataSourceProvider Consumer onUpdated, Function updater) { boolean changed = updater.apply(source); - if (changed) - { - metaData.dataVersion.incrementAndGet(); - } +// if (changed) +// { +// metaData.dataVersion.incrementAndGet(); +// } if (source instanceof IIncompleteFullDataSource) { @@ -439,8 +441,14 @@ public class FullDataFileHandler implements IFullDataSourceProvider return CompletableFuture.supplyAsync(() -> { IFullDataSource sourceLocal = source; + boolean changed = updater.apply(sourceLocal); - if (changed) metaData.dataVersion.incrementAndGet(); +// if (changed) +// { +// metaData.dataVersion.incrementAndGet(); +// } + + if (sourceLocal instanceof IIncompleteFullDataSource) { IFullDataSource newSource = ((IIncompleteFullDataSource) sourceLocal).trySelfPromote(); diff --git a/core/src/main/java/com/seibel/lod/core/file/fullDatafile/FullDataMetaFile.java b/core/src/main/java/com/seibel/lod/core/file/fullDatafile/FullDataMetaFile.java index b34b2056a..e540d474d 100644 --- a/core/src/main/java/com/seibel/lod/core/file/fullDatafile/FullDataMetaFile.java +++ b/core/src/main/java/com/seibel/lod/core/file/fullDatafile/FullDataMetaFile.java @@ -119,26 +119,26 @@ public class FullDataMetaFile extends AbstractMetaDataFile } } - public long getCacheVersion() { - debugCheck(); - return (this.metaData == null) ? 0 : this.metaData.dataVersion.get(); - } +// public long getCacheVersion() { +// debugCheck(); +// return (this.metaData == null) ? 0 : this.metaData.dataVersion.get(); +// } - public boolean isCacheVersionValid(long cacheVersion) - { - debugCheck(); - boolean noWrite = this.writeQueue.get().queue.isEmpty(); - if (!noWrite) - { - return false; - } - else - { - MetaData getData = this.metaData; - //NOTE: Do this instead of direct compare so values that wrapped around still work correctly. - return (getData == null ? 0 : this.metaData.dataVersion.get()) - cacheVersion <= 0; - } - } +// public boolean isCacheVersionValid(long cacheVersion) +// { +// debugCheck(); +// boolean noWrite = this.writeQueue.get().queue.isEmpty(); +// if (!noWrite) +// { +// return false; +// } +// else +// { +// MetaData getData = this.metaData; +// //NOTE: Do this instead of direct compare so values that wrapped around still work correctly. +// return (getData == null ? 0 : this.metaData.dataVersion.get()) - cacheVersion <= 0; +// } +// } public void addToWriteQueue(ChunkSizedData datatype) { debugCheck(); @@ -259,7 +259,7 @@ public class FullDataMetaFile extends AbstractMetaDataFile private static MetaData makeMetaData(IFullDataSource data) { AbstractDataSourceLoader loader = AbstractDataSourceLoader.getLoader(data.getClass(), data.getDataVersion()); - return new MetaData(data.getSectionPos(), -1, 1, + return new MetaData(data.getSectionPos(), -1, data.getDataDetail(), loader == null ? 0 : loader.datatypeId, data.getDataVersion()); } diff --git a/core/src/main/java/com/seibel/lod/core/file/fullDatafile/GeneratedFullDataFileHandler.java b/core/src/main/java/com/seibel/lod/core/file/fullDatafile/GeneratedFullDataFileHandler.java index a9749bb6e..473a40859 100644 --- a/core/src/main/java/com/seibel/lod/core/file/fullDatafile/GeneratedFullDataFileHandler.java +++ b/core/src/main/java/com/seibel/lod/core/file/fullDatafile/GeneratedFullDataFileHandler.java @@ -126,7 +126,7 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler if (exception == null && genTaskCompleted) { - this.files.get(task.pos).metaData.dataVersion.incrementAndGet(); +// this.files.get(task.pos).metaData.dataVersion.incrementAndGet(); return; } task.releaseStrongReference(); diff --git a/core/src/main/java/com/seibel/lod/core/file/fullDatafile/IFullDataSourceProvider.java b/core/src/main/java/com/seibel/lod/core/file/fullDatafile/IFullDataSourceProvider.java index 4cb3b9eb8..f494e06da 100644 --- a/core/src/main/java/com/seibel/lod/core/file/fullDatafile/IFullDataSourceProvider.java +++ b/core/src/main/java/com/seibel/lod/core/file/fullDatafile/IFullDataSourceProvider.java @@ -19,8 +19,8 @@ public interface IFullDataSourceProvider extends AutoCloseable { void write(DhSectionPos sectionPos, ChunkSizedData chunkData); CompletableFuture flushAndSave(); - long getCacheVersion(DhSectionPos sectionPos); - boolean isCacheVersionValid(DhSectionPos sectionPos, long cacheVersion); + //long getCacheVersion(DhSectionPos sectionPos); + //boolean isCacheVersionValid(DhSectionPos sectionPos, long cacheVersion); CompletableFuture onCreateDataFile(FullDataMetaFile file); IFullDataSource onDataFileLoaded(IFullDataSource source, MetaData metaData, Consumer onUpdated, Function updater); diff --git a/core/src/main/java/com/seibel/lod/core/file/metaData/AbstractMetaDataFile.java b/core/src/main/java/com/seibel/lod/core/file/metaData/AbstractMetaDataFile.java index c123a5c45..819b0a3cf 100644 --- a/core/src/main/java/com/seibel/lod/core/file/metaData/AbstractMetaDataFile.java +++ b/core/src/main/java/com/seibel/lod/core/file/metaData/AbstractMetaDataFile.java @@ -104,31 +104,31 @@ public abstract class AbstractMetaDataFile { try (FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ)) { - ByteBuffer buffer = ByteBuffer.allocate(METADATA_SIZE); - channel.read(buffer, 0); + ByteBuffer byteBuffer = ByteBuffer.allocate(METADATA_SIZE); + channel.read(byteBuffer, 0); channel.close(); - buffer.flip(); + byteBuffer.flip(); - int idBytes = buffer.getInt(); + int idBytes = byteBuffer.getInt(); if (idBytes != METADATA_IDENTITY_BYTES) { throw new IOException("Invalid file format: Metadata Identity byte check failed. Expected: [" + METADATA_IDENTITY_BYTES + "], Actual: [" + idBytes + "]."); } - int x = buffer.getInt(); - int y = buffer.getInt(); // Unused - int z = buffer.getInt(); - int checksum = buffer.getInt(); - byte detailLevel = buffer.get(); - byte dataLevel = buffer.get(); - byte loaderVersion = buffer.get(); - byte unused = buffer.get(); - long dataTypeId = buffer.getLong(); - long timestamp = buffer.getLong(); - LodUtil.assertTrue(buffer.remaining() == METADATA_RESERVED_SIZE); + int x = byteBuffer.getInt(); + int y = byteBuffer.getInt(); // Unused + int z = byteBuffer.getInt(); + int checksum = byteBuffer.getInt(); + byte detailLevel = byteBuffer.get(); + byte dataLevel = byteBuffer.get(); + byte loaderVersion = byteBuffer.get(); + byte unused = byteBuffer.get(); + long dataTypeId = byteBuffer.getLong(); + long unusedTimestamp = byteBuffer.getLong(); // not currently implemented + LodUtil.assertTrue(byteBuffer.remaining() == METADATA_RESERVED_SIZE); DhSectionPos dataPos = new DhSectionPos(detailLevel, x, z); - return new MetaData(dataPos, checksum, timestamp, dataLevel, dataTypeId, loaderVersion); + return new MetaData(dataPos, checksum, dataLevel, dataTypeId, loaderVersion); } } @@ -204,7 +204,7 @@ public abstract class AbstractMetaDataFile buff.put(this.metaData.loaderVersion); buff.put(Byte.MIN_VALUE); // Unused buff.putLong(this.metaData.dataTypeId); - buff.putLong(this.metaData.dataVersion.get()); + buff.putLong(Long.MAX_VALUE); //buff.putLong(this.metaData.dataVersion.get()); // not currently implemented LodUtil.assertTrue(buff.remaining() == METADATA_RESERVED_SIZE); buff.flip(); file.write(buff); diff --git a/core/src/main/java/com/seibel/lod/core/file/metaData/MetaData.java b/core/src/main/java/com/seibel/lod/core/file/metaData/MetaData.java index 1753e737a..dd7360387 100644 --- a/core/src/main/java/com/seibel/lod/core/file/metaData/MetaData.java +++ b/core/src/main/java/com/seibel/lod/core/file/metaData/MetaData.java @@ -13,17 +13,17 @@ public class MetaData { public DhSectionPos pos; public int checksum; - public AtomicLong dataVersion; +// public AtomicLong dataVersion; // currently broken public byte dataLevel; // Loader stuff public long dataTypeId; public byte loaderVersion; - public MetaData(DhSectionPos pos, int checksum, long dataVersion, byte dataLevel, long dataTypeId, byte loaderVersion) + public MetaData(DhSectionPos pos, int checksum, byte dataLevel, long dataTypeId, byte loaderVersion) { this.pos = pos; this.checksum = checksum; - this.dataVersion = new AtomicLong(dataVersion); +// this.dataVersion = new AtomicLong(dataVersion); this.dataLevel = dataLevel; this.dataTypeId = dataTypeId; this.loaderVersion = loaderVersion; diff --git a/core/src/main/java/com/seibel/lod/core/file/renderfile/RenderFileHandler.java b/core/src/main/java/com/seibel/lod/core/file/renderfile/RenderFileHandler.java index 6d323fa25..704d80908 100644 --- a/core/src/main/java/com/seibel/lod/core/file/renderfile/RenderFileHandler.java +++ b/core/src/main/java/com/seibel/lod/core/file/renderfile/RenderFileHandler.java @@ -67,7 +67,7 @@ public class RenderFileHandler implements ILodRenderSourceProvider { try { - RenderMetaDataFile metaFile = new RenderMetaDataFile(this, file); + RenderMetaDataFile metaFile = RenderMetaDataFile.createFromExistingFile(this, file); filesByPos.put(metaFile.pos, metaFile); } catch (IOException e) @@ -107,8 +107,14 @@ public class RenderFileHandler implements ILodRenderSourceProvider RenderMetaDataFile fileToUse; if (metaFiles.size() > 1) { + //fileToUse = metaFiles.stream().findFirst().orElse(null); // use the first file in the list + + // use the file's last modified date fileToUse = Collections.max(metaFiles, Comparator.comparingLong(renderMetaDataFile -> - renderMetaDataFile.metaData.dataVersion.get())); + renderMetaDataFile.path.lastModified())); + +// fileToUse = Collections.max(metaFiles, Comparator.comparingLong(renderMetaDataFile -> +// renderMetaDataFile.metaData.dataVersion.get())); { StringBuilder sb = new StringBuilder(); sb.append("Multiple files with the same pos: "); @@ -301,7 +307,7 @@ public class RenderFileHandler implements ILodRenderSourceProvider //LOGGER.info("Recreating cache for {}", data.getSectionPos()); DataRenderTransformer.asyncTransformDataSource(fullDataSourceFuture, this.level) - .thenAccept((newRenderSource) -> this.write(renderSourceReference.get(), file, newRenderSource, this.fullDataSourceProvider.getCacheVersion(renderSource.getSectionPos()))) + .thenAccept((newRenderSource) -> this.write(renderSourceReference.get(), file, newRenderSource)) .exceptionally((ex) -> { if (!UncheckedInterruptedException.isThrowableInterruption(ex)) @@ -315,16 +321,18 @@ public class RenderFileHandler implements ILodRenderSourceProvider public IRenderSource onRenderFileLoaded(IRenderSource renderSource, RenderMetaDataFile file) { - if (!this.fullDataSourceProvider.isCacheVersionValid(file.pos, file.metaData.dataVersion.get())) - { + // TODO + +// if (!this.fullDataSourceProvider.isCacheVersionValid(file.pos, file.metaData.dataVersion.get())) +// { this.updateCache(renderSource, file); - } +// } return renderSource; } private void write(IRenderSource currentRenderSource, RenderMetaDataFile file, - IRenderSource newRenderSource, long newDataVersion) + IRenderSource newRenderSource) { if (currentRenderSource == null || newRenderSource == null) { @@ -333,7 +341,7 @@ public class RenderFileHandler implements ILodRenderSourceProvider currentRenderSource.updateFromRenderSource(newRenderSource); - file.metaData.dataVersion.set(newDataVersion); + //file.metaData.dataVersion.set(newDataVersion); file.metaData.dataLevel = currentRenderSource.getDataDetail(); file.loader = AbstractRenderSourceLoader.getLoader(currentRenderSource.getClass(), currentRenderSource.getRenderVersion()); file.dataType = currentRenderSource.getClass(); @@ -344,10 +352,10 @@ public class RenderFileHandler implements ILodRenderSourceProvider public void onReadRenderSourceFromCache(RenderMetaDataFile file, IRenderSource data) { - if (!this.fullDataSourceProvider.isCacheVersionValid(file.pos, file.metaData.dataVersion.get())) - { +// if (!this.fullDataSourceProvider.isCacheVersionValid(file.pos, file.metaData.dataVersion.get())) +// { this.updateCache(data, file); - } +// } } public boolean refreshRenderSource(IRenderSource source) @@ -363,13 +371,13 @@ public class RenderFileHandler implements ILodRenderSourceProvider LodUtil.assertTrue(file != null); LodUtil.assertTrue(file.metaData != null); - if (!this.fullDataSourceProvider.isCacheVersionValid(file.pos, file.metaData.dataVersion.get())) - { +// if (!this.fullDataSourceProvider.isCacheVersionValid(file.pos, file.metaData.dataVersion.get())) +// { this.updateCache(source, file); return true; - } +// } - return false; +// return false; } diff --git a/core/src/main/java/com/seibel/lod/core/file/renderfile/RenderMetaDataFile.java b/core/src/main/java/com/seibel/lod/core/file/renderfile/RenderMetaDataFile.java index 32cfc4e63..4d295be9c 100644 --- a/core/src/main/java/com/seibel/lod/core/file/renderfile/RenderMetaDataFile.java +++ b/core/src/main/java/com/seibel/lod/core/file/renderfile/RenderMetaDataFile.java @@ -33,18 +33,6 @@ public class RenderMetaDataFile extends AbstractMetaDataFile // null - Nothing is loaded or being loaded AtomicReference data = new AtomicReference<>(null); -// @FunctionalInterface -// public interface CacheValidator -// { -// boolean isCacheValid(DhSectionPos sectionPos, long timestamp); -// } -// @FunctionalInterface -// public interface CacheSourceProducer -// { -// CompletableFuture getSourceFuture(DhSectionPos sectionPos); -// } -// CacheValidator validator; -// CacheSourceProducer source; private final RenderFileHandler fileHandler; private boolean doesFileExist; @@ -250,7 +238,7 @@ public class RenderMetaDataFile extends AbstractMetaDataFile private static MetaData makeMetaData(IRenderSource data) { AbstractRenderSourceLoader loader = AbstractRenderSourceLoader.getLoader(data.getClass(), data.getRenderVersion()); - return new MetaData(data.getSectionPos(), -1, -1, + return new MetaData(data.getSectionPos(), -1, data.getDataDetail(), loader == null ? 0 : loader.renderTypeId, data.getRenderVersion()); }