Skip to content

Commit 2fdccfd

Browse files
authored
make sure Suggester#init() does not receive duplicate entries (#3930)
1 parent afcc90f commit 2fdccfd

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

opengrok-web/src/main/java/org/opengrok/web/api/v1/suggester/provider/service/impl/SuggesterServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.web.api.v1.suggester.provider.service.impl;
2424

@@ -331,7 +331,7 @@ private Collection<NamedIndexDir> getAllProjectIndexDirs() {
331331
return env.getProjectList().stream()
332332
.filter(Project::isIndexed)
333333
.map(this::getNamedIndexDir)
334-
.collect(Collectors.toList());
334+
.collect(Collectors.toSet());
335335
} else {
336336
Path indexDir = Paths.get(env.getDataRootPath(), IndexDatabase.INDEX_DIR);
337337
return Collections.singleton(new NamedIndexDir("", indexDir));

suggester/src/main/java/org/opengrok/suggest/Suggester.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.suggest;
2424

@@ -48,6 +48,7 @@
4848
import java.util.List;
4949
import java.util.Map;
5050
import java.util.Map.Entry;
51+
import java.util.Objects;
5152
import java.util.Set;
5253
import java.util.concurrent.Callable;
5354
import java.util.concurrent.ConcurrentHashMap;
@@ -745,6 +746,25 @@ public Path getPath() {
745746
public String toString() {
746747
return name;
747748
}
749+
750+
@Override
751+
public boolean equals(Object obj) {
752+
if (obj == null) {
753+
return false;
754+
}
755+
756+
if (!(obj instanceof NamedIndexDir)) {
757+
return false;
758+
}
759+
760+
NamedIndexDir that = (NamedIndexDir) obj;
761+
return (this.name.equals(that.name) && this.path.equals(that.path));
762+
}
763+
764+
@Override
765+
public int hashCode() {
766+
return Objects.hash(this.name, this.path);
767+
}
748768
}
749769

750770
/**

suggester/src/test/java/org/opengrok/suggest/SuggesterTest.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.suggest;
2424

@@ -60,11 +60,13 @@
6060
import static org.hamcrest.MatcherAssert.assertThat;
6161
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
6262
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
63+
import static org.junit.jupiter.api.Assertions.assertEquals;
6364
import static org.junit.jupiter.api.Assertions.assertFalse;
65+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
6466
import static org.junit.jupiter.api.Assertions.assertThrows;
6567
import static org.junit.jupiter.api.Assertions.assertTrue;
6668

67-
public class SuggesterTest {
69+
class SuggesterTest {
6870

6971
private final MeterRegistry registry = new SimpleMeterRegistry();
7072

@@ -101,13 +103,13 @@ private Suggester.NamedIndexReader getNamedIndexReader() throws IOException {
101103
}
102104

103105
@Test
104-
public void testNullSuggesterDir() {
106+
void testNullSuggesterDir() {
105107
assertThrows(IllegalArgumentException.class,
106108
() -> new Suggester(null, 10, Duration.ofMinutes(5), false, true, null, Integer.MAX_VALUE, 1, registry));
107109
}
108110

109111
@Test
110-
public void testNullDuration() {
112+
void testNullDuration() {
111113
assertThrows(IllegalArgumentException.class, () -> {
112114
Path tempFile = Files.createTempFile("opengrok", "test");
113115
try {
@@ -119,7 +121,7 @@ public void testNullDuration() {
119121
}
120122

121123
@Test
122-
public void testNegativeDuration() {
124+
void testNegativeDuration() {
123125
assertThrows(IllegalArgumentException.class, () -> {
124126
Path tempFile = Files.createTempFile("opengrok", "test");
125127
try {
@@ -172,7 +174,7 @@ private static int getSuggesterProjectDataSize(final Suggester suggester) throws
172174
}
173175

174176
@Test
175-
public void testSimpleSuggestions() throws IOException {
177+
void testSimpleSuggestions() throws IOException {
176178
SuggesterTestData t = initSuggester();
177179

178180
Suggester.NamedIndexReader ir = t.getNamedIndexReader();
@@ -187,7 +189,7 @@ public void testSimpleSuggestions() throws IOException {
187189
}
188190

189191
@Test
190-
public void testRefresh() throws IOException {
192+
void testRefresh() throws IOException {
191193
SuggesterTestData t = initSuggester();
192194

193195
addText(t.getIndexDirectory(), "a1 a2");
@@ -206,7 +208,7 @@ public void testRefresh() throws IOException {
206208
}
207209

208210
@Test
209-
public void testIndexChangedWhileOffline() throws IOException {
211+
void testIndexChangedWhileOffline() throws IOException {
210212
SuggesterTestData t = initSuggester();
211213

212214
t.s.close();
@@ -233,7 +235,7 @@ public void testIndexChangedWhileOffline() throws IOException {
233235
}
234236

235237
@Test
236-
public void testRemove() throws IOException {
238+
void testRemove() throws IOException {
237239
SuggesterTestData t = initSuggester();
238240

239241
t.s.remove(Collections.singleton("test"));
@@ -245,7 +247,7 @@ public void testRemove() throws IOException {
245247
}
246248

247249
@Test
248-
public void testComplexQuerySearch() throws IOException {
250+
void testComplexQuerySearch() throws IOException {
249251
SuggesterTestData t = initSuggester();
250252

251253
List<LookupResultItem> res = t.s.search(Collections.singletonList(t.getNamedIndexReader()),
@@ -259,7 +261,7 @@ public void testComplexQuerySearch() throws IOException {
259261

260262
@Test
261263
@SuppressWarnings("unchecked") // for contains()
262-
public void testOnSearch() throws IOException {
264+
void testOnSearch() throws IOException {
263265
SuggesterTestData t = initSuggester();
264266

265267
Query q = new BooleanQuery.Builder()
@@ -278,7 +280,7 @@ public void testOnSearch() throws IOException {
278280
}
279281

280282
@Test
281-
public void testGetSearchCountsForUnknown() throws IOException {
283+
void testGetSearchCountsForUnknown() throws IOException {
282284
SuggesterTestData t = initSuggester();
283285

284286
assertTrue(t.s.getSearchCounts("unknown", "unknown", 0, 10).isEmpty());
@@ -288,7 +290,7 @@ public void testGetSearchCountsForUnknown() throws IOException {
288290

289291
@Test
290292
@SuppressWarnings("unchecked") // for contains()
291-
public void testIncreaseSearchCount() throws IOException {
293+
void testIncreaseSearchCount() throws IOException {
292294
SuggesterTestData t = initSuggester();
293295

294296
t.s.increaseSearchCount("test", new Term("test", "term2"), 100, true);
@@ -300,4 +302,24 @@ public void testIncreaseSearchCount() throws IOException {
300302
t.close();
301303
}
302304

305+
@Test
306+
void testNamedIndexDirEquals() {
307+
Suggester.NamedIndexDir namedIndexDir1 = new Suggester.NamedIndexDir("foo", Path.of("/foo"));
308+
Suggester.NamedIndexDir namedIndexDir2 = new Suggester.NamedIndexDir("foo", Path.of("/foo"));
309+
assertEquals(namedIndexDir1, namedIndexDir2);
310+
}
311+
312+
@Test
313+
void testNamedIndexDirNotEqualsName() {
314+
Suggester.NamedIndexDir namedIndexDir1 = new Suggester.NamedIndexDir("foo", Path.of("/foo"));
315+
Suggester.NamedIndexDir namedIndexDir2 = new Suggester.NamedIndexDir("bar", Path.of("/foo"));
316+
assertNotEquals(namedIndexDir1, namedIndexDir2);
317+
}
318+
319+
@Test
320+
void testNamedIndexDirNotEqualsPath() {
321+
Suggester.NamedIndexDir namedIndexDir1 = new Suggester.NamedIndexDir("foo", Path.of("/foo"));
322+
Suggester.NamedIndexDir namedIndexDir2 = new Suggester.NamedIndexDir("foo", Path.of("/bar"));
323+
assertNotEquals(namedIndexDir1, namedIndexDir2);
324+
}
303325
}

0 commit comments

Comments
 (0)