From ef53c7f4b8d44bd89c19ec6db271fc4d2199bd01 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Sun, 2 Aug 2020 00:28:22 +0200 Subject: [PATCH] Allow for compression option in StorageFolder --- .../server/instance/InstanceContainer.java | 9 +++++ .../server/storage/StorageFolder.java | 6 ++-- .../server/storage/StorageManager.java | 34 +++++++++++++------ .../server/storage/StorageOptions.java | 26 ++++++++++++++ .../server/storage/StorageSystem.java | 5 +-- .../storage/systems/FileStorageSystem.java | 12 ++++--- 6 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 src/main/java/net/minestom/server/storage/StorageOptions.java diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 0051792dc..1018881f0 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -365,6 +365,15 @@ public class InstanceContainer extends Instance { saveChunksToStorageFolder(callback); } + /** + * Save the instance without callback + * + * @see #saveInstance(Runnable) + */ + public void saveInstance() { + saveInstance(null); + } + @Override public void saveChunkToStorageFolder(Chunk chunk, Runnable callback) { Check.notNull(getStorageFolder(), "You cannot save the chunk if no StorageFolder has been defined"); diff --git a/src/main/java/net/minestom/server/storage/StorageFolder.java b/src/main/java/net/minestom/server/storage/StorageFolder.java index a3360becc..d770fea2a 100644 --- a/src/main/java/net/minestom/server/storage/StorageFolder.java +++ b/src/main/java/net/minestom/server/storage/StorageFolder.java @@ -24,15 +24,15 @@ public class StorageFolder { private Map cachedData; - protected StorageFolder(StorageSystem storageSystem, String folderPath) { + protected StorageFolder(StorageSystem storageSystem, String folderPath, StorageOptions storageOptions) { this.storageSystem = storageSystem; this.folderPath = folderPath; this.cachedData = new HashMap<>(); - this.storageSystem.open(folderPath); + this.storageSystem.open(folderPath, storageOptions); } - + public byte[] get(String key) { return storageSystem.get(key); } diff --git a/src/main/java/net/minestom/server/storage/StorageManager.java b/src/main/java/net/minestom/server/storage/StorageManager.java index 297872a36..ca872f0cd 100644 --- a/src/main/java/net/minestom/server/storage/StorageManager.java +++ b/src/main/java/net/minestom/server/storage/StorageManager.java @@ -23,16 +23,34 @@ public class StorageManager { * WARNING: a storage folder needs to be created with an unique storage system linked * you cannot open the save folder with two or more different StorageSystem implementation * - * @param folderPath the path to the folder - * @param storageSystem the storage system used in the specified folder + * @param folderPath the path to the folder + * @param storageOptions the storage option + * @param storageSystem the storage system used in the specified folder * @return the specified storage folder */ - public StorageFolder getFolder(String folderPath, StorageSystem storageSystem) { + public StorageFolder getFolder(String folderPath, StorageOptions storageOptions, StorageSystem storageSystem) { + Check.notNull(storageOptions, "The storage option cannot be null"); StorageFolder storageFolder = - folderMap.computeIfAbsent(folderPath, s -> new StorageFolder(storageSystem, folderPath)); + folderMap.computeIfAbsent(folderPath, s -> new StorageFolder(storageSystem, folderPath, storageOptions)); return storageFolder; } + /** + * Used to get an access to the specified folder + * The default StorageSystem provider will be used + * + * @param folderPath the path to the folder + * @param storageOptions the storage option + * @return the specified storage default with the default + * @throws NullPointerException if no default StorageSystem is defined {@link #defineDefaultStorageSystem(Supplier)} + */ + public StorageFolder getFolder(String folderPath, StorageOptions storageOptions) { + Check.notNull(defaultStorageSystemSupplier, + "You need to either define a default storage system or specify your storage system for this specific folder"); + StorageSystem storageSystem = defaultStorageSystemSupplier.get(); + return getFolder(folderPath, storageOptions, storageSystem); + } + /** * Used to get an access to the specified folder * The default StorageSystem provider will be used @@ -42,11 +60,7 @@ public class StorageManager { * @throws NullPointerException if no default StorageSystem is defined {@link #defineDefaultStorageSystem(Supplier)} */ public StorageFolder getFolder(String folderPath) { - Check.notNull(defaultStorageSystemSupplier, - "You need to either define a default storage system or specify your storage system for this specific folder"); - - StorageSystem storageSystem = defaultStorageSystemSupplier.get(); - return getFolder(folderPath, storageSystem); + return getFolder(folderPath, new StorageOptions()); } /** @@ -71,7 +85,7 @@ public class StorageManager { } /** - * Get all folders which have been loaded by {@link #getFolder(String)} or {@link #getFolder(String, StorageSystem)} + * Get all folders which have been loaded by {@link #getFolder(String)} or {@link #getFolder(String, StorageOptions, StorageSystem)} * * @return an unmodifiable list of all the loaded StorageFolder */ diff --git a/src/main/java/net/minestom/server/storage/StorageOptions.java b/src/main/java/net/minestom/server/storage/StorageOptions.java new file mode 100644 index 000000000..2e63e99cb --- /dev/null +++ b/src/main/java/net/minestom/server/storage/StorageOptions.java @@ -0,0 +1,26 @@ +package net.minestom.server.storage; + +public class StorageOptions { + + private boolean compression; + + /** + * Get if compression should be enabled + * + * @return true if compression should be enabled, false otherwise + */ + public boolean hasCompression() { + return compression; + } + + /** + * Define if the storage solution should use compression + * + * @param compression true to enable compression, false otherwise + * @return the reference to the current options + */ + public StorageOptions setCompression(boolean compression) { + this.compression = compression; + return this; + } +} diff --git a/src/main/java/net/minestom/server/storage/StorageSystem.java b/src/main/java/net/minestom/server/storage/StorageSystem.java index 0a9569f50..82f6f8714 100644 --- a/src/main/java/net/minestom/server/storage/StorageSystem.java +++ b/src/main/java/net/minestom/server/storage/StorageSystem.java @@ -11,9 +11,10 @@ public interface StorageSystem { /** * Called when a folder is opened with this StorageSystem * - * @param folderPath the name of the folder + * @param folderPath the name of the folder + * @param storageOptions the storage option */ - void open(String folderPath); + void open(String folderPath, StorageOptions storageOptions); /** * @param key diff --git a/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java b/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java index d8031adef..0bd3a6074 100644 --- a/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java +++ b/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java @@ -1,9 +1,8 @@ package net.minestom.server.storage.systems; +import net.minestom.server.storage.StorageOptions; import net.minestom.server.storage.StorageSystem; -import org.rocksdb.Options; -import org.rocksdb.RocksDB; -import org.rocksdb.RocksDBException; +import org.rocksdb.*; import java.io.File; @@ -25,9 +24,14 @@ public class FileStorageSystem implements StorageSystem { } @Override - public void open(String folderPath) { + public void open(String folderPath, StorageOptions storageOptions) { Options options = new Options().setCreateIfMissing(true); + if (storageOptions.hasCompression()) { + options.setCompressionType(CompressionType.ZSTD_COMPRESSION); + options.setCompressionOptions(new CompressionOptions().setLevel(1)); + } + try { this.rocksDB = RocksDB.open(options, folderPath); } catch (RocksDBException e) {