Class MongoDBUtils
java.lang.Object
dev.getelements.elements.dao.mongo.MongoDBUtils
Some helper methods used in various parts of the MongoDB code.
Created by patricktwohig on 6/10/15.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondev.morphia.Datastore
int
boolean
isIndexedPlan
(org.bson.Document plan) boolean
isIndexedQuery
(dev.morphia.query.Query<?> query) boolean
isScanQuery
(dev.morphia.query.Query<?> query) <MongoModelT>
Pagination<MongoModelT> paginationFromAggregation
(Supplier<dev.morphia.aggregation.Aggregation<?>> aggregationSupplier, Class<MongoModelT> modelTClass, int offset, int count) Transforms anAggregation
to the resultingPagination
.<MongoModelT>
Pagination<MongoModelT> paginationFromAggregation
(Supplier<dev.morphia.aggregation.Aggregation<?>> aggregationSupplier, Class<MongoModelT> modelTClass, int offset, int count, dev.morphia.aggregation.AggregationOptions options) Transforms anAggregation
to the resultingPagination
.<ModelT,
MongoModelT>
Pagination<ModelT> paginationFromQuery
(dev.morphia.query.Query<MongoModelT> query, int offset, int count, Class<ModelT> modelTClass) Transforms the givenQuery
to the resultingPagination
.<ModelT,
MongoModelT>
Pagination<ModelT> paginationFromQuery
(dev.morphia.query.Query<MongoModelT> query, int offset, int count, Function<MongoModelT, ModelT> function) Transforms the givenQuery
to the resultingPagination
.<ModelT,
MongoModelT>
Pagination<ModelT> paginationFromQuery
(dev.morphia.query.Query<MongoModelT> query, int offset, int count, Function<MongoModelT, ModelT> function, dev.morphia.query.FindOptions options) Transforms the givenQuery
to the resultingPagination
.Optional
<org.bson.types.ObjectId> Tries to parse a string into anObjectId
.org.bson.types.ObjectId
parseOrReturnNull
(String objectId) Parses the given ObjectID string usingObjectId
.<ExceptionT extends NotFoundException>
org.bson.types.ObjectIdparseOrThrow
(String objectId, Function<String, ExceptionT> exceptionTSupplier) Parses the given ObjectID string usingObjectId
.org.bson.types.ObjectId
parseOrThrowNotFoundException
(String objectId) Deprecated.<T> T
Performs the supplied operation, catching allMongoCommandException
instances and mapping to the appropriate type of exception internally.<T,
U> U Performs the supplied operation, catching allMongoCommandException
instances and mapping to the appropriate type of exception internally.<T,
ExceptionT extends Throwable>
Tperform
(Function<dev.morphia.Datastore, T> operation, Function<Throwable, ExceptionT> exceptionTSupplier) Performs the supplied operation, catching allMongoCommandException
instances and mapping to the appropraite type of exception internally.void
Performs the supplied operation, catching allMongoCommandException
instances and mapping to the appropriate type of exception internally.void
setDatastore
(dev.morphia.Datastore datastore) void
setMapper
(MapperRegistry mapperRegistry) void
setQueryMaxResults
(int queryMaxResults) <ModelT,
MongoModelT>
Tabulation<ModelT> tabulationFromQuery
(dev.morphia.query.Query<MongoModelT> query, Function<MongoModelT, ModelT> function) Transforms the givenQuery
to the resultingPagination
.<ModelT,
MongoModelT>
Tabulation<ModelT> tabulationFromQuery
(dev.morphia.query.Query<MongoModelT> query, Function<MongoModelT, ModelT> function, dev.morphia.query.FindOptions options) Transforms the givenQuery
to the resultingPagination
.
-
Field Details
-
COLLSCAN
- See Also:
-
-
Constructor Details
-
MongoDBUtils
public MongoDBUtils()
-
-
Method Details
-
perform
Performs the supplied operation, catching allMongoCommandException
instances and mapping to the appropriate type of exception internally.- Type Parameters:
T
- the expected return type- Parameters:
operation
- the operation- Returns:
- the object returned by the supplied operation
-
perform
Performs the supplied operation, catching allMongoCommandException
instances and mapping to the appropriate type of exception internally.- Type Parameters:
T
- the expected return type from the datastoreU
- the function return type- Parameters:
operation
- the operationuClass
- the type which to convert out of the type returned from the Datastore.- Returns:
- the object returned by the supplied operation
-
performV
Performs the supplied operation, catching allMongoCommandException
instances and mapping to the appropriate type of exception internally.- Parameters:
operation
- the operation
-
perform
public <T,ExceptionT extends Throwable> T perform(Function<dev.morphia.Datastore, T> operation, Function<Throwable, throws ExceptionTExceptionT> exceptionTSupplier) Performs the supplied operation, catching allMongoCommandException
instances and mapping to the appropraite type of exception internally.- Type Parameters:
T
- the expected return type- Parameters:
operation
- the operation- Returns:
- the object retured by the supplied operation
- Throws:
ExceptionT
-
parseOrThrowNotFoundException
Deprecated.Parses the given ObjectID string usingObjectId
. If this fails, this throws the appropriate exception type ofNotFoundException
.- Parameters:
objectId
- the object ID to parse- Returns:
- an
ObjectId
(never null)
-
parseOrThrow
public <ExceptionT extends NotFoundException> org.bson.types.ObjectId parseOrThrow(String objectId, Function<String, ExceptionT> exceptionTSupplier) throws ExceptionTParses the given ObjectID string usingObjectId
. If this fails, this throws the appropriate exception type ofNotFoundException
.- Parameters:
objectId
- the object ID to parse- Returns:
- an
ObjectId
(never null) - Throws:
ExceptionT
-
parse
Tries to parse a string into anObjectId
.- Parameters:
idString
- the id string- Returns:
- the
Optional<org.bson.types.ObjectId>
-
parseOrReturnNull
Parses the given ObjectID string usingObjectId
. If this fails, this throws the appropriate exception type ofNotFoundException
.- Parameters:
objectId
- the object ID to parse- Returns:
- an
ObjectId
(never null)
-
paginationFromQuery
public <ModelT,MongoModelT> Pagination<ModelT> paginationFromQuery(dev.morphia.query.Query<MongoModelT> query, int offset, int count, Class<ModelT> modelTClass) Transforms the givenQuery
to the resultingPagination
.- Type Parameters:
ModelT
- the desired model typeMongoModelT
- the mongoDB model type- Parameters:
query
- the queryoffset
- the offsetcount
- the countmodelTClass
- the destination Class- Returns:
- a
Pagination
instance for the given ModelT
-
paginationFromQuery
public <ModelT,MongoModelT> Pagination<ModelT> paginationFromQuery(dev.morphia.query.Query<MongoModelT> query, int offset, int count, Function<MongoModelT, ModelT> function) Transforms the givenQuery
to the resultingPagination
.- Type Parameters:
ModelT
- the desired model typeMongoModelT
- the mongoDB model type- Parameters:
query
- the queryoffset
- the offsetcount
- the countfunction
- the function to transform the values- Returns:
- a
Pagination
instance for the given ModelT
-
paginationFromQuery
public <ModelT,MongoModelT> Pagination<ModelT> paginationFromQuery(dev.morphia.query.Query<MongoModelT> query, int offset, int count, Function<MongoModelT, ModelT> function, dev.morphia.query.FindOptions options) Transforms the givenQuery
to the resultingPagination
.- Type Parameters:
ModelT
- the desired model typeMongoModelT
- the mongoDB model type- Parameters:
query
- the queryoffset
- the offsetcount
- the countfunction
- the function to transform the valuesoptions
- aFindOptions
used to modify the query results- Returns:
- a
Pagination
instance for the given ModelT
-
tabulationFromQuery
public <ModelT,MongoModelT> Tabulation<ModelT> tabulationFromQuery(dev.morphia.query.Query<MongoModelT> query, Function<MongoModelT, ModelT> function) Transforms the givenQuery
to the resultingPagination
.- Type Parameters:
ModelT
- the desired model typeMongoModelT
- the mongoDB model type- Parameters:
query
- the queryfunction
- the function to transform the values- Returns:
- a
Pagination
instance for the given ModelT
-
tabulationFromQuery
public <ModelT,MongoModelT> Tabulation<ModelT> tabulationFromQuery(dev.morphia.query.Query<MongoModelT> query, Function<MongoModelT, ModelT> function, dev.morphia.query.FindOptions options) Transforms the givenQuery
to the resultingPagination
.- Type Parameters:
ModelT
- the desired model typeMongoModelT
- the mongoDB model type- Parameters:
query
- the queryfunction
- the function to transform the valuesoptions
- aFindOptions
used to modify the query results- Returns:
- a
Pagination
instance for the given ModelT
-
paginationFromAggregation
public <MongoModelT> Pagination<MongoModelT> paginationFromAggregation(Supplier<dev.morphia.aggregation.Aggregation<?>> aggregationSupplier, Class<MongoModelT> modelTClass, int offset, int count) Transforms anAggregation
to the resultingPagination
. This function must modify and build upon the suppliedAggregation
.- Type Parameters:
MongoModelT
- the mongoDB model type- Parameters:
aggregationSupplier
- aSupplier<?>
for the aggregationoffset
- the offsetcount
- the count- Returns:
- a
Pagination
instance for the given ModelT
-
paginationFromAggregation
public <MongoModelT> Pagination<MongoModelT> paginationFromAggregation(Supplier<dev.morphia.aggregation.Aggregation<?>> aggregationSupplier, Class<MongoModelT> modelTClass, int offset, int count, dev.morphia.aggregation.AggregationOptions options) Transforms anAggregation
to the resultingPagination
. This function must modify and build upon the suppliedAggregation
.- Type Parameters:
MongoModelT
- the mongoDB model type- Parameters:
aggregationSupplier
- aSupplier<?>
for the aggregationoffset
- the offsetcount
- the countoptions
- aAggregationOptions
used to modify the query results- Returns:
- a
Pagination
instance for the given ModelT
-
isScanQuery
public boolean isScanQuery(dev.morphia.query.Query<?> query) -
isIndexedQuery
public boolean isIndexedQuery(dev.morphia.query.Query<?> query) -
isIndexedPlan
public boolean isIndexedPlan(org.bson.Document plan) -
getDatastore
public dev.morphia.Datastore getDatastore() -
setDatastore
@Inject public void setDatastore(dev.morphia.Datastore datastore) -
getQueryMaxResults
public int getQueryMaxResults() -
setQueryMaxResults
@Inject public void setQueryMaxResults(@Named("dev.getelements.elements.query.max.results") int queryMaxResults) -
getMapper
-
setMapper
-
parseOrThrow(String, Function)