jdb: Add countDocuments method

This commit is contained in:
Oliver
2025-11-20 11:06:00 +01:00
parent 6fe7cbabbd
commit ec842f7287
3 changed files with 38 additions and 1 deletions

View File

@@ -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.
*

View File

@@ -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