Skip to content

Commit

Permalink
Update Hikari and H2
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLimeGlass committed Sep 20, 2024
1 parent 8be6d63 commit 980ddfd
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 39 deletions.
5 changes: 2 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ jarName=Skript.jar
testEnv=java21/paper-1.21.0
testEnvJavaVersion=21

# Note that HikariCP 4.x is Java 8 and 5.x is Java 11+
hikaricp.version=4.0.3
h2.version=2.1.214
hikaricp.version=5.1.0
h2.version=2.3.232
4 changes: 2 additions & 2 deletions src/main/java/ch/njol/skript/SkriptAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import ch.njol.skript.util.Utils;
import ch.njol.skript.util.Version;
import ch.njol.skript.variables.Variables;
import ch.njol.skript.variables.VariablesStorage;
import ch.njol.skript.variables.VariableStorage;

/**
* Utility class for Skript addons. Use {@link Skript#registerAddon(JavaPlugin)} to create a SkriptAddon instance for your plugin.
Expand Down Expand Up @@ -94,7 +94,7 @@ public SkriptAddon loadClasses(String basePackage, String... subPackages) throws
* @return This SkriptAddon for method chaining.
* @throws SkriptAPIException if the operation was not successful because the storage class is already registered.
*/
public <T extends VariablesStorage> SkriptAddon registerStorage(Class<T> storage, String... names) throws SkriptAPIException {
public <T extends VariableStorage> SkriptAddon registerStorage(Class<T> storage, String... names) throws SkriptAPIException {
Variables.registerStorage(this, storage, names);
return this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ch/njol/skript/variables/FlatFileStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
* accessed. (rem: print a warning when Skript starts)
* rem: store null variables (in memory) to prevent looking up the same variables over and over again
*/
public class FlatFileStorage extends VariablesStorage {
public class FlatFileStorage extends VariableStorage {

/**
* The {@link Charset} used in the CSV storage file.
Expand Down Expand Up @@ -137,7 +137,7 @@ public class FlatFileStorage extends VariablesStorage {
* Loads the variables in the CSV file.
* <p>
* Doesn't lock the connection, as required by
* {@link Variables#variableLoaded(String, Object, VariablesStorage)}.
* {@link Variables#variableLoaded(String, Object, VariableStorage)}.
*/
@Override
@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -492,7 +492,7 @@ private void save(PrintWriter pw, String parent, TreeMap<String, Object> map) {

try {
// Loop over storages to make sure this variable is ours to store
for (VariablesStorage storage : Variables.STORAGES) {
for (VariableStorage storage : Variables.STORAGES) {
if (storage.accept(name)) {
if (storage == this) {
// Serialize the value
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/ch/njol/skript/variables/JdbcStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import ch.njol.util.NonNullPair;
import ch.njol.util.SynchronizedReference;

public abstract class JdbcStorage extends VariablesStorage {
public abstract class JdbcStorage extends VariableStorage {

protected static final String DEFAULT_TABLE_NAME = "variables21";

Expand Down Expand Up @@ -218,7 +218,7 @@ private boolean prepareQueries() {

/**
* Doesn't lock the database for reading (it's not used anywhere else, and locking while loading will interfere with loaded variables being deleted by
* {@link Variables#variableLoaded(String, Object, VariablesStorage)}).
* {@link Variables#variableLoaded(String, Object, VariableStorage)}).
*/
@Override
protected final boolean loadAbstract(SectionNode section) {
Expand Down Expand Up @@ -295,7 +295,8 @@ public boolean load(SectionNode n) {
}

/**
* Doesn't lock the database - {@link #save(String, String, byte[])} does that // what?
* Doesn't lock the database
* {@link #save(String, String, byte[])} does that as values are inserted in the database.
*/
private void loadVariables(ResultSet result) throws SQLException {
SQLException e = Task.callSync(new Callable<SQLException>() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ch/njol/skript/variables/UnloadedStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
public class UnloadedStorage {

private final Class<? extends VariablesStorage> storage;
private final Class<? extends VariableStorage> storage;
private final SkriptAddon source;
private final String[] names;

Expand All @@ -37,7 +37,7 @@ public class UnloadedStorage {
* @param storage The class of the actual VariableStorage to initalize with.
* @param names The possible user input names from the config.sk to match this storage.
*/
public UnloadedStorage(SkriptAddon source, Class<? extends VariablesStorage> storage, String... names) {
public UnloadedStorage(SkriptAddon source, Class<? extends VariableStorage> storage, String... names) {
this.storage = storage;
this.source = source;
this.names = names;
Expand All @@ -46,7 +46,7 @@ public UnloadedStorage(SkriptAddon source, Class<? extends VariablesStorage> sto
/**
* @return the storage class
*/
public Class<? extends VariablesStorage> getStorageClass() {
public Class<? extends VariableStorage> getStorageClass() {
return storage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* @see DatabaseStorage
*/
// FIXME ! large databases (>25 MB) cause the server to be unresponsive instead of loading slowly
public abstract class VariablesStorage implements Closeable {
public abstract class VariableStorage implements Closeable {

/**
* The size of the variable changes queue.
Expand Down Expand Up @@ -102,7 +102,7 @@ public abstract class VariablesStorage implements Closeable {
*
* @param name the name.
*/
protected VariablesStorage(SkriptAddon source, String name) {
protected VariableStorage(SkriptAddon source, String name) {
assert name != null;
databaseName = name;
this.source = source;
Expand Down Expand Up @@ -193,7 +193,6 @@ protected final <T> T getOptional(SectionNode sectionNode, String key, Class<T>
@Nullable
private <T> T getValue(SectionNode sectionNode, String key, Class<T> type, boolean error) {
String rawValue = sectionNode.getValue(key);
// Section node doesn't have this key
if (rawValue == null) {
if (error)
Skript.error("The config is missing the entry for '" + key + "' in the database '" + databaseName + "'");
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/ch/njol/skript/variables/Variables.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public Class<? extends ConfigurationSerializable> getClass(@NotNull String id) {
/**
* The variable storages configured.
*/
static final List<VariablesStorage> STORAGES = new ArrayList<>();
static final List<VariableStorage> STORAGES = new ArrayList<>();

/**
* Register a VariableStorage class for Skript to create if the user config value matches.
Expand All @@ -165,7 +165,7 @@ public Class<? extends ConfigurationSerializable> getClass(@NotNull String id) {
* @return if the operation was successful, or false if the class is already registered.
* @throws SkriptAPIException if the operation was not successful because the storage class is already registered.
*/
public static <T extends VariablesStorage> boolean registerStorage(SkriptAddon source, Class<T> storage, String... names) {
public static <T extends VariableStorage> boolean registerStorage(SkriptAddon source, Class<T> storage, String... names) {
for (UnloadedStorage registered : UNLOADED_STORAGES) {
if (registered.getStorageClass().isAssignableFrom(storage))
throw new SkriptAPIException("Storage class '" + storage.getName() + "' cannot be registered because '" + registered.getStorageClass().getName() + "' is a superclass or equal class");
Expand Down Expand Up @@ -210,7 +210,7 @@ public static boolean load() {
} catch (InterruptedException ignored) {}

synchronized (TEMP_VARIABLES) {
Map<String, NonNullPair<Object, VariablesStorage>> tvs = TEMP_VARIABLES.get();
Map<String, NonNullPair<Object, VariableStorage>> tvs = TEMP_VARIABLES.get();
if (tvs != null)
Skript.info("Loaded " + tvs.size() + " variables so far...");
else
Expand All @@ -236,7 +236,7 @@ public static boolean load() {
assert name != null;

// Initiate the right VariablesStorage class
VariablesStorage variablesStorage;
VariableStorage variablesStorage;
Optional<UnloadedStorage> optional = UNLOADED_STORAGES.stream()
.filter(registered -> registered.matches(type))
.findFirst();
Expand All @@ -250,10 +250,10 @@ public static boolean load() {

UnloadedStorage unloadedStorage = optional.get();
try {
Class<? extends VariablesStorage> storageClass = unloadedStorage.getStorageClass();
Class<? extends VariableStorage> storageClass = unloadedStorage.getStorageClass();
Constructor<?> constructor = storageClass.getDeclaredConstructor(SkriptAddon.class, String.class);
constructor.setAccessible(true);
variablesStorage = (VariablesStorage) constructor.newInstance(unloadedStorage.getSource(), type);
variablesStorage = (VariableStorage) constructor.newInstance(unloadedStorage.getSource(), type);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
Skript.exception(e, "Failed to initalize database type '" + type + "' ensure constructors are properly created.");
successful = false;
Expand All @@ -270,7 +270,7 @@ public static boolean load() {
// Get the amount of variables currently loaded
int totalVariablesLoaded;
synchronized (TEMP_VARIABLES) {
Map<String, NonNullPair<Object, VariablesStorage>> tvs = TEMP_VARIABLES.get();
Map<String, NonNullPair<Object, VariableStorage>> tvs = TEMP_VARIABLES.get();
assert tvs != null;
totalVariablesLoaded = tvs.size();
}
Expand All @@ -289,7 +289,7 @@ public static boolean load() {
// Get the amount of variables loaded by this variables storage object
int newVariablesLoaded;
synchronized (TEMP_VARIABLES) {
Map<String, NonNullPair<Object, VariablesStorage>> tvs = TEMP_VARIABLES.get();
Map<String, NonNullPair<Object, VariableStorage>> tvs = TEMP_VARIABLES.get();
assert tvs != null;
newVariablesLoaded = tvs.size() - totalVariablesLoaded;
}
Expand Down Expand Up @@ -703,7 +703,7 @@ static void processChangeQueue() {
* <p>
* Access must be synchronised.
*/
private static final SynchronizedReference<Map<String, NonNullPair<Object, VariablesStorage>>> TEMP_VARIABLES =
private static final SynchronizedReference<Map<String, NonNullPair<Object, VariableStorage>>> TEMP_VARIABLES =
new SynchronizedReference<>(new HashMap<>());

/**
Expand All @@ -728,7 +728,7 @@ static void processChangeQueue() {
* Must only be used while variables are loaded
* when Skript is starting. Must be called on Bukkit's main thread.
* This method directly invokes
* {@link VariablesStorage#save(String, String, byte[])},
* {@link VariableStorage#save(String, String, byte[])},
* i.e. you should not be holding any database locks or such
* when calling this!
*
Expand All @@ -737,20 +737,20 @@ static void processChangeQueue() {
* @param source the storage the variable came from.
* @return Whether the variable was stored somewhere. Not valid while storages are loading.
*/
static boolean variableLoaded(String name, @Nullable Object value, VariablesStorage source) {
static boolean variableLoaded(String name, @Nullable Object value, VariableStorage source) {
assert Bukkit.isPrimaryThread(); // required by serialisation

if (value == null)
return false;

synchronized (TEMP_VARIABLES) {
Map<String, NonNullPair<Object, VariablesStorage>> tvs = TEMP_VARIABLES.get();
Map<String, NonNullPair<Object, VariableStorage>> tvs = TEMP_VARIABLES.get();
if (tvs != null) {
NonNullPair<Object, VariablesStorage> existingVariable = tvs.get(name);
NonNullPair<Object, VariableStorage> existingVariable = tvs.get(name);

// Check for conflicts with other storages
conflict: if (existingVariable != null) {
VariablesStorage existingVariableStorage = existingVariable.getSecond();
VariableStorage existingVariableStorage = existingVariable.getSecond();

if (existingVariableStorage == source) {
// No conflict if from the same storage
Expand Down Expand Up @@ -791,7 +791,7 @@ static boolean variableLoaded(String name, @Nullable Object value, VariablesStor

// Move the variable to the right storage
try {
for (VariablesStorage variablesStorage : STORAGES) {
for (VariableStorage variablesStorage : STORAGES) {
if (variablesStorage.accept(name)) {
if (variablesStorage != source) {
// Serialize and set value in new storage
Expand Down Expand Up @@ -831,20 +831,20 @@ private static int onStoragesLoaded() {
Skript.debug("Databases loaded, setting variables...");

synchronized (TEMP_VARIABLES) {
Map<String, NonNullPair<Object, VariablesStorage>> tvs = TEMP_VARIABLES.get();
Map<String, NonNullPair<Object, VariableStorage>> tvs = TEMP_VARIABLES.get();
TEMP_VARIABLES.set(null);
assert tvs != null;

variablesLock.writeLock().lock();
try {
// Calculate the amount of variables that don't have a storage
int unstoredVariables = 0;
for (Entry<String, NonNullPair<Object, VariablesStorage>> tv : tvs.entrySet()) {
for (Entry<String, NonNullPair<Object, VariableStorage>> tv : tvs.entrySet()) {
if (!variableLoaded(tv.getKey(), tv.getValue().getFirst(), tv.getValue().getSecond()))
unstoredVariables++;
}

for (VariablesStorage variablesStorage : STORAGES)
for (VariableStorage variablesStorage : STORAGES)
variablesStorage.allLoaded();

Skript.debug("Variables set. Queue size = " + saveQueue.size());
Expand Down Expand Up @@ -923,7 +923,7 @@ private static void saveVariableChange(String name, @Nullable Object value) {
// Save one variable change
SerializedVariable variable = saveQueue.take();

for (VariablesStorage variablesStorage : STORAGES) {
for (VariableStorage variablesStorage : STORAGES) {
if (variablesStorage.accept(variable.getName())) {
variablesStorage.save(variable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class H2StorageTest {
private final String testSection =
"h2:\n" +
"\tpattern: .*\n" +
"\tmonitor interval: 30 seconds\n" +
"\tfile: ./plugins/Skript/variables\n" +
"\tbackup interval: 0";

Expand All @@ -63,7 +62,6 @@ public void setup() {
database = new H2Storage(Skript.getAddonInstance(), "H2");
SectionNode section = new SectionNode("h2", "", config.getMainNode(), 0);
section.add(new EntryNode("pattern", ".*", section));
section.add(new EntryNode("monitor interval", "30 seconds", section));
section.add(new EntryNode("file", "./plugins/Skript/variables", section));
section.add(new EntryNode("backup interval", "0", section));
assertTrue(database.load_i(section));
Expand All @@ -77,7 +75,6 @@ public void testStorage() throws SQLException, InterruptedException, ExecutionEx
assertTrue(database.save("testing", "string", Classes.serialize("Hello World!").data));
// SerializedVariable result = database.executeTestQuery();
// assertTrue(result != null);
// System.out.println(result.getName());
}
}

Expand Down

0 comments on commit 980ddfd

Please sign in to comment.