Skip to content

Commit 83802f1

Browse files
committed
Add more tests
1 parent 8f02ad3 commit 83802f1

File tree

6 files changed

+291
-72
lines changed

6 files changed

+291
-72
lines changed

src/main/java/org/ohdsi/usagi/BerkeleyDbEngine.java

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,45 +78,64 @@ public class BerkeleyDbStats {
7878

7979
public BerkeleyDbStats getStats() {
8080
BerkeleyDbStats berkeleyDbStats = new BerkeleyDbStats();
81-
berkeleyDbStats.conceptCount = store.getPrimaryIndex(Integer.class, Concept.class).count();
82-
berkeleyDbStats.mapsToRelationshipCount = store.getPrimaryIndex(Integer.class, MapsToRelationship.class).count();
83-
berkeleyDbStats.parentChildCount = store.getPrimaryIndex(Integer.class, ParentChildRelationShip.class).count();
81+
if (isOpenForReading || isOpenForWriting) {
82+
berkeleyDbStats.conceptCount = store.getPrimaryIndex(Integer.class, Concept.class).count();
83+
berkeleyDbStats.mapsToRelationshipCount = store.getPrimaryIndex(Integer.class, MapsToRelationship.class).count();
84+
berkeleyDbStats.parentChildCount = store.getPrimaryIndex(Integer.class, ParentChildRelationShip.class).count();
85+
}
8486
return berkeleyDbStats;
8587
}
8688

8789
public void put(Concept concept) {
88-
conceptDataAccessor.primaryIndex.putNoReturn(concept);
90+
if (isOpenForWriting) {
91+
conceptDataAccessor.primaryIndex.putNoReturn(concept);
92+
}
8993
}
9094

9195
public void put(MapsToRelationship mapsToRelationship) {
92-
mapsToRelationshipDataAccessor.primaryIndex.putNoReturn(mapsToRelationship);
96+
if (isOpenForWriting) {
97+
mapsToRelationshipDataAccessor.primaryIndex.putNoReturn(mapsToRelationship);
98+
}
9399
}
94100

95101
public void putAtcToRxNorm(String atc, int conceptId) {
96-
AtcToRxNorm atcToRxNorm = atcToRxNormDataAccessor.primaryIndex.get(atc);
97-
if (atcToRxNorm == null) {
98-
atcToRxNorm = new AtcToRxNorm(atc);
102+
if (isOpenForWriting) {
103+
AtcToRxNorm atcToRxNorm = atcToRxNormDataAccessor.primaryIndex.get(atc);
104+
if (atcToRxNorm == null) {
105+
atcToRxNorm = new AtcToRxNorm(atc);
106+
}
107+
atcToRxNorm.conceptIds.add(conceptId);
108+
atcToRxNormDataAccessor.primaryIndex.putNoReturn(atcToRxNorm);
99109
}
100-
atcToRxNorm.conceptIds.add(conceptId);
101-
atcToRxNormDataAccessor.primaryIndex.putNoReturn(atcToRxNorm);
102110
}
103111

104112
public void put(ParentChildRelationShip parentChildRelationship) {
105-
parentChildRelationshipDataAccessor.primaryIndex.putNoReturn(parentChildRelationship);
113+
if (isOpenForWriting) {
114+
parentChildRelationshipDataAccessor.primaryIndex.putNoReturn(parentChildRelationship);
115+
}
106116
}
107117

108118
public EntityCursor<Concept> getConceptCursor() {
109-
return conceptDataAccessor.primaryIndex.entities();
119+
if (isOpenForReading || isOpenForWriting) {
120+
return conceptDataAccessor.primaryIndex.entities();
121+
}
122+
return null;
110123
}
111124

112125
public MapsToRelationship getMapsToRelationship(int conceptId) {
113-
return mapsToRelationshipDataAccessor.primaryIndex.get(conceptId);
126+
if (isOpenForReading || isOpenForWriting) {
127+
return mapsToRelationshipDataAccessor.primaryIndex.get(conceptId);
128+
}
129+
return null;
114130
}
115131

116132
public List<MapsToRelationship> getMapsToRelationshipsByConceptId2(int conceptId) {
133+
List<MapsToRelationship> relationships = new ArrayList<MapsToRelationship>();
134+
if (!(isOpenForReading || isOpenForWriting)) {
135+
return relationships;
136+
}
117137
EntityIndex<Integer, MapsToRelationship> subIndex = mapsToRelationshipDataAccessor.secondaryIndex.subIndex(conceptId);
118138
EntityCursor<MapsToRelationship> cursor = subIndex.entities();
119-
List<MapsToRelationship> relationships = new ArrayList<MapsToRelationship>();
120139
try {
121140
for (MapsToRelationship relationship : cursor)
122141
relationships.add(relationship);
@@ -127,17 +146,23 @@ public List<MapsToRelationship> getMapsToRelationshipsByConceptId2(int conceptId
127146
}
128147

129148
public Set<Integer> getRxNormConceptIds(String atc) {
130-
AtcToRxNorm atcToRxNorm = atcToRxNormDataAccessor.primaryIndex.get(atc);
131-
if (atcToRxNorm == null)
132-
return Collections.emptySet();
133-
else
134-
return atcToRxNorm.conceptIds;
149+
if (isOpenForReading || isOpenForWriting) {
150+
AtcToRxNorm atcToRxNorm = atcToRxNormDataAccessor.primaryIndex.get(atc);
151+
if (atcToRxNorm == null)
152+
return Collections.emptySet();
153+
else
154+
return atcToRxNorm.conceptIds;
155+
}
156+
return Collections.emptySet();
135157
}
136158

137159
public List<ParentChildRelationShip> getParentChildRelationshipsByParentConceptId(int conceptId) {
160+
List<ParentChildRelationShip> relationships = new ArrayList<ParentChildRelationShip>();
161+
if (!(isOpenForReading || isOpenForWriting)) {
162+
return relationships;
163+
}
138164
EntityIndex<Integer, ParentChildRelationShip> subIndex = parentChildRelationshipDataAccessor.secondaryIndexParent.subIndex(conceptId);
139165
EntityCursor<ParentChildRelationShip> cursor = subIndex.entities();
140-
List<ParentChildRelationShip> relationships = new ArrayList<ParentChildRelationShip>();
141166
try {
142167
for (ParentChildRelationShip relationship : cursor)
143168
relationships.add(relationship);
@@ -148,9 +173,12 @@ public List<ParentChildRelationShip> getParentChildRelationshipsByParentConceptI
148173
}
149174

150175
public List<ParentChildRelationShip> getParentChildRelationshipsByChildConceptId(int conceptId) {
176+
List<ParentChildRelationShip> relationships = new ArrayList<ParentChildRelationShip>();
177+
if (!(isOpenForReading || isOpenForWriting)) {
178+
return relationships;
179+
}
151180
EntityIndex<Integer, ParentChildRelationShip> subIndex = parentChildRelationshipDataAccessor.secondaryIndexChild.subIndex(conceptId);
152181
EntityCursor<ParentChildRelationShip> cursor = subIndex.entities();
153-
List<ParentChildRelationShip> relationships = new ArrayList<ParentChildRelationShip>();
154182
try {
155183
for (ParentChildRelationShip relationship : cursor)
156184
relationships.add(relationship);
@@ -161,14 +189,19 @@ public List<ParentChildRelationShip> getParentChildRelationshipsByChildConceptId
161189
}
162190

163191
public Concept getConcept(int conceptId) {
164-
return conceptDataAccessor.primaryIndex.get(conceptId);
192+
if (isOpenForReading || isOpenForWriting) {
193+
return conceptDataAccessor.primaryIndex.get(conceptId);
194+
}
195+
return null;
165196
}
166197

167198
public void shutdown() throws DatabaseException {
168199
try {
169200
if (isOpenForReading || isOpenForWriting) {
170201
store.close();
171202
dbEnvironment.close();
203+
isOpenForReading = false;
204+
isOpenForWriting = false;
172205
}
173206
} catch (DatabaseException dbe) {
174207
throw new RuntimeException(dbe);

src/main/java/org/ohdsi/usagi/UsagiSearchEngine.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ public List<ScoredConcept> search(String searchTerm, boolean useMlt, Collection<
361361
Document document = reader.document(scoreDoc.doc);
362362
int conceptId = Integer.parseInt(document.get("CONCEPT_ID"));
363363
Concept targetConcept = Global.dbEngine.getConcept(conceptId);
364+
if (targetConcept == null) {
365+
continue;
366+
}
364367
String term = document.get("TERM");
365368
// If matchscore = 0 but it was the one concept that was automatically selected, still allow it:
366369
if (scoreDoc.score > 0 || (filterConceptIds != null && filterConceptIds.size() == 1 && filterConceptIds.contains(targetConcept.conceptId)))
@@ -595,7 +598,7 @@ private double idf(int docFreq, int d) {
595598
}
596599

597600
public boolean isOpenForSearching() {
598-
return (reader != null);
601+
return (reader != null && Global.dbEngine != null);
599602
}
600603
}
601604

src/main/java/org/ohdsi/usagi/ui/MappingDetailPanel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ private Component createQueryPanel() {
138138
c.weightx = 1;
139139
c.gridwidth = GridBagConstraints.REMAINDER;
140140
manualQueryField = new JTextField("");
141+
manualQueryField.setName("manualQueryField");
141142
// manualQueryField.setPreferredSize(new Dimension(200, 5));
142143
manualQueryField.getDocument().addDocumentListener(new DocumentListener() {
143144

@@ -169,6 +170,7 @@ private Component createSearchResultsPanel() {
169170
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
170171
searchTableModel = new ConceptTableModel(true);
171172
searchTable = new UsagiTable(searchTableModel);
173+
searchTable.setName("searchResultsTable");
172174
sorter = new TableRowSorter<ConceptTableModel>(searchTableModel);
173175
searchTable.setRowSorter(sorter);
174176
searchTable.setPreferredScrollableViewportSize(new Dimension(100, 100));

src/main/java/org/ohdsi/usagi/ui/UsagiMain.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public void windowClosing(WindowEvent e) {
130130
}
131131
});
132132
frame.setLayout(new BorderLayout());
133+
frame.setName("UsagiMainFrame");
133134
frame.setJMenuBar(new UsagiMenubar());
134135

135136
JPanel main = new JPanel();

src/test/java/org/ohdsi/usagi/UsagiSearchEngineTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class UsagiSearchEngineTest {
3030
void setUp() throws IOException {
3131
testFolder = tempDir.toString();
3232
Global.folder = testFolder;
33+
Global.dbEngine = new BerkeleyDbEngine(testFolder);
34+
Global.dbEngine.createDatabase();
3335
searchEngine = new UsagiSearchEngine(testFolder);
3436
}
3537

@@ -38,6 +40,9 @@ void tearDown() {
3840
if (searchEngine != null) {
3941
searchEngine.close();
4042
}
43+
if (Global.dbEngine != null) {
44+
Global.dbEngine.shutdown();
45+
}
4146
}
4247

4348
@Test

0 commit comments

Comments
 (0)