Skip to content

Commit

Permalink
Some fixes for deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLimeGlass committed Mar 27, 2024
1 parent 8569bca commit c95ee21
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ public final boolean loadConfiguration(FileSection section) {
return false;

this.file = getFile(fileName).getAbsoluteFile();

if (file.exists() && !file.isFile()) {
logger.error("The database file '" + file.getName() + "' does not exist or is a directory.", ErrorType.SEMANTIC_ERROR);
return false;
Expand Down Expand Up @@ -225,15 +224,20 @@ public final boolean loadConfiguration(FileSection section) {
}

/**
* Loads configurations and variables.
* Loads configurations and should start loading variables too.
*
* @return Whether the database could be loaded successfully,
* i.e. whether the configuration is correct and all variables could be loaded.
*/
protected abstract boolean load(FileSection section);

protected void load(String name, SerializedVariable variable) {
load(name, variable.value.type, variable.value.data);
protected void loadVariable(String name, SerializedVariable variable) {
Value value = variable.value;
if (value == null) {
Variables.queueVariableChange(name, null);
return;
}
loadVariable(name, value.type, value.data);
}

/**
Expand All @@ -243,7 +247,7 @@ protected void load(String name, SerializedVariable variable) {
* @param type the type of the variable.
* @param value the serialized value of the variable.
*/
protected void load(String name, @NotNull String type, @NotNull byte[] value) {
protected void loadVariable(String name, @NotNull String type, @NotNull byte[] value) {
if (value == null || type == null)
throw new IllegalArgumentException("value and/or typeName cannot be null");
Variables.queueVariableChange(name, deserialize(type, value));
Expand Down Expand Up @@ -298,9 +302,9 @@ boolean accept(@Nullable String variableName) {
* @return the serialized variable.
*/
@SuppressWarnings("unchecked")
public <T> SerializedVariable serialize(String name, @NotNull T value) {
public <T> SerializedVariable serialize(String name, @Nullable T value) {
if (value == null)
throw new IllegalArgumentException("value cannot be null");
return new SerializedVariable(name, null);
Type<T> type = (Type<T>) TypeManager.getByClassExact(value.getClass()).orElse(null);
if (type == null)
throw new UnsupportedOperationException("Class '" + value.getClass().getName() + "' cannot be serialized. No type registered.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private static void processChangeQueue() {
.filter(storage -> storage.accept(change.name))
.forEach(storage -> {
SerializedVariable serialized = storage.serialize(change.name, change.value);
storage.save(serialized);
storage.save(serialized);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public void testVariables() throws InterruptedException {
Optional<?> value = RamStorage.SELF.deserialize(variable.value.type, variable.value.data);
assert value.isPresent();
assert value.get().equals("Hello New World!") : value.get();
Variables.setVariable("test", null, null, false);
Thread.sleep(1);
assert !Variables.getVariable("test", null, false).isPresent();
assert !RamStorage.VARIABLES.containsKey("test");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.util.Map;
import java.util.Optional;

/**
* A VariableStorage that only saves into ram memory.
*/
public class RamStorage extends VariableStorage {

public static final Map<String, SerializedVariable> VARIABLES = new HashMap<>();
Expand Down

0 comments on commit c95ee21

Please sign in to comment.