From ec842f72871ce7a014ab820f5c9d5e8f932a4e6f Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 20 Nov 2025 11:06:00 +0100 Subject: [PATCH] jdb: Add countDocuments method --- gradle.properties | 2 +- .../jdb/src/main/java/de/oliver/jdb/JDB.java | 20 +++++++++++++++++++ .../src/test/java/de/oliver/jdb/JDBTest.java | 17 ++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 8b76714a..fb698d92 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ fancylibVersion=37 fancysitulaVersion=0.0.13 -jdbVersion=1.0.1 +jdbVersion=1.0.2 plugintestsVersion=1.0.0 org.gradle.parallel=false org.gradle.caching=true \ No newline at end of file diff --git a/libraries/jdb/src/main/java/de/oliver/jdb/JDB.java b/libraries/jdb/src/main/java/de/oliver/jdb/JDB.java index 40f672fe..8297eb75 100644 --- a/libraries/jdb/src/main/java/de/oliver/jdb/JDB.java +++ b/libraries/jdb/src/main/java/de/oliver/jdb/JDB.java @@ -95,6 +95,26 @@ public class JDB { return documents; } + /** + * Counts the number of documents in the specified directory path. + * + * @param path the relative directory path + * @return the number of documents in the directory + */ + public int countDocuments(@NotNull String path) { + File directory = new File(baseDirectory, path); + if (!directory.exists()) { + return 0; + } + + File[] files = directory.listFiles(); + if (files == null) { + return 0; + } + + return files.length; + } + /** * Saves the given value as a document at the specified path. * diff --git a/libraries/jdb/src/test/java/de/oliver/jdb/JDBTest.java b/libraries/jdb/src/test/java/de/oliver/jdb/JDBTest.java index 968e853e..a4f5032c 100644 --- a/libraries/jdb/src/test/java/de/oliver/jdb/JDBTest.java +++ b/libraries/jdb/src/test/java/de/oliver/jdb/JDBTest.java @@ -110,6 +110,23 @@ public class JDBTest { assertTrue(result.isEmpty()); } + @Test + public void testCountDocuments() throws IOException { + // Prepare + String basePath = "./test_files/"; + JDB jdb = new JDB(basePath); + String path = "test_files"; + jdb.set(path + "/obj1", "Test message 1"); + jdb.set(path + "/obj2", "Test message 2"); + jdb.set(path + "/obj3", "Test message 3"); + + // Act + int count = jdb.countDocuments(path); + + // Assert + assertEquals(3, count); + } + @Test public void testSetNewObject() throws IOException { // Prepare