Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<sqlite.version>3.49.1.0</sqlite.version>
<teradata.version>13.10.00.35</teradata.version>
<firebird.version>5.0.7.java11</firebird.version>
<mongodb.version>3.12.14</mongodb.version>
<mongodb.version>4.11.5</mongodb.version>

<r2dbc.version>1.0.0.RELEASE</r2dbc.version>

Expand All @@ -141,7 +141,7 @@
<surefire.version>3.5.3</surefire.version>
<animal-sniffer.version>1.24</animal-sniffer.version>
<h2gis.version>2.2.3</h2gis.version>
<morphia.version>1.3.2</morphia.version>
<morphia.version>2.4.18</morphia.version>
<jmh.version>1.37</jmh.version>
<kotlin.version>2.1.20</kotlin.version>
<ksp.version>2.1.20-2.0.1</ksp.version>
Expand Down Expand Up @@ -454,7 +454,7 @@
<configuration>
<parameter>
<excludes>
<exclude>com.querydsl.core.annotations.QueryProjection</exclude>
<exclude>com.querydsl.core.annotations.QueryProjection</exclude>
</excludes>
<ignoreMissingOldVersion>true</ignoreMissingOldVersion>
<onlyModified>true</onlyModified>
Expand Down
2 changes: 1 addition & 1 deletion querydsl-libraries/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<exclude>org.eclipse.persistence:eclipselink:*:*:compile</exclude>
<exclude>org.datanucleus:javax.jdo:*:*:compile</exclude>
<exclude>org.springframework.roo:org.springframework.roo.annotations:*:*:compile</exclude>
<exclude>org.mongodb.morphia:morphia:*:*:compile</exclude>
<exclude>dev.morphia:core:*:*:compile</exclude>
<exclude>org.joda:joda-money:*:*:compile</exclude>
<exclude>org.batoo.jpa:batoo-jpa:*:*:compile</exclude>
<exclude>jakarta.annotation:jakarta.annotation-api:*:*:compile</exclude>
Expand Down
14 changes: 10 additions & 4 deletions querydsl-libraries/querydsl-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
<description>Mongodb support for Querydsl</description>

Comment thread
kamilkrzywanski marked this conversation as resolved.
<properties>
<japicmp.skip>true</japicmp.skip>
<osgi.import.package>com.mongodb;version="0.0.0",
org.mongodb.morphia.*;version="1.3.2",
dev.morphia.*;version="1.6.1",
org.bson.*;version="0.0.0",
${osgi.import.package.root}</osgi.import.package>
</properties>
Expand All @@ -27,12 +28,17 @@
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<artifactId>mongodb-driver-sync</artifactId>
<version>${mongodb.version}</version>
</dependency>
<dependency>
<groupId>org.mongodb.morphia</groupId>
<artifactId>morphia</artifactId>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-legacy</artifactId>
<version>${mongodb.version}</version>
</dependency>
<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>morphia-core</artifactId>
<version>${morphia.version}</version>
<scope>provided</scope>
<optional>true</optional>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
package com.querydsl.mongodb;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.ReadPreference;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mysema.commons.lang.CloseableIterator;
import com.querydsl.core.DefaultQueryMetadata;
import com.querydsl.core.Fetchable;
Expand All @@ -44,6 +43,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.bson.conversions.Bson;
import org.jetbrains.annotations.Nullable;

/**
Expand All @@ -64,22 +64,22 @@

private final QueryMixin<Q> queryMixin;

private final DBCollection collection;
private final MongoCollection collection;

private final Function<DBObject, K> transformer;
private final Function<K, Object> transformer;

private ReadPreference readPreference;

/**
* Create a new MongodbQuery instance
*
* @param collection collection
* @param transformer result transformer
* @param transformer id transformer
* @param serializer serializer
*/
@SuppressWarnings("unchecked")
public AbstractMongodbQuery(
DBCollection collection, Function<DBObject, K> transformer, MongodbSerializer serializer) {
MongoCollection collection, Function<K, Object> transformer, MongodbSerializer serializer) {
@SuppressWarnings("unchecked") // Q is this plus subclass
var query = (Q) this;
this.queryMixin = new QueryMixin<>(query, new DefaultQueryMetadata(), false);
Expand Down Expand Up @@ -122,7 +122,7 @@
return new AnyEmbeddedBuilder<>(queryMixin, collection);
}

protected abstract DBCollection getCollection(Class<?> type);
protected abstract MongoCollection getCollection(Class<?> type);

@Nullable
protected Predicate createFilter(QueryMetadata metadata) {
Expand Down Expand Up @@ -164,16 +164,15 @@
// TODO : fetch only ids
var cursor =
createCursor(
collection,
condition,
null,
QueryModifiers.EMPTY,
Collections.<OrderSpecifier<?>>emptyList());
collection,
condition,
null,
QueryModifiers.EMPTY,
Collections.<OrderSpecifier<?>>emptyList())
.cursor();
if (cursor.hasNext()) {
List<Object> ids = new ArrayList<>(cursor.count());
for (DBObject obj : cursor) {
ids.add(obj.get("_id"));
}
List<Object> ids = new ArrayList<>();
cursor.forEachRemaining(document -> ids.add(transformer.apply(document)));
return ids;
} else {
return Collections.emptyList();
Expand Down Expand Up @@ -236,7 +235,7 @@

@Override
public CloseableIterator<K> iterate() {
final var cursor = createCursor();
final var cursor = createCursor().cursor();
return new CloseableIterator<>() {
@Override
public boolean hasNext() {
Expand All @@ -245,7 +244,7 @@

@Override
public K next() {
return transformer.apply(cursor.next());
return cursor.next();
}

@Override
Expand All @@ -272,16 +271,16 @@
try {
var cursor = createCursor();
List<K> results = new ArrayList<>();
for (DBObject dbObject : cursor) {
results.add(transformer.apply(dbObject));
for (K dbObject : cursor) {
results.add(dbObject);
}
return results;
} catch (NoResults ex) {
return Collections.emptyList();
}
}

protected DBCursor createCursor() {
protected FindIterable<K> createCursor() {
var metadata = queryMixin.getMetadata();
Predicate filter = createFilter(metadata);
return createCursor(
Expand All @@ -292,13 +291,19 @@
metadata.getOrderBy());
}

protected DBCursor createCursor(
DBCollection collection,
protected FindIterable<K> createCursor(
MongoCollection collection,
@Nullable Predicate where,
Expression<?> projection,
QueryModifiers modifiers,
List<OrderSpecifier<?>> orderBy) {
var cursor = collection.find(createQuery(where), createProjection(projection));
var cursor =
readPreference != null
? collection
.withReadPreference(readPreference)
.find(createQuery(where))
.projection(createProjection(projection))

Check warning on line 305 in querydsl-libraries/querydsl-mongodb/src/main/java/com/querydsl/mongodb/AbstractMongodbQuery.java

View check run for this annotation

Codecov / codecov/patch

querydsl-libraries/querydsl-mongodb/src/main/java/com/querydsl/mongodb/AbstractMongodbQuery.java#L303-L305

Added lines #L303 - L305 were not covered by tests
: collection.find(createQuery(where)).projection(createProjection(projection));
Integer limit = modifiers.getLimitAsInteger();
Integer offset = modifiers.getOffsetAsInteger();
if (limit != null) {
Expand All @@ -310,15 +315,12 @@
if (orderBy.size() > 0) {
cursor.sort(serializer.toSort(orderBy));
}
if (readPreference != null) {
cursor.setReadPreference(readPreference);
}
return cursor;
}

private DBObject createProjection(Expression<?> projection) {
private Bson createProjection(Expression<?> projection) {
if (projection instanceof FactoryExpression) {
DBObject obj = new BasicDBObject();
var obj = new BasicDBObject();
for (Object expr : ((FactoryExpression) projection).getArgs()) {
if (expr instanceof Expression) {
obj.put((String) serializer.handle((Expression) expr), 1);
Expand All @@ -343,9 +345,9 @@
@Override
public K fetchFirst() {
try {
var c = createCursor().limit(1);
var c = createCursor().limit(1).cursor();
if (c.hasNext()) {
return transformer.apply(c.next());
return c.next();
} else {
return null;
}
Expand All @@ -372,9 +374,9 @@
if (limit == null) {
limit = 2L;
}
var c = createCursor().limit(limit.intValue());
var c = createCursor().limit(limit.intValue()).cursor();
if (c.hasNext()) {
var rv = transformer.apply(c.next());
var rv = c.next();
if (c.hasNext()) {
throw new NonUniqueResultException();
}
Expand Down Expand Up @@ -416,15 +418,15 @@
public long fetchCount() {
try {
Predicate filter = createFilter(queryMixin.getMetadata());
return collection.count(createQuery(filter));
return collection.countDocuments(createQuery(filter));
} catch (NoResults ex) {
return 0L;
}
}

private DBObject createQuery(@Nullable Predicate predicate) {
private Bson createQuery(@Nullable Predicate predicate) {
if (predicate != null) {
return (DBObject) serializer.handle(predicate);
return (Bson) serializer.handle(predicate);
} else {
return new BasicDBObject();
}
Expand All @@ -444,7 +446,7 @@
*
* @return
*/
public DBObject asDBObject() {
public Bson asDBObject() {
return createQuery(queryMixin.getMetadata().getWhere());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Map;
import java.util.regex.Pattern;
import org.bson.BSONObject;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

/**
Expand All @@ -52,7 +53,7 @@ public Object handle(Expression<?> expression) {
return expression.accept(this, null);
}

public DBObject toSort(List<OrderSpecifier<?>> orderBys) {
public Bson toSort(List<OrderSpecifier<?>> orderBys) {
var sort = new BasicDBObject();
for (OrderSpecifier<?> orderBy : orderBys) {
Object key = orderBy.getTarget().accept(this, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public QueryResults<K> fetchResults() {
public long fetchCount() {
try {
Predicate filter = createFilter(getQueryMixin().getMetadata());
return collection.count(createQuery(filter));
return collection.countDocuments(createQuery(filter));
} catch (NoResults ex) {
return 0L;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.querydsl.mongodb.document;

import dev.morphia.Datastore;
import dev.morphia.mapping.Mapper;
import dev.morphia.mapping.codec.writer.DocumentWriter;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;

public class DocumentUtils {

Check warning on line 9 in querydsl-libraries/querydsl-mongodb/src/main/java/com/querydsl/mongodb/document/DocumentUtils.java

View check run for this annotation

Codecov / codecov/patch

querydsl-libraries/querydsl-mongodb/src/main/java/com/querydsl/mongodb/document/DocumentUtils.java#L9

Added line #L9 was not covered by tests
public static Document getAsDocument(Datastore morphia, Object value) {
Mapper mapper = morphia.getMapper();
CodecRegistry codecRegistry = morphia.getCodecRegistry();
return DocumentWriter.encode(value, mapper, codecRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@
*/
package com.querydsl.mongodb.morphia;

import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.querydsl.core.types.EntityPath;
import com.querydsl.mongodb.AbstractMongodbQuery;
import java.util.function.Function;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.mapping.cache.DefaultEntityCache;
import org.mongodb.morphia.mapping.cache.EntityCache;
import dev.morphia.Datastore;

/**
* {@code MorphiaQuery} extends {@link AbstractMongodbQuery} with Morphia specific transformations
Expand All @@ -43,49 +38,27 @@
*/
public class MorphiaQuery<K> extends AbstractMongodbQuery<K, MorphiaQuery<K>> {

private final EntityCache cache;

private final Datastore datastore;

public MorphiaQuery(Morphia morphia, Datastore datastore, EntityPath<K> entityPath) {
this(morphia, datastore, new DefaultEntityCache(), entityPath);
}

public MorphiaQuery(Morphia morphia, Datastore datastore, Class<? extends K> entityType) {
this(morphia, datastore, new DefaultEntityCache(), entityType);
}

public MorphiaQuery(
Morphia morphia, Datastore datastore, EntityCache cache, EntityPath<K> entityPath) {
this(morphia, datastore, cache, entityPath.getType());
public MorphiaQuery(Datastore datastore, EntityPath<K> entityPath) {
this(datastore, entityPath.getType());
}

public MorphiaQuery(
final Morphia morphia,
final Datastore datastore,
final EntityCache cache,
final Class<? extends K> entityType) {
public MorphiaQuery(final Datastore datastore, final Class<? extends K> entityType) {
super(
datastore.getCollection(entityType),
new Function<DBObject, K>() {
@Override
public K apply(DBObject dbObject) {
return morphia.fromDBObject(datastore, entityType, dbObject, cache);
}
},
new MorphiaSerializer(morphia));
dbObject -> datastore.getMapper().getId(dbObject),
new MorphiaSerializer(datastore));
this.datastore = datastore;
this.cache = cache;
}

@Override
protected DBCursor createCursor() {
cache.flush();
protected FindIterable<K> createCursor() {
return super.createCursor();
}

@Override
protected DBCollection getCollection(Class<?> type) {
protected MongoCollection getCollection(Class<?> type) {
return datastore.getCollection(type);
}
}
Loading