Skip to content

Commit

Permalink
Added locking and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachesMLG committed Dec 28, 2024
1 parent c5791dd commit a8b32b7
Showing 1 changed file with 73 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.iridium.iridiumskyblock.managers.tablemanagers;

import com.iridium.iridiumskyblock.IridiumSkyblock;
import com.iridium.iridiumskyblock.managers.DatabaseKey;
import com.iridium.iridiumteams.database.DatabaseObject;
import com.j256.ormlite.dao.Dao;
Expand All @@ -15,13 +16,17 @@
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.stream.Collectors;

public class TableManager<Key, Value extends DatabaseObject, ID> {
private final ConcurrentHashMap<Key, Value> entries = new ConcurrentHashMap<>();
private final Dao<Value, ID> dao;
private final DatabaseKey<Key, Value> databaseKey;
private final static Lock lock = new ReentrantLock();

private final ConnectionSource connectionSource;

Expand All @@ -38,29 +43,83 @@ public TableManager(DatabaseKey<Key, Value> databaseKey, ConnectionSource connec

public void save() {
try {
List<Value> entryList = new ArrayList<>(entries.values());
for (Value t : entryList) {
if (!t.isChanged()) continue;
dao.createOrUpdate(t);
t.setChanged(false);
if (!lock.tryLock(5, TimeUnit.SECONDS)) {
IridiumSkyblock.getInstance().getLogger().warning("Warning: Lock acquisition took more than 5 second in save() method.");
}
dao.commit(getDatabaseConnection());
} catch (SQLException throwables) {
throwables.printStackTrace();
try {
List<Value> entryList = new ArrayList<>(entries.values());
boolean modified = false;
for (Value t : entryList) {
if (!t.isChanged()) continue;
modified = true;
dao.createOrUpdate(t);
t.setChanged(false);
}
if (modified) dao.commit(getDatabaseConnection());
} finally {
lock.unlock();
}
} catch (InterruptedException | SQLException e) {
e.printStackTrace();
}
}

public void save(Value value) {
try {
if (!value.isChanged()) return;
dao.createOrUpdate(value);
dao.commit(getDatabaseConnection());
value.setChanged(false);
} catch (SQLException throwables) {
throwables.printStackTrace();
if (!lock.tryLock(5, TimeUnit.SECONDS)) {
IridiumSkyblock.getInstance().getLogger().warning("Warning: Lock acquisition took more than 5 second in save(value) method.");
}
try {
if (!value.isChanged()) return;
dao.createOrUpdate(value);
dao.commit(getDatabaseConnection());
value.setChanged(false);
} finally {
lock.unlock();
}
} catch (InterruptedException | SQLException e) {
e.printStackTrace();
}
}

public CompletableFuture<Void> delete(Value value) {
entries.remove(databaseKey.getKey(value));
return CompletableFuture.runAsync(() -> {
try {
if (!lock.tryLock(5, TimeUnit.SECONDS)) {
IridiumSkyblock.getInstance().getLogger().warning("Warning: Lock acquisition took more than 5 second in delete(value) method.");
}
try {
dao.delete(value);
dao.commit(getDatabaseConnection());
} finally {
lock.unlock();
}
} catch (InterruptedException | SQLException e) {
e.printStackTrace();
}
});
}

public CompletableFuture<Void> delete(Collection<Value> values) {
values.forEach(value -> entries.remove(databaseKey.getKey(value)));
return CompletableFuture.runAsync(() -> {
try {
if (!lock.tryLock(5, TimeUnit.SECONDS)) {
IridiumSkyblock.getInstance().getLogger().warning("Warning: Lock acquisition took more than 5 second in delete(Collection<value>) method.");
}
try {
dao.delete(values);
dao.commit(getDatabaseConnection());
} finally {
lock.unlock();
}
} catch (InterruptedException | SQLException e) {
e.printStackTrace();
}
});
}

public void addEntry(Value value) {
entries.put(databaseKey.getKey(value), value);
}
Expand All @@ -85,30 +144,6 @@ public Optional<Value> getEntry(Function<? super Value, Boolean> searchFunction)
return entries.values().stream().filter(searchFunction::apply).findFirst();
}

public CompletableFuture<Void> delete(Value value) {
entries.remove(databaseKey.getKey(value));
return CompletableFuture.runAsync(() -> {
try {
dao.delete(value);
dao.commit(getDatabaseConnection());
} catch (SQLException throwables) {
throwables.printStackTrace();
}
});
}

public CompletableFuture<Void> delete(Collection<Value> values) {
values.forEach(value -> entries.remove(databaseKey.getKey(value)));
return CompletableFuture.runAsync(() -> {
try {
dao.delete(values);
dao.commit(getDatabaseConnection());
} catch (SQLException throwables) {
throwables.printStackTrace();
}
});
}

private DatabaseConnection getDatabaseConnection() throws SQLException {
return connectionSource.getReadWriteConnection(null);
}
Expand Down

0 comments on commit a8b32b7

Please sign in to comment.