Commit aee298bb authored by Georgi's avatar Georgi

Added a method findDistinctEntries() which takes a String for a given field...

Added a method findDistinctEntries() which takes a String for a given field and returns an Array of all distinct values for that field from the database. To be used to retrieve all distinct entries for field "LABEL", to know what type of evidences are present in the database. Added a unit test method for this new method.
parent 45f5e66e
Pipeline #2441 passed with stages
in 6 minutes and 22 seconds
......@@ -21,6 +21,7 @@ import be.cylab.mark.core.Evidence;
import be.cylab.mark.core.RawData;
import be.cylab.mark.core.SubjectAdapter;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCursor;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.util.Collections;
......@@ -489,6 +490,23 @@ public class RequestHandler implements ServerInterface {
return unique_subjects;
}
public final String[] findDistinctEntries(final String field) {
List<String> entries = new ArrayList<>();
try (MongoCursor<String> cursor = mongodb
.getCollection(COLLECTION_EVIDENCE)
.distinct(field, String.class).iterator()) {
while (cursor.hasNext()) {
String temp_entry = cursor.next();
entries.add(temp_entry);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return entries.toArray(new String[entries.size()]);
}
/**
* {@inheritDoc}
*
......
......@@ -106,6 +106,30 @@ public class RequestHandlerTest extends TestCase {
assertEquals(0, evidences.length);
}
public void testfindDistinctEntries() throws Throwable {
RequestHandler rq = this.getRequestHandler();
String label_field = "LABEL";
String name_field = "name";
Evidence ev = new Evidence();
ev.setLabel("test");
ev.setReport("Some report...");
ev.setScore(0.99);
ev.setSubject(new DummySubject("test"));
ev.setTime(123456);
rq.addEvidence(ev);
ev.setLabel("test2");
ev.setSubject(new DummySubject("test"));
rq.addEvidence(ev);
String[] distinct_labels = rq.findDistinctEntries(label_field);
assertEquals(2, distinct_labels.length);
String[] distinct_subjects = rq.findDistinctEntries(name_field);
assertEquals(1, distinct_subjects.length);
}
private RequestHandler getRequestHandler() {
String mongo_host = System.getenv(Config.ENV_MONGO_HOST);
if (mongo_host == null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment