Class PaginatedDbService<DTO>
- Type Parameters:
DTO
-
- Direct Known Subclasses:
CollectorService
,ConfigurationService
,ConfigurationVariableService
,DBAuthServiceBackendService
,DBGrantService
,DBJobDefinitionService
,DBNotificationGracePeriodService
,DBNotificationService
,ImportService
,PaginatedAuthzRolesService
,PaginatedGrokPatternService
,PaginatedMongoDbPipelineService
,PaginatedMongoDbRuleService
,PaginatedStreamService
,PaginatedUserService
,ScopedDbService
,SidecarService
,ViewService
,ViewSummaryService
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 Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
PaginatedDbService
(MongoConnection mongoConnection, MongoJackObjectMapperProvider mapper, Class<DTO> dtoClass, String collectionName) protected
PaginatedDbService
(MongoConnection mongoConnection, MongoJackObjectMapperProvider mapper, Class<DTO> dtoClass, String collectionName, com.mongodb.DBObject dbOptions, Class<?> view) protected
PaginatedDbService
(MongoConnection mongoConnection, MongoJackObjectMapperProvider mapper, Class<DTO> dtoClass, String collectionName, Class<?> view) -
Method Summary
Modifier and TypeMethodDescriptionprotected com.google.common.collect.ImmutableList<DTO>
asImmutableList
(Iterator<? extends DTO> cursor) int
Deletes thePaginatedDbService
for the given ID from the database.protected PaginatedList<DTO>
findPaginatedWithQueryAndSort
(org.mongojack.DBQuery.Query query, org.mongojack.DBSort.SortBuilder sort, int page, int perPage) Returns aPaginatedList<DTO>
for the given query and pagination parameters.protected PaginatedList<DTO>
findPaginatedWithQueryFilterAndSort
(org.mongojack.DBQuery.Query query, Predicate<DTO> filter, org.mongojack.DBSort.SortBuilder sort, int page, int perPage) Returns aPaginatedList<DTO>
for the given query, filter and pagination parameters.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) Get thePaginatedDbService
for the given ID.protected org.mongojack.DBSort.SortBuilder
getSortBuilder
(String order, String field) Returns a sort builder for the given order and field name.Stores the givenPaginatedDbService
in the database.Returns an unordered stream of all entries in the database.streamByIds
(Set<String> idSet) Returns an unordered stream of all entries in the database for the given IDs.streamQuery
(org.mongojack.DBQuery.Query query) Returns an unordered stream of database entries for the givenDBQuery.Query
.streamQueryWithSort
(org.mongojack.DBQuery.Query query, org.mongojack.DBSort.SortBuilder sort) Returns a stream of database entries for the givenDBQuery.Query
sorted by the giveDBSort.SortBuilder
.
-
Field Details
-
db
-
-
Constructor Details
-
PaginatedDbService
protected PaginatedDbService(MongoConnection mongoConnection, MongoJackObjectMapperProvider mapper, Class<DTO> dtoClass, String collectionName) -
PaginatedDbService
protected PaginatedDbService(MongoConnection mongoConnection, MongoJackObjectMapperProvider mapper, Class<DTO> dtoClass, String collectionName, Class<?> view) -
PaginatedDbService
protected PaginatedDbService(MongoConnection mongoConnection, MongoJackObjectMapperProvider mapper, Class<DTO> dtoClass, String collectionName, @Nullable com.mongodb.DBObject dbOptions, @Nullable Class<?> view)
-
-
Method Details
-
get
Get thePaginatedDbService
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
Stores the givenPaginatedDbService
in the database.- Parameters:
dto
- thePaginatedDbService
to save- Returns:
- the newly saved
PaginatedDbService
-
delete
Deletes thePaginatedDbService
for the given ID from the database.- Parameters:
id
- ID of thePaginatedDbService
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 aPaginatedList<DTO>
for the given query and pagination parameters.This method is only accessible by subclasses to avoid exposure of the
DBQuery
andDBSort
interfaces to consumers.- Parameters:
query
- the query to executesort
- the sort builder for the querypage
- the page number that should be returnedperPage
- the number of entries per page, 0 is unlimited- Returns:
- the paginated list
-
asImmutableList
-
findPaginatedWithQueryFilterAndSort
protected PaginatedList<DTO> findPaginatedWithQueryFilterAndSort(org.mongojack.DBQuery.Query query, Predicate<DTO> filter, org.mongojack.DBSort.SortBuilder sort, int page, int perPage) Returns aPaginatedList<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
andDBSort
interfaces to consumers.- Parameters:
query
- the query to executefilter
- the filter to apply to each database entrysort
- the sort builder for the querypage
- the page number that should be returnedperPage
- 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
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
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
Returns an unordered stream of database entries for the givenDBQuery.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 givenDBQuery.Query
sorted by the giveDBSort.SortBuilder
.The returned stream needs to be closed to free the underlying database resources.
- Parameters:
query
- the query to executesort
- the sort order for the query- Returns:
- stream of database entries that match the query
-
getSortBuilder
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
-