Skip to content

Commit

Permalink
feat(database): loadAll() method now returns the number of loaded ent…
Browse files Browse the repository at this point in the history
…ities

The `loadAll()` method in `EntityRepository.java` has been updated to return the number of loaded entities from the database.
  • Loading branch information
GeorgeV220 committed Jun 22, 2024
1 parent 04d12dc commit 8d1359f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -61,8 +62,10 @@ public interface EntityRepository<V extends Entity> {

/**
* Loads all entities from the database.
*
* @return The number of loaded entities.
*/
void loadAll();
CompletableFuture<BigInteger> loadAll();

/**
* Saves all loaded entities to the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import org.jetbrains.annotations.UnmodifiableView;

import java.io.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -154,14 +157,29 @@ public CompletableFuture<Void> delete(@NotNull String entityId) {
* Loads all entities from the data folder.
*/
@Override
public void loadAll() {
File[] files = dataFolder.listFiles((dir, name) -> name.endsWith(".json"));
if (files != null) {
public CompletableFuture<BigInteger> loadAll() {
return CompletableFuture.supplyAsync(() -> {
File[] files = dataFolder.listFiles((dir, name) -> name.endsWith(".json"));
if (files == null) {
return CompletableFuture.completedFuture(BigInteger.ZERO);
}

AtomicReference<BigInteger> atomicCount = new AtomicReference<>(BigInteger.ZERO);
List<CompletableFuture<Void>> futures = new ArrayList<>();

for (File file : files) {
String entityId = file.getName().replace(".json", "");
load(entityId);
CompletableFuture<Void> future = load(entityId).thenAccept(v -> {
if (v != null) {
atomicCount.updateAndGet(current -> current.add(BigInteger.ONE));
}
});
futures.add(future);
}
}

CompletableFuture<Void> allOf = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
return allOf.thenApply(v -> atomicCount.get());
}).thenCompose(countFuture -> countFuture);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import org.jetbrains.annotations.UnmodifiableView;

import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -175,16 +178,27 @@ public CompletableFuture<Void> delete(@NotNull String entityId) {
* Loads all entities from the database.
*/
@Override
public void loadAll() {
FindIterable<Document> documents = mongoDatabase.getCollection(this.collectionName).find();

for (Document document : documents) {
Object idValue = document.get("_id");

if (idValue != null) {
this.load(idValue.toString());
public CompletableFuture<BigInteger> loadAll() {
return CompletableFuture.supplyAsync(() -> {
FindIterable<Document> documents = mongoDatabase.getCollection(this.collectionName).find();
List<CompletableFuture<Void>> futures = new ArrayList<>();
AtomicReference<BigInteger> count = new AtomicReference<>(BigInteger.ZERO);

for (Document document : documents) {
Object idValue = document.get("_id");
if (idValue != null) {
CompletableFuture<Void> future = load(idValue.toString()).thenAccept(v -> {
if (v != null) {
count.updateAndGet(current -> current.add(BigInteger.ONE));
}
});
futures.add(future);
}
}
}

CompletableFuture<Void> allOf = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
return allOf.thenApply(v -> count.get());
}).thenCompose(countFuture -> countFuture);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import org.jetbrains.annotations.UnmodifiableView;

import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -239,25 +242,40 @@ public CompletableFuture<Void> delete(@NotNull String entityId) {
* Loads all entities from the database.
*/
@Override
public void loadAll() {
String statement = "SELECT * FROM " + this.tableName;
try (PreparedStatement preparedStatement = this.querySQL(statement)) {
if (preparedStatement == null) {
this.logger.log(Level.SEVERE, "Failed to create statement for loading all entities.");
return;
}
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet == null) {
this.logger.log(Level.SEVERE, "Failed to load all entities because the result set was null.");
return;
public CompletableFuture<BigInteger> loadAll() {
return CompletableFuture.supplyAsync(() -> {
String statement = "SELECT * FROM " + this.tableName;
List<CompletableFuture<Void>> futures = new ArrayList<>();
AtomicReference<BigInteger> count = new AtomicReference<>(BigInteger.ZERO);

try (PreparedStatement preparedStatement = this.querySQL(statement)) {
if (preparedStatement == null) {
this.logger.log(Level.SEVERE, "Failed to create statement for loading all entities.");
return CompletableFuture.completedFuture(BigInteger.ZERO);
}
while (resultSet.next()) {
this.load(resultSet.getString("_id"));
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet == null) {
this.logger.log(Level.SEVERE, "Failed to load all entities because the result set was null.");
return CompletableFuture.completedFuture(BigInteger.ZERO);
}
while (resultSet.next()) {
String id = resultSet.getString("_id");
CompletableFuture<Void> future = load(id).thenAccept(v -> {
if (v != null) {
count.updateAndGet(current -> current.add(BigInteger.ONE));
}
});
futures.add(future);
}
}
} catch (SQLException e) {
this.logger.log(Level.SEVERE, "[EntityRepository]:", e);
return CompletableFuture.completedFuture(BigInteger.ZERO);
}
} catch (SQLException e) {
this.logger.log(Level.SEVERE, "[EntityRepository]:", e);
}

CompletableFuture<Void> allOf = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
return allOf.thenApply(v -> count.get());
}).thenCompose(countFuture -> countFuture);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -167,14 +170,29 @@ public CompletableFuture<Void> delete(@NotNull String entityId) {
* Loads all entities from the data folder.
*/
@Override
public void loadAll() {
File[] files = dataFolder.listFiles((dir, name) -> name.endsWith(".yml"));
if (files != null) {
public CompletableFuture<BigInteger> loadAll() {
return CompletableFuture.supplyAsync(() -> {
File[] files = dataFolder.listFiles((dir, name) -> name.endsWith(".yml"));
if (files == null) {
return CompletableFuture.completedFuture(BigInteger.ZERO);
}

AtomicReference<BigInteger> atomicCount = new AtomicReference<>(BigInteger.ZERO);
List<CompletableFuture<Void>> futures = new ArrayList<>();

for (File file : files) {
String entityId = file.getName().replace(".yml", "");
load(entityId);
CompletableFuture<Void> future = load(entityId).thenAccept(v -> {
if (v != null) {
atomicCount.updateAndGet(current -> current.add(BigInteger.ONE));
}
});
futures.add(future);
}
}

CompletableFuture<Void> allOf = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
return allOf.thenApply(v -> atomicCount.get());
}).thenCompose(countFuture -> countFuture);
}

/**
Expand Down

0 comments on commit 8d1359f

Please sign in to comment.