Annotations for the storage api

This commit is contained in:
themode 2020-10-24 20:49:39 +02:00
parent b511c12129
commit fa0f9c8fa2
4 changed files with 41 additions and 28 deletions

View File

@ -5,6 +5,8 @@ import net.minestom.server.data.*;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
@ -37,10 +39,11 @@ public class StorageLocation {
* Gets the data associated with a key using {@link StorageSystem#get(String)}.
*
* @param key the key
* @return the data associated to {@code key}
* @return the data associated to {@code key}, null if not any
* @see StorageSystem#get(String)
*/
public byte[] get(String key) {
@Nullable
public byte[] get(@NotNull String key) {
return storageSystem.get(key);
}
@ -51,7 +54,7 @@ public class StorageLocation {
* @param data the data
* @see StorageSystem#set(String, byte[])
*/
public void set(String key, byte[] data) {
public void set(@NotNull String key, byte[] data) {
this.storageSystem.set(key, data);
}
@ -61,7 +64,7 @@ public class StorageLocation {
* @param key the key
* @see StorageSystem#delete(String)
*/
public void delete(String key) {
public void delete(@NotNull String key) {
this.storageSystem.delete(key);
}
@ -85,7 +88,7 @@ public class StorageLocation {
* @param type the class of the data
* @param <T> the type of the data
*/
public <T> void set(String key, T object, Class<T> type) {
public <T> void set(@NotNull String key, @NotNull T object, @NotNull Class<T> type) {
final DataType<T> dataType = DATA_MANAGER.getDataType(type);
Check.notNull(dataType, "You can only save registered DataType type!");
@ -109,7 +112,7 @@ public class StorageLocation {
* @param <T> the type of the data
* @return the object associated to the key
*/
public <T> T get(String key, Class<T> type) {
public <T> T get(@NotNull String key, @NotNull Class<T> type) {
final DataType<T> dataType = DATA_MANAGER.getDataType(type);
Check.notNull(dataType, "You can only get registered DataType type!");
@ -123,7 +126,7 @@ public class StorageLocation {
return dataType.decode(binaryReader);
}
public <T> T getOrDefault(String key, Class<T> type, T defaultValue) {
public <T> T getOrDefault(@NotNull String key, @NotNull Class<T> type, @Nullable T defaultValue) {
T value;
return (value = get(key, type)) != null ? value : defaultValue;
}
@ -135,7 +138,7 @@ public class StorageLocation {
* @param key the key of the data
* @param dataContainer the {@link DataContainer} which will contain the new data
*/
public void getAndCloneData(String key, DataContainer dataContainer) {
public void getAndCloneData(@NotNull String key, @NotNull DataContainer dataContainer) {
synchronized (cachedData) {
// Copy data from the cachedMap
if (cachedData.containsKey(key)) {
@ -161,7 +164,7 @@ public class StorageLocation {
* @param key the key of the data
* @param dataContainer the {@link DataContainer} which will contain the new data
*/
public void getAndCacheData(String key, DataContainer dataContainer) {
public void getAndCacheData(@NotNull String key, @NotNull DataContainer dataContainer) {
synchronized (cachedData) {
// Give the cached SerializableData if already loaded
if (cachedData.containsKey(key)) {
@ -184,7 +187,7 @@ public class StorageLocation {
*
* @param key the specified cached data key
*/
public void saveAndRemoveCachedData(String key) {
public void saveAndRemoveCachedData(@NotNull String key) {
synchronized (cachedData) {
final SerializableData serializableData = cachedData.get(key);
if (serializableData == null)
@ -212,7 +215,7 @@ public class StorageLocation {
*
* @param key the data key
*/
public void saveCachedData(String key) {
public void saveCachedData(@NotNull String key) {
synchronized (cachedData) {
final SerializableData data = cachedData.get(key);
set(key, data.getIndexedSerializedData());
@ -226,6 +229,7 @@ public class StorageLocation {
*
* @return the location
*/
@NotNull
public String getLocation() {
return location;
}

View File

@ -1,6 +1,8 @@
package net.minestom.server.storage;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -33,7 +35,7 @@ public final class StorageManager {
* @param storageSystem the {@link StorageSystem} used in the specified location
* @return the specified {@link StorageLocation}
*/
public StorageLocation getLocation(String location, StorageOptions storageOptions, StorageSystem storageSystem) {
public StorageLocation getLocation(@NotNull String location, @NotNull StorageOptions storageOptions, @NotNull StorageSystem storageSystem) {
Check.notNull(storageOptions, "The storage option cannot be null");
return locationMap.computeIfAbsent(location,
s -> new StorageLocation(storageSystem, location, storageOptions));
@ -48,7 +50,7 @@ public final class StorageManager {
* @return the {@link StorageLocation} at {@code location} with the default {@link StorageSystem}
* @throws NullPointerException if no default {@link StorageSystem} is defined with {@link #defineDefaultStorageSystem(Supplier)}
*/
public StorageLocation getLocation(String location, StorageOptions storageOptions) {
public StorageLocation getLocation(@NotNull String location, @NotNull StorageOptions storageOptions) {
Check.notNull(defaultStorageSystemSupplier,
"You need to either define a default storage system or specify your storage system for this specific location");
final StorageSystem storageSystem = defaultStorageSystemSupplier.get();
@ -63,7 +65,8 @@ public final class StorageManager {
* @return the {@link StorageLocation} at {@code location} with the default {@link StorageSystem}
* @throws NullPointerException if no default StorageSystem is defined {@link #defineDefaultStorageSystem(Supplier)}
*/
public StorageLocation getLocation(String location) {
@Nullable
public StorageLocation getLocation(@NotNull String location) {
return getLocation(location, new StorageOptions());
}
@ -74,7 +77,7 @@ public final class StorageManager {
* @param storageSystem the {@link StorageSystem} to use
* @return true if the location exists, false otherwise
*/
public boolean locationExists(String location, StorageSystem storageSystem) {
public boolean locationExists(@NotNull String location, @NotNull StorageSystem storageSystem) {
return storageSystem.exists(location);
}
@ -84,7 +87,7 @@ public final class StorageManager {
* @param location the location
* @return true if the location exists
*/
public boolean locationExists(String location) {
public boolean locationExists(@NotNull String location) {
return locationExists(location, defaultStorageSystemSupplier.get());
}
@ -94,6 +97,7 @@ public final class StorageManager {
*
* @return an unmodifiable list of all the loaded {@link StorageLocation}
*/
@NotNull
public Collection<StorageLocation> getLoadedLocations() {
return Collections.unmodifiableCollection(locationMap.values());
}
@ -103,7 +107,7 @@ public final class StorageManager {
*
* @param storageSystemSupplier the supplier called to get the default {@link StorageSystem}
*/
public void defineDefaultStorageSystem(Supplier<StorageSystem> storageSystemSupplier) {
public void defineDefaultStorageSystem(@NotNull Supplier<StorageSystem> storageSystemSupplier) {
if (this.defaultStorageSystemSupplier != null) {
LOGGER.warn("The default storage-system has been changed. This could lead to issues!");
}

View File

@ -1,5 +1,8 @@
package net.minestom.server.storage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Represents a way of storing data by key/value.
* The location does not have to be a file or folder path. It is the 'identifier' of the data location.
@ -12,7 +15,7 @@ public interface StorageSystem {
* @param location the location
* @return true if the location exists
*/
boolean exists(String location);
boolean exists(@NotNull String location);
/**
* Called when a {@link StorageLocation} is opened with this {@link StorageSystem}.
@ -20,15 +23,16 @@ public interface StorageSystem {
* @param location the location name
* @param storageOptions the {@link StorageOptions}
*/
void open(String location, StorageOptions storageOptions);
void open(@NotNull String location, @NotNull StorageOptions storageOptions);
/**
* Gets the data associated to a key.
*
* @param key the key to retrieve
* @return the retrieved data
* @return the retrieved data, null if the data doesn't exist
*/
byte[] get(String key);
@Nullable
byte[] get(@NotNull String key);
/**
* Sets the specified data to the defined key.
@ -36,14 +40,14 @@ public interface StorageSystem {
* @param key the key of the data
* @param data the data
*/
void set(String key, byte[] data);
void set(@NotNull String key, byte[] data);
/**
* Deletes the specified key from the database.
*
* @param key the key to delete
*/
void delete(String key);
void delete(@NotNull String key);
/**
* Called when the location is closed, generally during server shutdown.

View File

@ -2,6 +2,7 @@ package net.minestom.server.storage.systems;
import net.minestom.server.storage.StorageOptions;
import net.minestom.server.storage.StorageSystem;
import org.jetbrains.annotations.NotNull;
import org.rocksdb.*;
import java.nio.file.Files;
@ -24,12 +25,12 @@ public class FileStorageSystem implements StorageSystem {
private RocksDB rocksDB;
@Override
public boolean exists(String location) {
public boolean exists(@NotNull String location) {
return Files.isDirectory(Paths.get(location));
}
@Override
public void open(String location, StorageOptions storageOptions) {
public void open(@NotNull String location, @NotNull StorageOptions storageOptions) {
Options options = new Options().setCreateIfMissing(true);
if (storageOptions.hasCompression()) {
@ -45,7 +46,7 @@ public class FileStorageSystem implements StorageSystem {
}
@Override
public byte[] get(String key) {
public byte[] get(@NotNull String key) {
try {
return rocksDB.get(getKey(key));
} catch (RocksDBException e) {
@ -55,7 +56,7 @@ public class FileStorageSystem implements StorageSystem {
}
@Override
public void set(String key, byte[] data) {
public void set(@NotNull String key, byte[] data) {
try {
this.rocksDB.put(getKey(key), data);
} catch (RocksDBException e) {
@ -64,7 +65,7 @@ public class FileStorageSystem implements StorageSystem {
}
@Override
public void delete(String key) {
public void delete(@NotNull String key) {
try {
this.rocksDB.delete(getKey(key));
} catch (RocksDBException e) {