diff --git a/build.gradle b/build.gradle index 6e6dd6d6..d2cc5b50 100644 --- a/build.gradle +++ b/build.gradle @@ -117,6 +117,7 @@ dependencies { include(implementation 'org.rocksdb:rocksdbjni:8.10.0') include(implementation 'redis.clients:jedis:5.1.0') + include(implementation 'org.apache.commons:commons-pool2:2.12.0') //implementation 'org.rocksdb:rocksdbjni:8.10.0' //implementation 'redis.clients:jedis:5.1.0' } \ No newline at end of file diff --git a/src/main/java/me/cortex/voxy/common/storage/other/ReadonlyCachingLayer.java b/src/main/java/me/cortex/voxy/common/storage/other/ReadonlyCachingLayer.java new file mode 100644 index 00000000..79788941 --- /dev/null +++ b/src/main/java/me/cortex/voxy/common/storage/other/ReadonlyCachingLayer.java @@ -0,0 +1,84 @@ +package me.cortex.voxy.common.storage.other; + +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; +import me.cortex.voxy.common.storage.StorageBackend; +import me.cortex.voxy.common.storage.config.ConfigBuildCtx; +import me.cortex.voxy.common.storage.config.StorageConfig; + +import java.nio.ByteBuffer; +import java.util.List; + +public class ReadonlyCachingLayer extends StorageBackend { + private final StorageBackend cache; + private final StorageBackend onMiss; + + public ReadonlyCachingLayer(StorageBackend cache, StorageBackend onMiss) { + this.cache = cache; + this.onMiss = onMiss; + } + + @Override + public ByteBuffer getSectionData(long key) { + var result = this.cache.getSectionData(key); + if (result != null) { + return result; + } + result = this.onMiss.getSectionData(key); + if (result != null) { + this.cache.setSectionData(key, result); + } + return result; + } + + @Override + public void setSectionData(long key, ByteBuffer data) { + this.cache.setSectionData(key, data); + } + + @Override + public void deleteSectionData(long key) { + this.cache.deleteSectionData(key); + } + + @Override + public void putIdMapping(int id, ByteBuffer data) { + this.cache.putIdMapping(id, data); + } + + @Override + public Int2ObjectOpenHashMap getIdMappingsData() { + //TODO: replicate this data onto the cache + return this.onMiss.getIdMappingsData(); + } + + @Override + public void flush() { + this.cache.close(); + this.onMiss.close(); + } + + @Override + public void close() { + this.cache.close(); + this.onMiss.close(); + } + + public static class Config extends StorageConfig { + public StorageConfig cache; + public StorageConfig onMiss; + + @Override + public List getChildStorageConfigs() { + return List.of(this.cache, this.onMiss); + } + + @Override + public StorageBackend build(ConfigBuildCtx ctx) { + return new ReadonlyCachingLayer(this.cache.build(ctx), this.onMiss.build(ctx)); + } + + public static String getConfigTypeName() { + return "ReadonlyCachingLayer"; + } + } +}