Class PaginatedDbService<DTO>

java.lang.Object
org.graylog2.database.PaginatedDbService<DTO>
Type Parameters:
DTO -
Direct Known Subclasses:
CollectorService, ConfigurationService, ConfigurationVariableService, DataNodeProvisioningServiceImpl, DBAuthServiceBackendService, DBGrantService, DBJobDefinitionService, DBNotificationGracePeriodService, DBNotificationService, FavoritesService, ImportService, LastOpenedService, PaginatedAuthzRolesService, PaginatedGrokPatternService, PaginatedMongoDbPipelineService, PaginatedMongoDbRuleService, PaginatedStreamService, PaginatedUserService, RecentActivityService, ScopedDbService, SidecarService, ViewService, ViewSummaryService

public abstract class PaginatedDbService<DTO> extends Object
This class is a helper to implement a basic Mongojack-based database service that allows CRUD operations on a single DTO type and offers paginated access.

It makes only a few assumptions, which are common to many Graylog entities:

  • The DTO class has a name which is unique

Subclasses can add more sophisticated query methods by access the protected "db" property.
Indices can be added in the constructor.

  • Field Details

    • db

      protected final org.mongojack.JacksonDBCollection<DTO,org.bson.types.ObjectId> db
  • Constructor Details

  • Method Details

    • get

      public Optional<DTO> get(String id)
      Get the PaginatedDbService for the given ID.
      Parameters:
      id - the ID of the object
      Returns:
      an Optional containing the found object or an empty Optional if no object can be found for the given ID
    • save

      public DTO save(DTO dto)
      Stores the given PaginatedDbService in the database.
      Parameters:
      dto - the PaginatedDbService to save
      Returns:
      the newly saved PaginatedDbService
    • delete

      public int delete(String id)
      Deletes the PaginatedDbService for the given ID from the database.
      Parameters:
      id - ID of the PaginatedDbService to delete
      Returns:
      the number of deleted documents
    • findPaginatedWithQueryAndSort

      protected PaginatedList<DTO> findPaginatedWithQueryAndSort(org.mongojack.DBQuery.Query query, org.mongojack.DBSort.SortBuilder sort, int page, int perPage)
      Returns a PaginatedList<DTO> for the given query and pagination parameters.

      This method is only accessible by subclasses to avoid exposure of the DBQuery and DBSort interfaces to consumers.

      Parameters:
      query - the query to execute
      sort - the sort builder for the query
      page - the page number that should be returned
      perPage - the number of entries per page, 0 is unlimited
      Returns:
      the paginated list
    • asImmutableList

      protected com.google.common.collect.ImmutableList<DTO> asImmutableList(Iterator<? extends DTO> cursor)
    • findPaginatedWithQueryFilterAndSort

      protected PaginatedList<DTO> findPaginatedWithQueryFilterAndSort(org.mongojack.DBQuery.Query query, Predicate<DTO> filter, org.mongojack.DBSort.SortBuilder sort, int page, int perPage)
      Returns a PaginatedList<DTO> for the given query, filter and pagination parameters.

      Since the database cannot execute the filter function directly, this method streams over the result cursor and executes the filter function for each database object. This increases memory consumption and should only be used if necessary. Use the #findPaginatedWithQueryAndSort() method if possible.

      This method is only accessible by subclasses to avoid exposure of the DBQuery and DBSort interfaces to consumers.

      Parameters:
      query - the query to execute
      filter - the filter to apply to each database entry
      sort - the sort builder for the query
      page - the page number that should be returned
      perPage - the number of entries per page, 0 is unlimited
      Returns:
      the paginated list
    • findPaginatedWithQueryFilterAndSortWithGrandTotal

      protected PaginatedList<DTO> findPaginatedWithQueryFilterAndSortWithGrandTotal(org.mongojack.DBQuery.Query query, Predicate<DTO> filter, org.mongojack.DBSort.SortBuilder sort, org.mongojack.DBQuery.Query grandTotalQuery, int page, int perPage)
    • streamAll

      public Stream<DTO> streamAll()
      Returns an unordered stream of all entries in the database.

      The returned stream needs to be closed to free the underlying database resources.

      Returns:
      stream of all database entries
    • streamByIds

      public Stream<DTO> streamByIds(Set<String> idSet)
      Returns an unordered stream of all entries in the database for the given IDs.

      The returned stream needs to be closed to free the underlying database resources.

      Parameters:
      idSet - set of IDs to query
      Returns:
      stream of database entries for the given IDs
    • streamQuery

      protected Stream<DTO> streamQuery(org.mongojack.DBQuery.Query query)
      Returns an unordered stream of database entries for the given DBQuery.Query.

      The returned stream needs to be closed to free the underlying database resources.

      Parameters:
      query - the query to execute
      Returns:
      stream of database entries that match the query
    • streamQueryWithSort

      protected Stream<DTO> streamQueryWithSort(org.mongojack.DBQuery.Query query, org.mongojack.DBSort.SortBuilder sort)
      Returns a stream of database entries for the given DBQuery.Query sorted by the give DBSort.SortBuilder.

      The returned stream needs to be closed to free the underlying database resources.

      Parameters:
      query - the query to execute
      sort - the sort order for the query
      Returns:
      stream of database entries that match the query
    • getSortBuilder

      protected org.mongojack.DBSort.SortBuilder getSortBuilder(String order, String field)
      Returns a sort builder for the given order and field name.
      Parameters:
      order - the order. either "asc" or "desc"
      field - the field to sort on
      Returns:
      the sort builder
    • getMultiFieldSortBuilder

      protected org.mongojack.DBSort.SortBuilder getMultiFieldSortBuilder(String order, List<String> fields)
    • getPage

      public static <T> List<T> getPage(List<T> sourceList, int page, int pageSize)
      Utility method to use, if you can't page it inside of MongoDB
      Type Parameters:
      T -
      Parameters:
      sourceList -
      page -
      pageSize -
      Returns: