From e30b3824a35dae044dfe9c1017214d7063a86e67 Mon Sep 17 00:00:00 2001 From: srnyx <25808801+srnyx@users.noreply.github.com> Date: Thu, 29 Aug 2024 17:07:11 -0400 Subject: [PATCH] `SingleMongo`, `MultiMongo`, and `MagicDatabase` --- .../xyz/srnyx/magicmongo/MagicCollection.java | 626 ++++++++++++++++-- .../xyz/srnyx/magicmongo/MagicDatabase.java | 316 +++++++++ .../java/xyz/srnyx/magicmongo/MagicMongo.java | 129 +--- .../java/xyz/srnyx/magicmongo/MultiMongo.java | 84 +++ .../xyz/srnyx/magicmongo/SingleMongo.java | 26 + 5 files changed, 1035 insertions(+), 146 deletions(-) create mode 100644 src/main/java/xyz/srnyx/magicmongo/MagicDatabase.java create mode 100644 src/main/java/xyz/srnyx/magicmongo/MultiMongo.java create mode 100644 src/main/java/xyz/srnyx/magicmongo/SingleMongo.java diff --git a/src/main/java/xyz/srnyx/magicmongo/MagicCollection.java b/src/main/java/xyz/srnyx/magicmongo/MagicCollection.java index 8277908..ac7ed0d 100644 --- a/src/main/java/xyz/srnyx/magicmongo/MagicCollection.java +++ b/src/main/java/xyz/srnyx/magicmongo/MagicCollection.java @@ -1,14 +1,19 @@ package xyz.srnyx.magicmongo; -import com.mongodb.client.MongoCollection; -import com.mongodb.client.MongoDatabase; -import com.mongodb.client.model.Filters; -import com.mongodb.client.model.FindOneAndUpdateOptions; -import com.mongodb.client.model.ReturnDocument; -import com.mongodb.client.model.UpdateOptions; +import com.mongodb.MongoNamespace; +import com.mongodb.ReadConcern; +import com.mongodb.ReadPreference; +import com.mongodb.WriteConcern; +import com.mongodb.bulk.BulkWriteResult; +import com.mongodb.client.*; +import com.mongodb.client.model.*; import com.mongodb.client.result.DeleteResult; +import com.mongodb.client.result.InsertManyResult; +import com.mongodb.client.result.InsertOneResult; import com.mongodb.client.result.UpdateResult; +import org.bson.Document; +import org.bson.codecs.configuration.CodecRegistry; import org.bson.conversions.Bson; import org.jetbrains.annotations.NotNull; @@ -24,7 +29,7 @@ * * @param the type of the collection */ -public class MagicCollection { +public class MagicCollection implements MongoCollection { /** * The {@link MongoCollection} instance */ @@ -50,7 +55,7 @@ public MagicCollection(@NotNull MongoDatabase database, @NotNull String name, @N */ @NotNull public Optional findOne(@NotNull Bson filter) { - return Optional.ofNullable(collection.find(filter).first()); + return Optional.ofNullable(find(filter).first()); } /** @@ -75,20 +80,7 @@ public Optional findOne(@NotNull String field, @Nullable Object value) { */ @NotNull public List findMany(@NotNull Bson filter) { - return collection.find(filter).into(new ArrayList<>()); - } - - /** - * Updates a document in the collection - * - * @param filter the filter to apply - * @param update the update to apply - * - * @return the {@link UpdateResult} of the operation - */ - @NotNull - public UpdateResult updateOne(@NotNull Bson filter, @NotNull Bson update) { - return collection.updateOne(filter, update); + return find(filter).into(new ArrayList<>()); } /** @@ -101,7 +93,7 @@ public UpdateResult updateOne(@NotNull Bson filter, @NotNull Bson update) { */ @NotNull public UpdateResult upsertOne(@NotNull Bson filter, @NotNull Bson update) { - return collection.updateOne(filter, update, new UpdateOptions().upsert(true)); + return updateOne(filter, update, new UpdateOptions().upsert(true)); } /** @@ -113,8 +105,8 @@ public UpdateResult upsertOne(@NotNull Bson filter, @NotNull Bson update) { * @return the updated document */ @NotNull - public Optional findOneAndUpdate(@NotNull Bson filter, @NotNull Bson update) { - return Optional.ofNullable(collection.findOneAndUpdate(filter, update, new FindOneAndUpdateOptions() + public Optional findOneAndUpdateReturn(@NotNull Bson filter, @NotNull Bson update) { + return Optional.ofNullable(findOneAndUpdate(filter, update, new FindOneAndUpdateOptions() .returnDocument(ReturnDocument.AFTER))); } @@ -128,23 +120,11 @@ public Optional findOneAndUpdate(@NotNull Bson filter, @NotNull Bson update) */ @NotNull public Optional findOneAndUpsert(@NotNull Bson filter, @NotNull Bson update) { - return Optional.ofNullable(collection.findOneAndUpdate(filter, update, new FindOneAndUpdateOptions() + return Optional.ofNullable(findOneAndUpdate(filter, update, new FindOneAndUpdateOptions() .returnDocument(ReturnDocument.AFTER) .upsert(true))); } - /** - * Deletes a document in the collection - * - * @param filter the filter to apply - * - * @return the {@link DeleteResult} of the operation - */ - @NotNull - public DeleteResult deleteOne(@NotNull Bson filter) { - return collection.deleteOne(filter); - } - /** * Deletes a document in the collection * @@ -157,4 +137,574 @@ public DeleteResult deleteOne(@NotNull Bson filter) { public DeleteResult deleteOne(@NotNull String field, @Nullable Object value) { return deleteOne(Filters.eq(field, value)); } + + // OVERRIDE METHODS FROM MongoCollection + @Override @NotNull + public UpdateResult updateOne(@NotNull Bson filter, @NotNull Bson update) { + return collection.updateOne(filter, update); + } + @Override @NotNull + public UpdateResult updateOne(@NotNull Bson filter, @NotNull Bson update, @NotNull UpdateOptions updateOptions) { + return collection.updateOne(filter, update, updateOptions); + } + @Override @NotNull + public UpdateResult updateOne(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull Bson update) { + return collection.updateOne(clientSession, filter, update); + } + @Override @NotNull + public UpdateResult updateOne(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull Bson update, @NotNull UpdateOptions updateOptions) { + return collection.updateOne(clientSession, filter, update, updateOptions); + } + @Override @NotNull + public UpdateResult updateOne(@NotNull Bson filter, @NotNull List update) { + return collection.updateOne(filter, update); + } + @Override @NotNull + public UpdateResult updateOne(@NotNull Bson filter, @NotNull List update, @NotNull UpdateOptions updateOptions) { + return collection.updateOne(filter, update, updateOptions); + } + @Override @NotNull + public UpdateResult updateOne(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull List update) { + return collection.updateOne(clientSession, filter, update); + } + @Override @NotNull + public UpdateResult updateOne(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull List update, @NotNull UpdateOptions updateOptions) { + return collection.updateOne(clientSession, filter, update, updateOptions); + } + @Override @NotNull + public UpdateResult updateMany(@NotNull Bson filter, @NotNull Bson update) { + return collection.updateMany(filter, update); + } + @Override @NotNull + public UpdateResult updateMany(@NotNull Bson filter, @NotNull Bson update, @NotNull UpdateOptions updateOptions) { + return collection.updateMany(filter, update, updateOptions); + } + @Override @NotNull + public UpdateResult updateMany(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull Bson update) { + return collection.updateMany(clientSession, filter, update); + } + @Override @NotNull + public UpdateResult updateMany(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull Bson update, @NotNull UpdateOptions updateOptions) { + return collection.updateMany(clientSession, filter, update, updateOptions); + } + @Override @NotNull + public UpdateResult updateMany(@NotNull Bson filter, @NotNull List update) { + return collection.updateMany(filter, update); + } + @Override @NotNull + public UpdateResult updateMany(@NotNull Bson filter, @NotNull List update, @NotNull UpdateOptions updateOptions) { + return collection.updateMany(filter, update, updateOptions); + } + @Override @NotNull + public UpdateResult updateMany(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull List update) { + return collection.updateMany(clientSession, filter, update); + } + @Override @NotNull + public UpdateResult updateMany(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull List update, @NotNull UpdateOptions updateOptions) { + return collection.updateMany(clientSession, filter, update, updateOptions); + } + @Override @Nullable + public T findOneAndDelete(@NotNull Bson filter) { + return collection.findOneAndDelete(filter); + } + @Override @Nullable + public T findOneAndDelete(@NotNull Bson filter, @NotNull FindOneAndDeleteOptions options) { + return collection.findOneAndDelete(filter, options); + } + @Override @Nullable + public T findOneAndDelete(@NotNull ClientSession clientSession, @NotNull Bson filter) { + return collection.findOneAndDelete(clientSession, filter); + } + @Override @Nullable + public T findOneAndDelete(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull FindOneAndDeleteOptions options) { + return collection.findOneAndDelete(clientSession, filter, options); + } + @Override @Nullable + public T findOneAndReplace(@NotNull Bson filter, @NotNull T replacement) { + return collection.findOneAndReplace(filter, replacement); + } + @Override @Nullable + public T findOneAndReplace(@NotNull Bson filter, @NotNull T replacement, @NotNull FindOneAndReplaceOptions options) { + return collection.findOneAndReplace(filter, replacement, options); + } + @Override @Nullable + public T findOneAndReplace(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull T replacement) { + return collection.findOneAndReplace(clientSession, filter, replacement); + } + @Override @Nullable + public T findOneAndReplace(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull T replacement, @NotNull FindOneAndReplaceOptions options) { + return collection.findOneAndReplace(clientSession, filter, replacement, options); + } + @Override @Nullable + public T findOneAndUpdate(@NotNull Bson filter, @NotNull Bson update) { + return collection.findOneAndUpdate(filter, update); + } + @Override @Nullable + public T findOneAndUpdate(@NotNull Bson filter, @NotNull Bson update, @NotNull FindOneAndUpdateOptions options) { + return collection.findOneAndUpdate(filter, update, options); + } + @Override @Nullable + public T findOneAndUpdate(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull Bson update) { + return collection.findOneAndUpdate(clientSession, filter, update); + } + @Override @Nullable + public T findOneAndUpdate(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull Bson update, @NotNull FindOneAndUpdateOptions options) { + return collection.findOneAndUpdate(clientSession, filter, update, options); + } + @Override @Nullable + public T findOneAndUpdate(@NotNull Bson filter, @NotNull List update) { + return collection.findOneAndUpdate(filter, update); + } + @Override @Nullable + public T findOneAndUpdate(@NotNull Bson filter, @NotNull List update, @NotNull FindOneAndUpdateOptions options) { + return collection.findOneAndUpdate(filter, update, options); + } + @Override @Nullable + public T findOneAndUpdate(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull List update) { + return collection.findOneAndUpdate(clientSession, filter, update); + } + @Override @Nullable + public T findOneAndUpdate(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull List update, @NotNull FindOneAndUpdateOptions options) { + return collection.findOneAndUpdate(clientSession, filter, update, options); + } + @Override + public void drop() { + collection.drop(); + } + @Override + public void drop(@NotNull ClientSession clientSession) { + collection.drop(clientSession); + } + @Override + public void drop(@NotNull DropCollectionOptions dropCollectionOptions) { + collection.drop(dropCollectionOptions); + } + @Override + public void drop(@NotNull ClientSession clientSession, @NotNull DropCollectionOptions dropCollectionOptions) { + collection.drop(clientSession, dropCollectionOptions); + } + @Override @NotNull + public String createSearchIndex(@NotNull String indexName, @NotNull Bson definition) { + return collection.createSearchIndex(indexName, definition); + } + @Override @NotNull + public String createSearchIndex(@NotNull Bson definition) { + return collection.createSearchIndex(definition); + } + @Override @NotNull + public List createSearchIndexes(@NotNull List searchIndexModels) { + return collection.createSearchIndexes(searchIndexModels); + } + @Override + public void updateSearchIndex(@NotNull String indexName, @NotNull Bson definition) { + collection.updateSearchIndex(indexName, definition); + } + @Override + public void dropSearchIndex(@NotNull String indexName) { + collection.dropSearchIndex(indexName); + } + @Override @NotNull + public ListSearchIndexesIterable listSearchIndexes() { + return collection.listSearchIndexes(); + } + @Override @NotNull + public ListSearchIndexesIterable listSearchIndexes(@NotNull Class tResultClass) { + return collection.listSearchIndexes(tResultClass); + } + @Override @NotNull + public String createIndex(@NotNull Bson keys) { + return collection.createIndex(keys); + } + @Override @NotNull + public String createIndex(@NotNull Bson keys, @NotNull IndexOptions indexOptions) { + return collection.createIndex(keys, indexOptions); + } + @Override @NotNull + public String createIndex(@NotNull ClientSession clientSession, @NotNull Bson keys) { + return collection.createIndex(clientSession, keys); + } + @Override @NotNull + public String createIndex(@NotNull ClientSession clientSession, @NotNull Bson keys, @NotNull IndexOptions indexOptions) { + return collection.createIndex(clientSession, keys, indexOptions); + } + @Override @NotNull + public List createIndexes(@NotNull List indexes) { + return collection.createIndexes(indexes); + } + @Override @NotNull + public List createIndexes(@NotNull List indexes, @NotNull CreateIndexOptions createIndexOptions) { + return collection.createIndexes(indexes, createIndexOptions); + } + @Override @NotNull + public List createIndexes(@NotNull ClientSession clientSession, @NotNull List indexes) { + return collection.createIndexes(clientSession, indexes); + } + @Override @NotNull + public List createIndexes(@NotNull ClientSession clientSession, @NotNull List indexes, @NotNull CreateIndexOptions createIndexOptions) { + return collection.createIndexes(clientSession, indexes, createIndexOptions); + } + @Override @NotNull + public ListIndexesIterable listIndexes() { + return collection.listIndexes(); + } + @Override @NotNull + public ListIndexesIterable listIndexes(@NotNull Class tResultClass) { + return collection.listIndexes(tResultClass); + } + @Override @NotNull + public ListIndexesIterable listIndexes(@NotNull ClientSession clientSession) { + return collection.listIndexes(clientSession); + } + @Override @NotNull + public ListIndexesIterable listIndexes(@NotNull ClientSession clientSession, @NotNull Class tResultClass) { + return collection.listIndexes(clientSession, tResultClass); + } + @Override + public void dropIndex(@NotNull String indexName) { + collection.dropIndex(indexName); + } + @Override + public void dropIndex(@NotNull String indexName, @NotNull DropIndexOptions dropIndexOptions) { + collection.dropIndex(indexName, dropIndexOptions); + } + @Override + public void dropIndex(@NotNull Bson keys) { + collection.dropIndex(keys); + } + @Override + public void dropIndex(@NotNull Bson keys, @NotNull DropIndexOptions dropIndexOptions) { + collection.dropIndex(keys, dropIndexOptions); + } + @Override + public void dropIndex(@NotNull ClientSession clientSession, @NotNull String indexName) { + collection.dropIndex(clientSession, indexName); + } + @Override + public void dropIndex(@NotNull ClientSession clientSession, @NotNull Bson keys) { + collection.dropIndex(clientSession, keys); + } + @Override + public void dropIndex(@NotNull ClientSession clientSession, @NotNull String indexName, @NotNull DropIndexOptions dropIndexOptions) { + collection.dropIndex(clientSession, indexName, dropIndexOptions); + } + @Override + public void dropIndex(@NotNull ClientSession clientSession, @NotNull Bson keys, @NotNull DropIndexOptions dropIndexOptions) { + collection.dropIndex(clientSession, keys, dropIndexOptions); + } + @Override + public void dropIndexes() { + collection.dropIndexes(); + } + @Override + public void dropIndexes(@NotNull ClientSession clientSession) { + collection.dropIndexes(clientSession); + } + @Override + public void dropIndexes(@NotNull DropIndexOptions dropIndexOptions) { + collection.dropIndexes(dropIndexOptions); + } + @Override + public void dropIndexes(@NotNull ClientSession clientSession, @NotNull DropIndexOptions dropIndexOptions) { + collection.dropIndexes(clientSession, dropIndexOptions); + } + @Override + public void renameCollection(@NotNull MongoNamespace newCollectionNamespace) { + collection.renameCollection(newCollectionNamespace); + } + @Override + public void renameCollection(@NotNull MongoNamespace newCollectionNamespace, @NotNull RenameCollectionOptions renameCollectionOptions) { + collection.renameCollection(newCollectionNamespace, renameCollectionOptions); + } + @Override + public void renameCollection(@NotNull ClientSession clientSession, @NotNull MongoNamespace newCollectionNamespace) { + collection.renameCollection(clientSession, newCollectionNamespace); + } + @Override + public void renameCollection(@NotNull ClientSession clientSession, @NotNull MongoNamespace newCollectionNamespace, @NotNull RenameCollectionOptions renameCollectionOptions) { + collection.renameCollection(clientSession, newCollectionNamespace, renameCollectionOptions); + } + @Override @NotNull + public MongoNamespace getNamespace() { + return collection.getNamespace(); + } + @Override @NotNull + public Class getDocumentClass() { + return collection.getDocumentClass(); + } + @Override @NotNull + public CodecRegistry getCodecRegistry() { + return collection.getCodecRegistry(); + } + @Override @NotNull + public ReadPreference getReadPreference() { + return collection.getReadPreference(); + } + @Override @NotNull + public WriteConcern getWriteConcern() { + return collection.getWriteConcern(); + } + @Override @NotNull + public ReadConcern getReadConcern() { + return collection.getReadConcern(); + } + @Override @NotNull + public MongoCollection withDocumentClass(@NotNull Class clazz) { + return collection.withDocumentClass(clazz); + } + @Override @NotNull + public MongoCollection withCodecRegistry(@NotNull CodecRegistry codecRegistry) { + return collection.withCodecRegistry(codecRegistry); + } + @Override @NotNull + public MongoCollection withReadPreference(@NotNull ReadPreference readPreference) { + return collection.withReadPreference(readPreference); + } + @Override @NotNull + public MongoCollection withWriteConcern(@NotNull WriteConcern writeConcern) { + return collection.withWriteConcern(writeConcern); + } + @Override @NotNull + public MongoCollection withReadConcern(@NotNull ReadConcern readConcern) { + return collection.withReadConcern(readConcern); + } + @Override + public long countDocuments() { + return collection.countDocuments(); + } + @Override + public long countDocuments(@NotNull Bson filter) { + return collection.countDocuments(filter); + } + @Override + public long countDocuments(@NotNull Bson filter, @NotNull CountOptions options) { + return collection.countDocuments(filter, options); + } + @Override + public long countDocuments(@NotNull ClientSession clientSession) { + return collection.countDocuments(clientSession); + } + @Override + public long countDocuments(@NotNull ClientSession clientSession, @NotNull Bson filter) { + return collection.countDocuments(clientSession, filter); + } + @Override + public long countDocuments(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull CountOptions options) { + return collection.countDocuments(clientSession, filter, options); + } + @Override + public long estimatedDocumentCount() { + return collection.estimatedDocumentCount(); + } + @Override + public long estimatedDocumentCount(@NotNull EstimatedDocumentCountOptions options) { + return collection.estimatedDocumentCount(options); + } + @Override @NotNull + public DistinctIterable distinct(@NotNull String fieldName, @NotNull Class tResultClass) { + return collection.distinct(fieldName, tResultClass); + } + @Override @NotNull + public DistinctIterable distinct(@NotNull String fieldName, @NotNull Bson filter, @NotNull Class tResultClass) { + return collection.distinct(fieldName, filter, tResultClass); + } + @Override @NotNull + public DistinctIterable distinct(@NotNull ClientSession clientSession, @NotNull String fieldName, @NotNull Class tResultClass) { + return collection.distinct(clientSession, fieldName, tResultClass); + } + @Override @NotNull + public DistinctIterable distinct(@NotNull ClientSession clientSession, @NotNull String fieldName, @NotNull Bson filter, @NotNull Class tResultClass) { + return collection.distinct(clientSession, fieldName, filter, tResultClass); + } + @Override @NotNull + public FindIterable find() { + return collection.find(); + } + @Override @NotNull + public FindIterable find(@NotNull Class tResultClass) { + return collection.find(tResultClass); + } + @Override @NotNull + public FindIterable find(@NotNull Bson filter) { + return collection.find(filter); + } + @Override @NotNull + public FindIterable find(@NotNull Bson filter, @NotNull Class tResultClass) { + return collection.find(filter, tResultClass); + } + @Override @NotNull + public FindIterable find(@NotNull ClientSession clientSession) { + return collection.find(clientSession); + } + @Override @NotNull + public FindIterable find(@NotNull ClientSession clientSession, @NotNull Class tResultClass) { + return collection.find(clientSession, tResultClass); + } + @Override @NotNull + public FindIterable find(@NotNull ClientSession clientSession, @NotNull Bson filter) { + return collection.find(clientSession, filter); + } + @Override @NotNull + public FindIterable find(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull Class tResultClass) { + return collection.find(clientSession, filter, tResultClass); + } + @Override @NotNull + public AggregateIterable aggregate(@NotNull List pipeline) { + return collection.aggregate(pipeline); + } + @Override @NotNull + public AggregateIterable aggregate(@NotNull List pipeline, @NotNull Class tResultClass) { + return collection.aggregate(pipeline, tResultClass); + } + @Override @NotNull + public AggregateIterable aggregate(@NotNull ClientSession clientSession, @NotNull List pipeline) { + return collection.aggregate(clientSession, pipeline); + } + @Override @NotNull + public AggregateIterable aggregate(@NotNull ClientSession clientSession, @NotNull List pipeline, @NotNull Class tResultClass) { + return collection.aggregate(clientSession, pipeline, tResultClass); + } + @Override @NotNull + public ChangeStreamIterable watch() { + return collection.watch(); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull Class tResultClass) { + return collection.watch(tResultClass); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull List pipeline) { + return collection.watch(pipeline); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull List pipeline, @NotNull Class tResultClass) { + return collection.watch(pipeline, tResultClass); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull ClientSession clientSession) { + return collection.watch(clientSession); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull ClientSession clientSession, @NotNull Class tResultClass) { + return collection.watch(clientSession, tResultClass); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull ClientSession clientSession, @NotNull List pipeline) { + return collection.watch(clientSession, pipeline); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull ClientSession clientSession, @NotNull List pipeline, @NotNull Class tResultClass) { + return collection.watch(clientSession, pipeline, tResultClass); + } + @Override @NotNull + public MapReduceIterable mapReduce(@NotNull String mapFunction, @NotNull String reduceFunction) { + return collection.mapReduce(mapFunction, reduceFunction); + } + @Override @NotNull + public MapReduceIterable mapReduce(@NotNull String mapFunction, @NotNull String reduceFunction, @NotNull Class tResultClass) { + return collection.mapReduce(mapFunction, reduceFunction, tResultClass); + } + @Override @NotNull + public MapReduceIterable mapReduce(@NotNull ClientSession clientSession, @NotNull String mapFunction, @NotNull String reduceFunction) { + return collection.mapReduce(clientSession, mapFunction, reduceFunction); + } + @Override @NotNull + public MapReduceIterable mapReduce(@NotNull ClientSession clientSession, @NotNull String mapFunction, @NotNull String reduceFunction, @NotNull Class tResultClass) { + return collection.mapReduce(clientSession, mapFunction, reduceFunction, tResultClass); + } + @Override @NotNull + public BulkWriteResult bulkWrite(@NotNull List> requests) { + return collection.bulkWrite(requests); + } + @Override @NotNull + public BulkWriteResult bulkWrite(@NotNull List> requests, @NotNull BulkWriteOptions options) { + return collection.bulkWrite(requests, options); + } + @Override @NotNull + public BulkWriteResult bulkWrite(@NotNull ClientSession clientSession, @NotNull List> requests) { + return collection.bulkWrite(clientSession, requests); + } + @Override @NotNull + public BulkWriteResult bulkWrite(@NotNull ClientSession clientSession, @NotNull List> requests, @NotNull BulkWriteOptions options) { + return collection.bulkWrite(clientSession, requests, options); + } + @Override @NotNull + public InsertOneResult insertOne(@NotNull T t) { + return collection.insertOne(t); + } + @Override @NotNull + public InsertOneResult insertOne(@NotNull T t, @NotNull InsertOneOptions options) { + return collection.insertOne(t, options); + } + @Override @NotNull + public InsertOneResult insertOne(@NotNull ClientSession clientSession, @NotNull T t) { + return collection.insertOne(clientSession, t); + } + @Override @NotNull + public InsertOneResult insertOne(@NotNull ClientSession clientSession, @NotNull T t, @NotNull InsertOneOptions options) { + return collection.insertOne(clientSession, t, options); + } + @Override @NotNull + public InsertManyResult insertMany(@NotNull List ts) { + return collection.insertMany(ts); + } + @Override @NotNull + public InsertManyResult insertMany(@NotNull List ts, @NotNull InsertManyOptions options) { + return collection.insertMany(ts, options); + } + @Override @NotNull + public InsertManyResult insertMany(@NotNull ClientSession clientSession, @NotNull List ts) { + return collection.insertMany(clientSession, ts); + } + @Override @NotNull + public InsertManyResult insertMany(@NotNull ClientSession clientSession, @NotNull List ts, @NotNull InsertManyOptions options) { + return collection.insertMany(clientSession, ts, options); + } + @Override @NotNull + public DeleteResult deleteMany(@NotNull Bson filter) { + return collection.deleteMany(filter); + } + @Override @NotNull + public DeleteResult deleteMany(@NotNull Bson filter, @NotNull DeleteOptions options) { + return collection.deleteMany(filter, options); + } + @Override @NotNull + public DeleteResult deleteMany(@NotNull ClientSession clientSession, @NotNull Bson filter) { + return collection.deleteMany(clientSession, filter); + } + @Override @NotNull + public DeleteResult deleteMany(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull DeleteOptions options) { + return collection.deleteMany(clientSession, filter, options); + } + @Override @NotNull + public DeleteResult deleteOne(@NotNull Bson filter) { + return collection.deleteOne(filter); + } + @Override @NotNull + public DeleteResult deleteOne(@NotNull Bson filter, @NotNull DeleteOptions options) { + return collection.deleteOne(filter, options); + } + @Override @NotNull + public DeleteResult deleteOne(@NotNull ClientSession clientSession, @NotNull Bson filter) { + return collection.deleteOne(clientSession, filter); + } + @Override @NotNull + public DeleteResult deleteOne(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull DeleteOptions options) { + return collection.deleteOne(clientSession, filter, options); + } + @Override @NotNull + public UpdateResult replaceOne(@NotNull Bson filter, @NotNull T replacement) { + return collection.replaceOne(filter, replacement); + } + @Override @NotNull + public UpdateResult replaceOne(@NotNull Bson filter, @NotNull T replacement, @NotNull ReplaceOptions replaceOptions) { + return collection.replaceOne(filter, replacement, replaceOptions); + } + @Override @NotNull + public UpdateResult replaceOne(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull T replacement) { + return collection.replaceOne(clientSession, filter, replacement); + } + @Override @NotNull + public UpdateResult replaceOne(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull T replacement, @NotNull ReplaceOptions replaceOptions) { + return collection.replaceOne(clientSession, filter, replacement, replaceOptions); + } } diff --git a/src/main/java/xyz/srnyx/magicmongo/MagicDatabase.java b/src/main/java/xyz/srnyx/magicmongo/MagicDatabase.java new file mode 100644 index 0000000..b749d9f --- /dev/null +++ b/src/main/java/xyz/srnyx/magicmongo/MagicDatabase.java @@ -0,0 +1,316 @@ +package xyz.srnyx.magicmongo; + +import com.mongodb.ReadConcern; +import com.mongodb.ReadPreference; +import com.mongodb.WriteConcern; +import com.mongodb.client.*; +import com.mongodb.client.model.CreateCollectionOptions; +import com.mongodb.client.model.CreateViewOptions; + +import org.bson.Document; +import org.bson.codecs.configuration.CodecRegistry; +import org.bson.conversions.Bson; + +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +/** + * A wrapper class for {@link MongoDatabase} + */ +public class MagicDatabase implements MongoDatabase { + /** + * The {@link MongoDatabase} instance for this {@link MagicDatabase} + */ + @NotNull public final MongoDatabase database; + /** + * A map of names to classes of collections for this database + */ + @NotNull public final Map> nameToClass = new HashMap<>(); + /** + * A map of {@link MagicCollection mongo collections} for this database + */ + @NotNull public final Map, MagicCollection> collections = new HashMap<>(); + + /** + * Creates a new {@link MagicDatabase} instance + * + * @param database the {@link MongoDatabase} instance to use + */ + public MagicDatabase(@NotNull MongoDatabase database) { + this.database = database; + } + + /** + * Constructs, but doesn't load, a new {@link MagicCollection} with the given name and class + *
Useful if you want to store collections in your own way (e.g. if a class is used for multiple collections) + * + * @param name the name of the collection + * @param clazz the class of the collection + * + * @return the new {@link MagicCollection} + * + * @param the type of the class + */ + @NotNull + public MagicCollection newMagicCollection(@NotNull String name, @NotNull Class clazz) { + return new MagicCollection<>(this, name, clazz); + } + + /** + * Loads a new {@link MagicCollection} with the given name and class + * + * @param name the name of the collection + * @param clazz the class of the collection + * + * @return the new {@link MagicCollection} + * + * @param the type of the class + */ + @NotNull + public MagicCollection loadMagicCollection(@NotNull String name, @NotNull Class clazz) { + final MagicCollection collection = newMagicCollection(name, clazz); + nameToClass.put(name, clazz); + collections.put(clazz, collection); + return collection; + } + + /** + * Loads many new {@link MagicCollection MagicCollections} with the given names and classes + * + * @param toLoad a map of strings (names) and classes (types) of collections to load + * + * @return this {@link MagicDatabase} instance for chaining + */ + @NotNull + public MagicDatabase loadMagicCollections(@NotNull Map> toLoad) { + toLoad.forEach(this::loadMagicCollection); + return this; + } + + /** + * Gets the {@link MagicCollection} for the given class + * + * @param clazz the class to get the {@link MagicCollection} for + * + * @return the {@link MagicCollection} for the given class + * + * @param the type of the class + */ + @NotNull + public MagicCollection getMagicCollection(@NotNull Class clazz) { + final MagicCollection collection = collections.get(clazz); + if (collection == null) throw new IllegalArgumentException("No MagicCollection found for class " + clazz); + return (MagicCollection) collection; + } + + /** + * Gets the {@link MagicCollection} with the given name + *
Recommended to use {@link #getMagicCollection(Class)} instead + * + * @param name the name of the {@link MagicCollection} to get + * + * @return the {@link MagicCollection} with the given name + * + * @see #getMagicCollection(Class) + */ + @NotNull + public MagicCollection getMagicCollection(@NotNull String name) { + final Class clazz = nameToClass.get(name); + if (clazz == null) throw new IllegalArgumentException("No MagicCollection found with name " + name); + return getMagicCollection(clazz); + } + + // OVERRIDE METHODS FROM MongoDatabase + @Override @NotNull + public String getName() { + return database.getName(); + } + @Override @NotNull + public CodecRegistry getCodecRegistry() { + return database.getCodecRegistry(); + } + @Override @NotNull + public ReadPreference getReadPreference() { + return database.getReadPreference(); + } + @Override @NotNull + public WriteConcern getWriteConcern() { + return database.getWriteConcern(); + } + @Override @NotNull + public ReadConcern getReadConcern() { + return database.getReadConcern(); + } + @Override @NotNull + public MongoDatabase withCodecRegistry(@NotNull CodecRegistry codecRegistry) { + return database.withCodecRegistry(codecRegistry); + } + @Override @NotNull + public MongoDatabase withReadPreference(@NotNull ReadPreference readPreference) { + return database.withReadPreference(readPreference); + } + @Override @NotNull + public MongoDatabase withWriteConcern(@NotNull WriteConcern writeConcern) { + return database.withWriteConcern(writeConcern); + } + @Override @NotNull + public MongoDatabase withReadConcern(@NotNull ReadConcern readConcern) { + return database.withReadConcern(readConcern); + } + @Override @NotNull + public MongoCollection getCollection(@NotNull String collectionName) { + return database.getCollection(collectionName); + } + @Override @NotNull + public MongoCollection getCollection(@NotNull String collectionName, @NotNull Class tDocumentClass) { + return database.getCollection(collectionName, tDocumentClass); + } + @Override @NotNull + public Document runCommand(@NotNull Bson command) { + return database.runCommand(command); + } + @Override @NotNull + public Document runCommand(@NotNull Bson command, @NotNull ReadPreference readPreference) { + return database.runCommand(command, readPreference); + } + @Override @NotNull + public R runCommand(@NotNull Bson command, @NotNull Class tResultClass) { + return database.runCommand(command, tResultClass); + } + @Override @NotNull + public R runCommand(@NotNull Bson command, @NotNull ReadPreference readPreference, @NotNull Class tResultClass) { + return database.runCommand(command, readPreference, tResultClass); + } + @Override @NotNull + public Document runCommand(@NotNull ClientSession clientSession, @NotNull Bson command) { + return database.runCommand(clientSession, command); + } + @Override @NotNull + public Document runCommand(@NotNull ClientSession clientSession, @NotNull Bson command, @NotNull ReadPreference readPreference) { + return database.runCommand(clientSession, command, readPreference); + } + @Override @NotNull + public R runCommand(@NotNull ClientSession clientSession, @NotNull Bson command, @NotNull Class tResultClass) { + return database.runCommand(clientSession, command, tResultClass); + } + @Override @NotNull + public R runCommand(@NotNull ClientSession clientSession, @NotNull Bson command, @NotNull ReadPreference readPreference, @NotNull Class tResultClass) { + return database.runCommand(clientSession, command, readPreference, tResultClass); + } + @Override + public void drop() { + database.drop(); + } + @Override + public void drop(@NotNull ClientSession clientSession) { + database.drop(clientSession); + } + @Override @NotNull + public ListCollectionNamesIterable listCollectionNames() { + return database.listCollectionNames(); + } + @Override @NotNull + public ListCollectionsIterable listCollections() { + return database.listCollections(); + } + @Override @NotNull + public ListCollectionsIterable listCollections(@NotNull Class tResultClass) { + return database.listCollections(tResultClass); + } + @Override @NotNull + public ListCollectionNamesIterable listCollectionNames(@NotNull ClientSession clientSession) { + return database.listCollectionNames(clientSession); + } + @Override @NotNull + public ListCollectionsIterable listCollections(@NotNull ClientSession clientSession) { + return database.listCollections(clientSession); + } + @Override @NotNull + public ListCollectionsIterable listCollections(@NotNull ClientSession clientSession, @NotNull Class tResultClass) { + return database.listCollections(clientSession, tResultClass); + } + @Override + public void createCollection(@NotNull String collectionName) { + database.createCollection(collectionName); + } + @Override + public void createCollection(@NotNull String collectionName, @NotNull CreateCollectionOptions createCollectionOptions) { + database.createCollection(collectionName, createCollectionOptions); + } + @Override + public void createCollection(@NotNull ClientSession clientSession, @NotNull String collectionName) { + database.createCollection(clientSession, collectionName); + } + @Override + public void createCollection(@NotNull ClientSession clientSession, @NotNull String collectionName, @NotNull CreateCollectionOptions createCollectionOptions) { + database.createCollection(clientSession, collectionName, createCollectionOptions); + } + @Override + public void createView(@NotNull String viewName, @NotNull String viewOn, @NotNull List pipeline) { + database.createView(viewName, viewOn, pipeline); + } + @Override + public void createView(@NotNull String viewName, @NotNull String viewOn, @NotNull List pipeline, @NotNull CreateViewOptions createViewOptions) { + database.createView(viewName, viewOn, pipeline, createViewOptions); + } + @Override + public void createView(@NotNull ClientSession clientSession, @NotNull String viewName, @NotNull String viewOn, @NotNull List pipeline) { + database.createView(clientSession, viewName, viewOn, pipeline); + } + @Override + public void createView(@NotNull ClientSession clientSession, @NotNull String viewName, @NotNull String viewOn, @NotNull List pipeline, @NotNull CreateViewOptions createViewOptions) { + database.createView(clientSession, viewName, viewOn, pipeline, createViewOptions); + } + @Override @NotNull + public ChangeStreamIterable watch() { + return database.watch(); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull Class tResultClass) { + return database.watch(tResultClass); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull List pipeline) { + return database.watch(pipeline); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull List pipeline, @NotNull Class tResultClass) { + return database.watch(pipeline, tResultClass); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull ClientSession clientSession) { + return database.watch(clientSession); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull ClientSession clientSession, @NotNull Class tResultClass) { + return database.watch(clientSession, tResultClass); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull ClientSession clientSession, @NotNull List pipeline) { + return database.watch(clientSession, pipeline); + } + @Override @NotNull + public ChangeStreamIterable watch(@NotNull ClientSession clientSession, @NotNull List pipeline, @NotNull Class tResultClass) { + return database.watch(clientSession, pipeline, tResultClass); + } + @Override @NotNull + public AggregateIterable aggregate(@NotNull List pipeline) { + return database.aggregate(pipeline); + } + @Override @NotNull + public AggregateIterable aggregate(@NotNull List pipeline, @NotNull Class tResultClass) { + return database.aggregate(pipeline, tResultClass); + } + @Override @NotNull + public AggregateIterable aggregate(@NotNull ClientSession clientSession, @NotNull List pipeline) { + return database.aggregate(clientSession, pipeline); + } + @Override @NotNull + public AggregateIterable aggregate(@NotNull ClientSession clientSession, @NotNull List pipeline, @NotNull Class tResultClass) { + return database.aggregate(clientSession, pipeline, tResultClass); + } +} diff --git a/src/main/java/xyz/srnyx/magicmongo/MagicMongo.java b/src/main/java/xyz/srnyx/magicmongo/MagicMongo.java index ea9be07..26353db 100644 --- a/src/main/java/xyz/srnyx/magicmongo/MagicMongo.java +++ b/src/main/java/xyz/srnyx/magicmongo/MagicMongo.java @@ -13,53 +13,34 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.HashMap; -import java.util.Map; - /** - * A class for managing MongoDB collections using {@link MagicCollection MagicCollections} + * A class for managing Mongo using {@link MagicDatabase MagicDatabases} and {@link MagicCollection MagicCollections} + * + * @see SingleMongo + * @see MultiMongo */ public class MagicMongo { - /** - * The {@link MongoClient} - */ - @NotNull public MongoClient client; - /** - * The {@link MongoDatabase} - */ - @NotNull public MongoDatabase database; - /** - * A map of {@link MagicCollection mongo collections} - */ - @NotNull public final Map, MagicCollection> mongoCollections = new HashMap<>(); + @NotNull protected final ConnectionString connection; + @NotNull public final MongoClient client; + @Nullable private final CodecRegistry codecRegistry; /** - * Creates a new {@link MagicMongo} instance for managing MongoDB collections - * - * @param connectionUrl the connection URL for the MongoDB database - * @param codecRegistry the {@link CodecRegistry} to use for the MongoDB database + * Creates a new {@link MagicMongo} instance * - * @throws IllegalArgumentException if no database name is found in the connection URL + * @param connectionUrl the connection URL for the MongoDB database + * @param codecRegistry the {@link CodecRegistry} to use for the MongoDB database */ public MagicMongo(@NotNull String connectionUrl, @Nullable CodecRegistry codecRegistry) { - // Get database name - final ConnectionString connection = new ConnectionString(connectionUrl); - final String databaseName = connection.getDatabase(); - if (databaseName == null) throw new IllegalArgumentException("No database name found in connection URL " + connectionUrl); - - // Connect to database + this.connection = new ConnectionString(connectionUrl); client = MongoClients.create(connection); - database = client.getDatabase(databaseName); - if (codecRegistry != null) database = database.withCodecRegistry(codecRegistry); + this.codecRegistry = codecRegistry; } /** - * Creates a new {@link MagicMongo} instance for managing MongoDB collections with the default {@link CodecRegistry} + * Creates a new {@link MagicMongo} instance with the default {@link CodecRegistry} * - * @param connectionUrl the connection URL for the MongoDB database - * - * @throws IllegalArgumentException if no database name is found in the connection URL + * @param connectionUrl the connection URL for the MongoDB database */ public MagicMongo(@NotNull String connectionUrl) { this(connectionUrl, CodecRegistries.fromRegistries( @@ -68,84 +49,16 @@ public MagicMongo(@NotNull String connectionUrl) { } /** - * Constructs, but doesn't load, a new {@link MagicCollection} with the given name and class - *
Useful if you want to store collections in your own way (e.g. if a class is used for multiple collections) - * - * @param name the name of the collection - * @param clazz the class of the collection - * - * @return the new {@link MagicCollection} - * - * @param the type of the class - */ - @NotNull - public MagicCollection newCollection(@NotNull String name, @NotNull Class clazz) { - return new MagicCollection<>(database, name, clazz); - } - - /** - * Loads a new {@link MagicCollection} with the given name and class - * - * @param name the name of the collection - * @param clazz the class of the collection - * - * @return the new {@link MagicCollection} - * - * @param the type of the class - */ - @NotNull - public MagicCollection loadCollection(@NotNull String name, @NotNull Class clazz) { - final MagicCollection collection = newCollection(name, clazz); - mongoCollections.put(clazz, collection); - return collection; - } - - /** - * Loads many new {@link MagicCollection MagicCollections} with the given names and classes - * - * @param collections a map of strings (names) and classes (types) of collections to load - * - * @return this {@link MagicMongo} instance for chaining - */ - @NotNull - public MagicMongo loadCollections(@NotNull Map> collections) { - collections.forEach(this::loadCollection); - return this; - } - - /** - * Gets the {@link MagicCollection} for the given class - * - * @param clazz the class to get the {@link MagicCollection} for - * - * @return the {@link MagicCollection} for the given class - * - * @param the type of the class - */ - @NotNull - public MagicCollection getCollection(@NotNull Class clazz) { - final MagicCollection collection = mongoCollections.get(clazz); - if (collection == null) throw new IllegalArgumentException("No MagicCollection found for class " + clazz); - return (MagicCollection) collection; - } - - /** - * Gets the {@link MagicCollection} with the given name. - *
Highly recommended to use {@link #getCollection(Class)} instead! - * - * @param name the name of the {@link MagicCollection} to get + * Constructs a new {@link MagicDatabase} with the specified name * - * @return the {@link MagicCollection} with the given name + * @param name the name of the database to create * - * @see #getCollection(Class) + * @return the new {@link MagicDatabase} instance */ @NotNull - public MagicCollection getCollection(@NotNull String name) { - final MagicCollection collection = mongoCollections.values().stream() - .filter(collectionFilter -> collectionFilter.collection.getNamespace().getCollectionName().equals(name)) - .findFirst() - .orElse(null); - if (collection == null) throw new IllegalArgumentException("No MagicCollection found with name " + name); - return collection; + public MagicDatabase newMagicDatabase(@NotNull String name) { + final MongoDatabase database = client.getDatabase(name); + if (codecRegistry != null) database.withCodecRegistry(codecRegistry); + return new MagicDatabase(database); } } diff --git a/src/main/java/xyz/srnyx/magicmongo/MultiMongo.java b/src/main/java/xyz/srnyx/magicmongo/MultiMongo.java new file mode 100644 index 0000000..ab7d47f --- /dev/null +++ b/src/main/java/xyz/srnyx/magicmongo/MultiMongo.java @@ -0,0 +1,84 @@ +package xyz.srnyx.magicmongo; + +import org.bson.codecs.configuration.CodecRegistry; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.HashMap; +import java.util.Map; + + +/** + * A {@link MagicMongo} instance with multiple {@link MagicDatabase MagicDatabases} that can be loaded and accessed + */ +public class MultiMongo extends MagicMongo { + /** + * The {@link MagicDatabase MagicDatabases} that have been loaded + */ + @NotNull public final Map databases = new HashMap<>(); + + public MultiMongo(@NotNull String connectionUrl, @Nullable CodecRegistry codecRegistry) { + super(connectionUrl, codecRegistry); + + // Load database if specified + final String databaseName = connection.getDatabase(); + if (databaseName != null) loadMagicDatabase(databaseName); + } + + /** + * Constructs a new {@link MagicDatabase} with the specified name and loads it + * + * @param name the name of the database to create + * + * @return the new {@link MagicDatabase} instance + */ + @NotNull + public MagicDatabase loadMagicDatabase(@NotNull String name) { + final MagicDatabase database = newMagicDatabase(name); + databases.put(name, database); + return database; + } + + /** + * Constructs new {@link MagicDatabase MagicDatabases} with the specified names and loads them + * + * @param toLoad the names of the databases to create + * + * @return this {@link MultiMongo} instance + */ + @NotNull + public MultiMongo loadMagicDatabases(@NotNull Iterable toLoad) { + toLoad.forEach(this::loadMagicDatabase); + return this; + } + + /** + * Constructs new {@link MagicDatabase MagicDatabases} with the specified names and loads them + * + * @param toLoad the names of the databases to create + * + * @return this {@link MultiMongo} instance + */ + @NotNull + public MultiMongo loadMagicDatabases(@NotNull String... toLoad) { + for (final String name : toLoad) loadMagicDatabase(name); + return this; + } + + /** + * Gets a {@link MagicDatabase} by name + * + * @param name the name of the database to get + * + * @return the {@link MagicDatabase} instance + * + * @throws IllegalArgumentException if no {@link MagicDatabase} is found with the specified name + */ + @NotNull + public MagicDatabase getMagicDatabase(@NotNull String name) { + final MagicDatabase collection = databases.get(name); + if (collection == null) throw new IllegalArgumentException("No MagicDatabase found with name " + name); + return collection; + } +} diff --git a/src/main/java/xyz/srnyx/magicmongo/SingleMongo.java b/src/main/java/xyz/srnyx/magicmongo/SingleMongo.java new file mode 100644 index 0000000..fcf43f4 --- /dev/null +++ b/src/main/java/xyz/srnyx/magicmongo/SingleMongo.java @@ -0,0 +1,26 @@ +package xyz.srnyx.magicmongo; + +import org.bson.codecs.configuration.CodecRegistry; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + + +/** + * A {@link MagicMongo} instance with a single {@link MagicDatabase} that's specified in the connection URL + */ +public class SingleMongo extends MagicMongo { + /** + * The {@link MagicDatabase} instance for the single database + */ + @NotNull public final MagicDatabase database; + + public SingleMongo(@NotNull String connectionUrl, @Nullable CodecRegistry codecRegistry) { + super(connectionUrl, codecRegistry); + + // Load database + final String databaseName = connection.getDatabase(); + if (databaseName == null) throw new IllegalArgumentException("No database name found in connection URL: " + connectionUrl); + database = newMagicDatabase(databaseName); + } +}