Skip to content

Commit

Permalink
Add remove() to Query
Browse files Browse the repository at this point in the history
fixes #1355
  • Loading branch information
evanchooly committed Jun 6, 2019
1 parent 3a57c7a commit 1faa48b
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 154 deletions.
4 changes: 3 additions & 1 deletion morphia/src/main/java/dev/morphia/AdvancedDatastore.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public interface AdvancedDatastore extends Datastore {
* @param <T> the type of the entities
* @return the new keys of the inserted entities
*/
<T> Iterable<Key<T>> insert(Iterable<T> entities);
default <T> Iterable<Key<T>> insert(Iterable<T> entities) {
return insert(entities, new InsertOptions());
}

/**
* Inserts entities in to the mapped collection.
Expand Down
21 changes: 5 additions & 16 deletions morphia/src/main/java/dev/morphia/Datastore.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import dev.morphia.aggregation.AggregationPipeline;
import dev.morphia.annotations.Indexed;
Expand Down Expand Up @@ -59,7 +60,9 @@ public interface Datastore {
* @param query the query to use when finding documents to delete
* @param <T> the type to delete
* @return results of the delete
* @deprecated use {@link Query#remove()} instead
*/
@Deprecated(since = "2.0", forRemoval = true)
<T> WriteResult delete(Query<T> query);

/**
Expand All @@ -70,7 +73,9 @@ public interface Datastore {
* @param <T> the type to delete
* @return results of the delete
* @since 1.3
* @deprecated use {@link Query#remove(DeleteOptions)} instead
*/
@Deprecated(since = "2.0", forRemoval = true)
<T> WriteResult delete(Query<T> query, DeleteOptions options);

/**
Expand Down Expand Up @@ -293,22 +298,6 @@ public interface Datastore {
*/
MongoDatabase getDatabase();

/**
* @return the default WriteConcern used by this Datastore
* @deprecated {@link MongoClient#setWriteConcern(WriteConcern)}
*/
@Deprecated
WriteConcern getDefaultWriteConcern();

/**
* Sets the default WriteConcern for this Datastore
*
* @param wc the default WriteConcern to be used by this Datastore
* @deprecated {@link MongoClient#setWriteConcern(WriteConcern)}
*/
@Deprecated
void setDefaultWriteConcern(WriteConcern wc);

/**
* Creates a (type-safe) reference to the entity; if stored this will become a {@link com.mongodb.DBRef}
*
Expand Down
54 changes: 4 additions & 50 deletions morphia/src/main/java/dev/morphia/DatastoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class DatastoreImpl implements AdvancedDatastore {
private final IndexHelper indexHelper;
private DB db;
private Mapper mapper;
private WriteConcern defConcern;

private volatile QueryFactory queryFactory = new DefaultQueryFactory();

Expand All @@ -96,7 +95,6 @@ class DatastoreImpl implements AdvancedDatastore {
.withCodecRegistry(fromRegistries(mongoClient.getMongoClientOptions().getCodecRegistry(),
getDefaultCodecRegistry()));
this.db = mongoClient.getDB(dbName);
this.defConcern = mongoClient.getWriteConcern();
this.indexHelper = new IndexHelper(mapper, database);
}

Expand Down Expand Up @@ -138,20 +136,12 @@ public <T> UpdateOperations<T> createUpdateOperations(final Class<T> clazz) {

@Override
public <T> WriteResult delete(final Query<T> query, final DeleteOptions options) {
final QueryImpl queryImpl = (QueryImpl) query;

DBCollection dbColl = queryImpl.getCollection();
// TODO remove this after testing.
if (dbColl == null) {
dbColl = getCollection(queryImpl.getEntityClass());
}

return dbColl.remove(queryImpl.getQueryObject(), enforceWriteConcern(options, queryImpl.getEntityClass()).getOptions());
return query.remove(options);
}

@Override
public <T> WriteResult delete(final Query<T> query) {
return delete(query, new DeleteOptions().writeConcern(getWriteConcern(((QueryImpl) query).getEntityClass())));
return query.remove(new DeleteOptions().writeConcern(getWriteConcern(((QueryImpl) query).getEntityClass())));
}

@Override
Expand All @@ -172,11 +162,7 @@ public <T> WriteResult delete(final T entity, final DeleteOptions options) {
if (wrapped instanceof Class<?>) {
throw new MappingException("Did you mean to delete all documents? -- delete(ds.createQuery(???.class))");
}
try {
return delete(createQuery(wrapped.getClass()).filter("_id", mapper.getId(wrapped)), options);
} catch (Exception e) {
throw new RuntimeException(e);
}
return find(wrapped.getClass()).filter("_id", mapper.getId(wrapped)).remove(options);
}

@Override
Expand Down Expand Up @@ -436,16 +422,6 @@ public MongoDatabase getDatabase() {
return database;
}

@Override
public WriteConcern getDefaultWriteConcern() {
return defConcern;
}

@Override
public void setDefaultWriteConcern(final WriteConcern wc) {
defConcern = wc;
}

@Override
@Deprecated
// use mapper instead.
Expand Down Expand Up @@ -757,19 +733,6 @@ public void setMapper(final Mapper mapper) {
this.mapper = mapper;
}

/**
* Inserts entities in to the database
*
* @param entities the entities to insert
* @param <T> the type of the entities
* @return the keys of entities
*/
@Override
public <T> Iterable<Key<T>> insert(final Iterable<T> entities) {
return insert(entities, new InsertOptions()
.writeConcern(defConcern));
}

/**
* Inserts an entity in to the database
*
Expand Down Expand Up @@ -837,15 +800,6 @@ <T> UpdateOptions enforceWriteConcern(final UpdateOptions options, final Class<T
return options;
}

<T> DeleteOptions enforceWriteConcern(final DeleteOptions options, final Class<T> klass) {
if (options.getWriteConcern() == null) {
return options
.copy()
.writeConcern(getWriteConcern(klass));
}
return options;
}

protected <T> Key<T> save(final DBCollection dbColl, final T entity, final InsertOptions options) {
final MappedClass mc = validateSave(entity);

Expand Down Expand Up @@ -1153,7 +1107,7 @@ private <T> UpdateResults update(final QueryImpl<T> query, final DBObject update
* @param clazzOrEntity the class or entity to use when looking up the WriteConcern
*/
private WriteConcern getWriteConcern(final Object clazzOrEntity) {
WriteConcern wc = defConcern;
WriteConcern wc = getMongo().getWriteConcern();
if (clazzOrEntity != null) {
final Entity entityAnn = getMapper().getMappedClass(clazzOrEntity).getEntityAnnotation();
if (entityAnn != null && !entityAnn.concern().isEmpty()) {
Expand Down
6 changes: 5 additions & 1 deletion morphia/src/main/java/dev/morphia/DeleteOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ public DeleteOptions writeConcern(final WriteConcern writeConcern) {
return this;
}

DBCollectionRemoveOptions getOptions() {
/**
* @morphia.internal
* @return the internal options implementation
*/
public DBCollectionRemoveOptions getOptions() {
return options;
}
}
2 changes: 0 additions & 2 deletions morphia/src/main/java/dev/morphia/mapping/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,7 @@ public <T> Class<T> getClassFromCollection(final String collection) {
* @param object the object to process
* @return the collection name
* @morphia.internal
* @deprecated no replacement is planned
*/
@Deprecated
public String getCollectionName(final Object object) {
if (object == null) {
throw new IllegalArgumentException();
Expand Down
7 changes: 7 additions & 0 deletions morphia/src/main/java/dev/morphia/query/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import com.mongodb.DBObject;
import com.mongodb.ReadPreference;
import com.mongodb.WriteResult;
import dev.morphia.DeleteOptions;
import dev.morphia.Key;
import dev.morphia.query.internal.MorphiaCursor;
import dev.morphia.query.internal.MorphiaKeyCursor;
Expand Down Expand Up @@ -472,4 +474,9 @@ public interface Query<T> {
@Deprecated
Key<T> getKey(FindOptions options);

default WriteResult remove() {
return remove(new DeleteOptions());
}

WriteResult remove(DeleteOptions options);
}
33 changes: 31 additions & 2 deletions morphia/src/main/java/dev/morphia/query/QueryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.DBCollectionFindOptions;
import dev.morphia.Datastore;
import dev.morphia.DeleteOptions;
import dev.morphia.Key;
import dev.morphia.annotations.Entity;
import dev.morphia.internal.PathTarget;
Expand Down Expand Up @@ -230,7 +233,6 @@ public int getBatchSize() {

/**
* @return the collection this query targets
*
* @morphia.internal
*/
public DBCollection getCollection() {
Expand Down Expand Up @@ -279,8 +281,8 @@ public int getOffset() {
}

/**
* @morphia.internal
* @return the query object
* @morphia.internal
*/
public DBObject getQueryObject() {
final DBObject obj = new BasicDBObject();
Expand Down Expand Up @@ -531,6 +533,12 @@ public Query<T> where(final CodeWScope js) {
return this;
}

@Override
public WriteResult remove(final DeleteOptions options) {
return getCollection()
.remove(getQueryObject(), enforceWriteConcern(options, getEntityClass()).getOptions());
}

@Override
public String getFieldName() {
throw new UnsupportedOperationException("this method is unused on a Query");
Expand Down Expand Up @@ -744,4 +752,25 @@ public void remove(final Criteria criteria) {
public void attach(final CriteriaContainer container) {
compoundContainer.attach(container);
}

private DeleteOptions enforceWriteConcern(final DeleteOptions options, final Class<T> klass) {
if (options.getWriteConcern() == null) {
return options
.copy()
.writeConcern(getWriteConcern(klass));
}
return options;
}

private WriteConcern getWriteConcern(final Object clazzOrEntity) {
WriteConcern wc = ds.getMongo().getWriteConcern();
if (clazzOrEntity != null) {
final Entity entityAnn = mapper.getMappedClass(clazzOrEntity).getEntityAnnotation();
if (entityAnn != null && !entityAnn.concern().isEmpty()) {
wc = WriteConcern.valueOf(entityAnn.concern());
}
}

return wc;
}
}
31 changes: 1 addition & 30 deletions morphia/src/test/java/dev/morphia/TestDatastore.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.WriteConcern;
import com.mongodb.client.model.Collation;
import com.mongodb.client.model.CollationStrength;
import dev.morphia.annotations.Entity;
Expand All @@ -39,7 +38,6 @@
import dev.morphia.testmodel.Rectangle;
import org.bson.types.ObjectId;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.util.ArrayList;
Expand All @@ -49,7 +47,6 @@
import static com.mongodb.ReadPreference.secondaryPreferred;
import static com.mongodb.WriteConcern.ACKNOWLEDGED;
import static com.mongodb.WriteConcern.MAJORITY;
import static com.mongodb.WriteConcern.W2;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -145,7 +142,7 @@ public void testGet() {
assertNotNull(res.get(1));
assertNotNull(res.get(1).username);

getDs().getCollection(FacebookUser.class).remove(new BasicDBObject());
getDs().find(FacebookUser.class).remove();
getAds().insert(fbUsers);
assertEquals(4, getDs().find(FacebookUser.class).count());
assertNotNull(getDs().find(FacebookUser.class).filter("_id", 1).first());
Expand Down Expand Up @@ -500,32 +497,6 @@ public void testEnforceWriteConcern() {
.getWriteConcern());
assertEquals(MAJORITY, ds.enforceWriteConcern(updateOptions.writeConcern(MAJORITY), FacebookUser.class)
.getWriteConcern());

DeleteOptions deleteOptions = new DeleteOptions();
assertNull(deleteOptions.getWriteConcern());

assertEquals(ACKNOWLEDGED, ds.enforceWriteConcern(deleteOptions, FacebookUser.class)
.getWriteConcern());
assertEquals(MAJORITY, ds.enforceWriteConcern(deleteOptions.writeConcern(MAJORITY), FacebookUser.class)
.getWriteConcern());
}

@Test
public void entityWriteConcern() {
ensureEntityWriteConcern();

getDs().setDefaultWriteConcern(WriteConcern.UNACKNOWLEDGED);
ensureEntityWriteConcern();
}

private void ensureEntityWriteConcern() {
DatastoreImpl datastore = (DatastoreImpl) getAds();
assertEquals(ACKNOWLEDGED, datastore.enforceWriteConcern(new InsertOptions(), Simple.class)
.getWriteConcern());
assertEquals(ACKNOWLEDGED, datastore.enforceWriteConcern(new UpdateOptions(), Simple.class)
.getWriteConcern());
assertEquals(ACKNOWLEDGED, datastore.enforceWriteConcern(new FindAndModifyOptions(), Simple.class)
.getWriteConcern());
}

@Test
Expand Down
Loading

0 comments on commit 1faa48b

Please sign in to comment.