From 44645943e202eba8e6d6d4c355e896d055e7ed89 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Tue, 18 Mar 2025 07:42:31 -0500 Subject: [PATCH] Fix terrain API repo failing if no cache was provided --- .../data/IDhApiTerrainDataCache.java | 5 +++- .../methods/data/DhApiTerrainDataRepo.java | 24 ++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/com/seibel/distanthorizons/api/interfaces/data/IDhApiTerrainDataCache.java b/api/src/main/java/com/seibel/distanthorizons/api/interfaces/data/IDhApiTerrainDataCache.java index a57a8a56e..2ec043a10 100644 --- a/api/src/main/java/com/seibel/distanthorizons/api/interfaces/data/IDhApiTerrainDataCache.java +++ b/api/src/main/java/com/seibel/distanthorizons/api/interfaces/data/IDhApiTerrainDataCache.java @@ -9,12 +9,15 @@ package com.seibel.distanthorizons.api.interfaces.data; * @version 2024-7-14 * @since API 3.0.0 */ -public interface IDhApiTerrainDataCache +public interface IDhApiTerrainDataCache // TODO should this be AutoClosable? { /** * Removes any data that's currently stored in this cache. * This cane be done to free up memory or invalidate * the cache so fresh data can be pulled in. + *

+ * This should be called before de-referencing this object + * so DH can handle any necessary cleanup for internal objects. */ void clear(); diff --git a/core/src/main/java/com/seibel/distanthorizons/core/api/external/methods/data/DhApiTerrainDataRepo.java b/core/src/main/java/com/seibel/distanthorizons/core/api/external/methods/data/DhApiTerrainDataRepo.java index d3674423e..bbda3c133 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/api/external/methods/data/DhApiTerrainDataRepo.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/api/external/methods/data/DhApiTerrainDataRepo.java @@ -197,10 +197,10 @@ public class DhApiTerrainDataRepo implements IDhApiTerrainDataRepo ILevelWrapper coreLevelWrapper = (ILevelWrapper) levelWrapper; - if (!(apiDataCache instanceof DhApiTerrainDataCache)) + // the data cache can be null, but must be our own implementation + if (apiDataCache != null + && !(apiDataCache instanceof DhApiTerrainDataCache)) { - // custom level wrappers aren't supported, - // the API user must get a level wrapper from our code somewhere return DhApiResult.createFail("Unsupported [" + IDhApiTerrainDataCache.class.getSimpleName() + "] implementation, only the core class [" + DhApiTerrainDataCache.class.getSimpleName() + "] is a valid parameter."); } DhApiTerrainDataCache dataCache = (DhApiTerrainDataCache) apiDataCache; @@ -226,10 +226,9 @@ public class DhApiTerrainDataRepo implements IDhApiTerrainDataRepo // get the data source // //=====================// + FullDataSourceV2 dataSource = null; try { - FullDataSourceV2 dataSource = null; - // try using the cached data if possible if (dataCache != null) { @@ -244,7 +243,12 @@ public class DhApiTerrainDataRepo implements IDhApiTerrainDataRepo { return DhApiResult.createFail("Unable to find/generate any data at the " + DhSectionPos.class.getSimpleName() + " [" + DhSectionPos.toString(sectionPos) + "]."); } - dataCache.add(sectionPos, dataSource); + + // save to the cache if present + if (dataCache != null) + { + dataCache.add(sectionPos, dataSource); + } } @@ -316,6 +320,14 @@ public class DhApiTerrainDataRepo implements IDhApiTerrainDataRepo LOGGER.error("Unexpected exception in getTerrainDataColumnArray. Error: [" + e.getMessage() + "]", e); return DhApiResult.createFail("Unexpected exception: [" + e.getMessage() + "]."); } + finally + { + if (dataCache == null + && dataSource != null) + { + dataSource.close(); + } + } }