-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDatabaseSearchService.java
61 lines (54 loc) · 2.04 KB
/
DatabaseSearchService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package io.oneko.search.impl;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import io.micrometer.core.instrument.MeterRegistry;
import io.oneko.event.EventDispatcher;
import io.oneko.project.ProjectRepository;
import io.oneko.project.event.ProjectDeletedEvent;
import io.oneko.project.event.ProjectSavedEvent;
import io.oneko.search.MeasuringSearchService;
import io.oneko.search.ProjectSearchResultEntry;
import io.oneko.search.SearchResult;
import io.oneko.search.VersionSearchResultEntry;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
@Service
@ConditionalOnProperty(value = "o-neko.search.meilisearch.enabled", havingValue = "false", matchIfMissing = true)
public class DatabaseSearchService extends MeasuringSearchService {
private final ProjectRepository projectRepository;
private final Cache<String, SearchResult> queryResultCache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.maximumSize(512)
.build();
public DatabaseSearchService(ProjectRepository projectRepository,
EventDispatcher eventDispatcher, MeterRegistry meterRegistry) {
super(meterRegistry);
this.projectRepository = projectRepository;
eventDispatcher.registerListener(event -> {
if (event instanceof ProjectSavedEvent) {
queryResultCache.invalidateAll();
} else if (event instanceof ProjectDeletedEvent) {
queryResultCache.invalidateAll();
}
});
}
@Override
public SearchResult findProjectsAndVersionsInternal(String searchTerm) {
return queryResultCache.get(searchTerm, s -> {
var versions = projectRepository.findProjectVersion(searchTerm)
.stream()
.map(VersionSearchResultEntry::of)
.toList();
var projects = projectRepository.findProject(searchTerm)
.stream()
.map(ProjectSearchResultEntry::of)
.toList();
return SearchResult.builder()
.query(searchTerm)
.projects(projects)
.versions(versions)
.build();
});
}
}