From 81f3d0a25b79ef50e86aca0c960cb631ca28fca1 Mon Sep 17 00:00:00 2001 From: Jakob Vogel Date: Tue, 16 Apr 2024 16:59:06 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Stores=20a=20reference=20to=20the=20raw=20M?= =?UTF-8?q?ongo=20object=20=F0=9F=96=87=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/sirius/db/mongo/Mango.java | 3 +++ src/main/java/sirius/db/mongo/MongoEntity.java | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/main/java/sirius/db/mongo/Mango.java b/src/main/java/sirius/db/mongo/Mango.java index a8ba2ba15..0bfa81f98 100644 --- a/src/main/java/sirius/db/mongo/Mango.java +++ b/src/main/java/sirius/db/mongo/Mango.java @@ -263,6 +263,9 @@ public static E make(EntityDescriptor descriptor, Doc do E result = (E) descriptor.make(Mango.class, null, key -> doc.getUnderlyingObject().containsKey(key) ? doc.get(key) : null); + + result.setMongoDocument(doc); + if (descriptor.isVersioned()) { result.setVersion(doc.get(VERSION).asInt(0)); } diff --git a/src/main/java/sirius/db/mongo/MongoEntity.java b/src/main/java/sirius/db/mongo/MongoEntity.java index b6c10b5af..47549d843 100644 --- a/src/main/java/sirius/db/mongo/MongoEntity.java +++ b/src/main/java/sirius/db/mongo/MongoEntity.java @@ -36,6 +36,9 @@ public abstract class MongoEntity extends BaseEntity { @Transient protected int version = 0; + @Transient + protected Doc mongoDocument; + @Part protected static Mongo mongo; @@ -99,4 +102,18 @@ public int getVersion() { public void setVersion(int version) { this.version = version; } + + /** + * Returns the underlying {@linkplain Doc Mongo document}. Be aware that this is only available after loading the + * {@linkplain MongoEntity entity} from the database, and the document is not updated automatically. + * + * @return the underlying Mongo document + */ + public Doc getMongoDocument() { + return mongoDocument; + } + + protected void setMongoDocument(Doc mongoDocument) { + this.mongoDocument = mongoDocument; + } } From b85243ad11aa4f67cc0bca5f8479723ba9946de4 Mon Sep 17 00:00:00 2001 From: Jakob Vogel Date: Tue, 16 Apr 2024 17:49:16 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Makes=20retaining=20of=20the=20raw=20docume?= =?UTF-8?q?nt=20optional/selectable=20=F0=9F=8E=9B=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/sirius/db/mongo/Mango.java | 15 +++++++++------ src/main/java/sirius/db/mongo/MongoQuery.java | 19 ++++++++++++++++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/sirius/db/mongo/Mango.java b/src/main/java/sirius/db/mongo/Mango.java index 0bfa81f98..3b9564c12 100644 --- a/src/main/java/sirius/db/mongo/Mango.java +++ b/src/main/java/sirius/db/mongo/Mango.java @@ -246,25 +246,28 @@ protected Optional findEntity(Object id, mongo.find(entityDescriptor.getRealm()); return finder.where(MongoEntity.ID, id.toString()) .singleIn(entityDescriptor.getRelationName()) - .map(doc -> make(entityDescriptor, doc)); + .map(doc -> make(entityDescriptor, doc, false)); } /** * Creates a new entity for the given descriptor based on the given doc. * - * @param descriptor the descriptor of the entity to create - * @param doc the document to read the values from - * @param the effective type of the generated entity + * @param descriptor the descriptor of the entity to create + * @param doc the document to read the values from + * @param retainRawDocument whether to retain the raw database document in the entity + * @param the effective type of the generated entity * @return the generated entity */ @SuppressWarnings("unchecked") - public static E make(EntityDescriptor descriptor, Doc doc) { + public static E make(EntityDescriptor descriptor, Doc doc, boolean retainRawDocument) { try { E result = (E) descriptor.make(Mango.class, null, key -> doc.getUnderlyingObject().containsKey(key) ? doc.get(key) : null); - result.setMongoDocument(doc); + if (retainRawDocument) { + result.setMongoDocument(doc); + } if (descriptor.isVersioned()) { result.setVersion(doc.get(VERSION).asInt(0)); diff --git a/src/main/java/sirius/db/mongo/MongoQuery.java b/src/main/java/sirius/db/mongo/MongoQuery.java index 973bc8e55..1743a56d5 100644 --- a/src/main/java/sirius/db/mongo/MongoQuery.java +++ b/src/main/java/sirius/db/mongo/MongoQuery.java @@ -45,6 +45,8 @@ public class MongoQuery extends Query, E, M private List facets; + private boolean retainRawDocuments = false; + @Part private static Mango mango; @@ -150,12 +152,23 @@ public MongoQuery markLongRunning() { return this; } + /** + * Marks the query to retain the raw database documents. + * + * @return the query itself for fluent method calls + */ + public MongoQuery retainingRawDocuments() { + retainRawDocuments = true; + return this; + } + @Override protected void doIterate(Predicate resultHandler) { if (forceFail) { return; } - finder.eachIn(descriptor.getRelationName(), doc -> resultHandler.test(Mango.make(descriptor, doc))); + finder.eachIn(descriptor.getRelationName(), + doc -> resultHandler.test(Mango.make(descriptor, doc, retainRawDocuments))); } @Override @@ -206,7 +219,7 @@ protected Iterator pullNextBlock() { if (lastId != null) { query.where(QueryBuilder.FILTERS.gt(MongoEntity.ID, lastId)); } - query.allIn(relation, doc -> buffer.add(Mango.make(descriptor, doc))); + query.allIn(relation, doc -> buffer.add(Mango.make(descriptor, doc, retainRawDocuments))); if (!buffer.isEmpty()) { lastId = buffer.getLast().getId(); @@ -273,7 +286,7 @@ public List randomList() { } finder.sample(descriptor.getRelationName(), doc -> { - result.add(Mango.make(descriptor, doc)); + result.add(Mango.make(descriptor, doc, retainRawDocuments)); failOnOverflow(result); return true; });