Allow for compression option in StorageFolder

This commit is contained in:
Felix Cravic 2020-08-02 00:28:22 +02:00
parent 395b205484
commit ef53c7f4b8
6 changed files with 73 additions and 19 deletions

View File

@ -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");

View File

@ -24,15 +24,15 @@ public class StorageFolder {
private Map<String, SerializableData> 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);
}

View File

@ -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
*/

View File

@ -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;
}
}

View File

@ -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

View File

@ -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) {