Skip to content

Commit

Permalink
TRUNK-5375: Fix to setMemberIds method to bypass time-consuming exist…
Browse files Browse the repository at this point in the history
…ence check within existing cohort membership
  • Loading branch information
mseaton committed Mar 20, 2018
1 parent 6839ebe commit 73fb6cf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
5 changes: 4 additions & 1 deletion api/src/main/java/org/openmrs/Cohort.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,11 @@ public Set<Integer> getMemberIds() {
@Deprecated
public void setMemberIds(Set<Integer> memberIds) {
if (getMemberships().size() == 0) {
Date startDate = new Date();
for (Integer id : memberIds) {
addMembership(new CohortMembership(id));
CohortMembership membership = new CohortMembership(id, startDate);
membership.setCohort(this);
getMemberships().add(membership);
}
}
else {
Expand Down
24 changes: 21 additions & 3 deletions api/src/test/java/org/openmrs/CohortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
*/
package org.openmrs;

import static org.junit.Assert.assertTrue;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import static org.junit.Assert.assertTrue;

/**
* Behavior-driven tests of the Cohort class.
Expand Down Expand Up @@ -147,4 +150,19 @@ public void intersect_shouldContainVoidedAndExpiredMemberships() throws Exceptio
assertTrue(m.getVoided() && m.getEndDate() != null);
});
}

@Test
public void setMemberIds_shouldSupportLargeCohorts() {
int cohortSize = 100000;
Cohort c = new Cohort();
Set<Integer> ids = new HashSet<Integer>();
for (int i=0; i<cohortSize; i++) {
ids.add(i);
}
long startTime = System.currentTimeMillis();
c.setMemberIds(ids);
long endTime = System.currentTimeMillis();
double secondsToSet = (endTime - startTime)/1000;
Assert.assertTrue("Setting cohort of size " + cohortSize + " took " + secondsToSet + " seconds", secondsToSet < 5);
}
}

0 comments on commit 73fb6cf

Please sign in to comment.