Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ViewSummaryService to not extend PaginatedDbService #21292

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.graylog.plugins.views.search.db;

import jakarta.inject.Inject;
import jakarta.inject.Named;
import org.graylog.plugins.views.search.views.ViewResolver;
import org.graylog.plugins.views.search.views.ViewSummaryDTO;
import org.graylog.plugins.views.search.views.ViewSummaryService;
Expand All @@ -25,13 +27,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.inject.Inject;
import jakarta.inject.Named;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SearchesCleanUpJob extends Periodical {
private static final Logger LOG = LoggerFactory.getLogger(SearchesCleanUpJob.class);
Expand Down Expand Up @@ -104,7 +104,9 @@ public void doRun() {

private Set<String> findReferencedSearchIds() {
final HashSet<String> toKeepViewIds = new HashSet<>();
toKeepViewIds.addAll(viewSummaryService.streamAll().map(ViewSummaryDTO::searchId).collect(Collectors.toSet()));
try (final Stream<ViewSummaryDTO> stream = viewSummaryService.streamAll()) {
toKeepViewIds.addAll(stream.map(ViewSummaryDTO::searchId).collect(Collectors.toSet()));
}
toKeepViewIds.addAll(viewResolvers
.values().stream().flatMap(vr -> vr.getSearchIds().stream()).collect(Collectors.toSet()));
toKeepViewIds.addAll(staticReferencedSearches.stream().map(StaticReferencedSearch::id).toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,34 @@
*/
package org.graylog.plugins.views.search.views;

import com.google.errorprone.annotations.MustBeClosed;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import jakarta.inject.Inject;
import org.bson.conversions.Bson;
import org.graylog.plugins.views.search.permissions.SearchUser;
import org.graylog2.bindings.providers.MongoJackObjectMapperProvider;
import org.graylog2.database.MongoCollections;
import org.graylog2.database.MongoConnection;
import org.graylog2.database.PaginatedDbService;
import org.graylog2.database.PaginatedList;
import org.graylog2.database.utils.MongoUtils;
import org.graylog2.rest.models.SortOrder;

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static com.google.common.base.Preconditions.checkNotNull;

public class ViewSummaryService extends PaginatedDbService<ViewSummaryDTO> implements ViewUtils<ViewSummaryDTO> {
public class ViewSummaryService implements ViewUtils<ViewSummaryDTO> {
private static final String COLLECTION_NAME = "views";

private final MongoCollection<ViewSummaryDTO> collection;
private final MongoUtils<ViewSummaryDTO> mongoUtils;

@Inject
protected ViewSummaryService(MongoConnection mongoConnection,
MongoJackObjectMapperProvider mongoJackObjectMapperProvider,
MongoCollections mongoCollections) {
super(mongoConnection, mongoJackObjectMapperProvider, ViewSummaryDTO.class, COLLECTION_NAME);
this.collection = mongoCollections.collection(COLLECTION_NAME, ViewSummaryDTO.class);
protected ViewSummaryService(MongoCollections mongoCollections) {
collection = mongoCollections.collection(COLLECTION_NAME, ViewSummaryDTO.class);
mongoUtils = mongoCollections.utils(collection);
}

public PaginatedList<ViewSummaryDTO> searchPaginatedByType(SearchUser searchUser,
Expand Down Expand Up @@ -76,7 +77,7 @@ public PaginatedList<ViewSummaryDTO> searchPaginatedByType(SearchUser searchUser
.toList()
: views;

final long grandTotal = db.getCount(Filters.or(Filters.eq(ViewDTO.FIELD_TYPE, type), Filters.not(Filters.exists(ViewDTO.FIELD_TYPE))));
final long grandTotal = collection.countDocuments(Filters.or(Filters.eq(ViewDTO.FIELD_TYPE, type), Filters.not(Filters.exists(ViewDTO.FIELD_TYPE))));

return new PaginatedList<>(paginatedStreams, views.size(), page, perPage, grandTotal);
}
Expand All @@ -85,4 +86,13 @@ public PaginatedList<ViewSummaryDTO> searchPaginatedByType(SearchUser searchUser
public MongoCollection<ViewSummaryDTO> collection() {
return collection;
}

@MustBeClosed
public Stream<ViewSummaryDTO> streamAll() {
return MongoUtils.stream(collection.find());
}

public Optional<ViewSummaryDTO> get(String id) {
return mongoUtils.getById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.ImmutableGraph;
import com.google.common.graph.MutableGraph;
import com.google.errorprone.annotations.MustBeClosed;
import jakarta.inject.Inject;
import org.graylog.plugins.views.search.Search;
import org.graylog.plugins.views.search.db.SearchDbService;
import org.graylog.plugins.views.search.views.ViewDTO;
Expand All @@ -48,8 +50,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.inject.Inject;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -162,9 +162,12 @@ public void delete(ViewDTO nativeEntity) {

@Override
public Set<EntityExcerpt> listEntityExcerpts() {
return getNativeViews().map(this::createExcerpt).collect(Collectors.toSet());
try (final Stream<ViewSummaryDTO> nativeViews = getNativeViews()) {
return nativeViews.map(this::createExcerpt).collect(Collectors.toSet());
}
}

@MustBeClosed
protected Stream<ViewSummaryDTO> getNativeViews() {
return viewSummaryService.streamAll().filter(v -> v.type().equals(this.getDTOType()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.graylog.testing.mongodb.MongoDBInstance;
import org.graylog2.bindings.providers.MongoJackObjectMapperProvider;
import org.graylog2.database.MongoCollections;
import org.graylog2.database.MongoConnection;
import org.graylog2.shared.bindings.ObjectMapperModule;
import org.graylog2.shared.bindings.ValidatorModule;
import org.joda.time.DateTime;
Expand Down Expand Up @@ -60,10 +59,8 @@ public class SearchesCleanUpJobWithDBServicesTest {
private SearchDbService searchDbService;

static class TestViewService extends ViewSummaryService {
TestViewService(MongoConnection mongoConnection,
MongoJackObjectMapperProvider mongoJackObjectMapperProvider,
MongoCollections mongoCollections) {
super(mongoConnection, mongoJackObjectMapperProvider, mongoCollections);
TestViewService(MongoCollections mongoCollections) {
super(mongoCollections);
}
}

Expand All @@ -73,8 +70,6 @@ public void setup(MongoJackObjectMapperProvider mapperProvider) {


final ViewSummaryService viewService = new TestViewService(
mongodb.mongoConnection(),
mapperProvider,
new MongoCollections(mapperProvider, mongodb.mongoConnection())
);
this.searchDbService = spy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void setUp() throws IOException {
searchDbService = new ViewFacadeTest.TestSearchDBService(mongoConnection, mapper);
final MongoCollections mongoCollections = new MongoCollections(mapper, mongoConnection);
ViewFacadeTest.TestViewService viewService = new ViewFacadeTest.TestViewService(null, mongoCollections);
ViewFacadeTest.TestViewSummaryService viewSummaryService = new ViewFacadeTest.TestViewSummaryService(mongoConnection, mapper, mongoCollections);
ViewFacadeTest.TestViewSummaryService viewSummaryService = new ViewFacadeTest.TestViewSummaryService(mongoCollections);
UserService userService = mock(UserService.class);
final UserImpl fakeUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()),
mock(ClusterConfigService.class), ImmutableMap.of("username", "testuser"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,8 @@ protected TestViewService(ClusterConfigService clusterConfigService,
}

public static class TestViewSummaryService extends ViewSummaryService {
protected TestViewSummaryService(MongoConnection mongoConnection,
MongoJackObjectMapperProvider mongoJackObjectMapperProvider,
MongoCollections mongoCollections) {
super(mongoConnection, mongoJackObjectMapperProvider, mongoCollections);

protected TestViewSummaryService(MongoCollections mongoCollections) {
super(mongoCollections);
}
}

Expand Down Expand Up @@ -154,7 +151,7 @@ public void setUp() {
final MongoCollections mongoCollections = new MongoCollections(mapper, mongoConnection);
searchDbService = new TestSearchDBService(mongoConnection, mapper);
viewService = new TestViewService(null, mongoCollections);
viewSummaryService = new TestViewSummaryService(mongoConnection, mapper, mongoCollections);
viewSummaryService = new TestViewSummaryService(mongoCollections);
userService = mock(UserService.class);

facade = new SearchFacade(objectMapper, searchDbService, viewService, viewSummaryService, userService);
Expand Down
Loading