Search in sources :

Example 1 with MappedField

use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.

the class DatastoreImpl method tryVersionedUpdate.

private <T> WriteResult tryVersionedUpdate(final DBCollection dbColl, final T entity, final DBObject dbObj, final Object idValue, final InsertOptions options, final MappedClass mc) {
    WriteResult wr;
    if (mc.getFieldsAnnotatedWith(Version.class).isEmpty()) {
        return null;
    }
    final MappedField mfVersion = mc.getMappedVersionField();
    final String versionKeyName = mfVersion.getNameToStore();
    Long oldVersion = (Long) mfVersion.getFieldValue(entity);
    long newVersion = nextValue(oldVersion);
    dbObj.put(versionKeyName, newVersion);
    if (idValue != null && newVersion != 1) {
        final Query<?> query = find(dbColl.getName(), entity.getClass()).disableValidation().filter(Mapper.ID_KEY, idValue).enableValidation().filter(versionKeyName, oldVersion);
        final UpdateResults res = update(query, dbObj, new UpdateOptions().bypassDocumentValidation(options.getBypassDocumentValidation()).writeConcern(options.getWriteConcern()));
        wr = res.getWriteResult();
        if (res.getUpdatedCount() != 1) {
            throw new ConcurrentModificationException(format("Entity of class %s (id='%s',version='%d') was concurrently updated.", entity.getClass().getName(), idValue, oldVersion));
        }
    } else {
        wr = saveDocument(dbColl, dbObj, options);
    }
    return wr;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) ConcurrentModificationException(java.util.ConcurrentModificationException) WriteResult(com.mongodb.WriteResult) Version(org.mongodb.morphia.annotations.Version) UpdateResults(org.mongodb.morphia.query.UpdateResults) DBCollectionUpdateOptions(com.mongodb.client.model.DBCollectionUpdateOptions)

Example 2 with MappedField

use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.

the class DatastoreImpl method update.

@Override
public <T> UpdateResults update(final Query<T> query, final UpdateOperations<T> operations, final UpdateOptions options) {
    DBCollection dbColl = query.getCollection();
    // TODO remove this after testing.
    if (dbColl == null) {
        dbColl = getCollection(query.getEntityClass());
    }
    final MappedClass mc = getMapper().getMappedClass(query.getEntityClass());
    final List<MappedField> fields = mc.getFieldsAnnotatedWith(Version.class);
    DBObject queryObject = query.getQueryObject();
    if (operations.isIsolated()) {
        queryObject.put("$isolated", true);
    }
    if (!fields.isEmpty()) {
        operations.inc(fields.get(0).getNameToStore(), 1);
    }
    final BasicDBObject update = (BasicDBObject) ((UpdateOpsImpl) operations).getOps();
    if (LOG.isTraceEnabled()) {
        LOG.trace(format("Executing update(%s) for query: %s, ops: %s, multi: %s, upsert: %s", dbColl.getName(), queryObject, update, options.isMulti(), options.isUpsert()));
    }
    return new UpdateResults(dbColl.update(queryObject, update, enforceWriteConcern(options, query.getEntityClass()).getOptions()));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateResults(org.mongodb.morphia.query.UpdateResults)

Example 3 with MappedField

use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.

the class DatastoreImpl method update.

@Override
@SuppressWarnings("unchecked")
public <T> UpdateResults update(final T entity, final UpdateOperations<T> operations) {
    if (entity instanceof Query) {
        return update((Query<T>) entity, operations);
    }
    final MappedClass mc = mapper.getMappedClass(entity);
    Query<?> query = createQuery(mapper.getMappedClass(entity).getClazz()).disableValidation().filter(Mapper.ID_KEY, mapper.getId(entity));
    if (!mc.getFieldsAnnotatedWith(Version.class).isEmpty()) {
        final MappedField field = mc.getFieldsAnnotatedWith(Version.class).get(0);
        query.field(field.getNameToStore()).equal(field.getFieldValue(entity));
    }
    return update((Query<T>) query, operations);
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Query(org.mongodb.morphia.query.Query) Version(org.mongodb.morphia.annotations.Version) MappedClass(org.mongodb.morphia.mapping.MappedClass)

Example 4 with MappedField

use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.

the class DatastoreImpl method update.

@SuppressWarnings("unchecked")
private <T> UpdateResults update(final Query<T> query, final DBObject update, final UpdateOptions options) {
    DBCollection dbColl = query.getCollection();
    // TODO remove this after testing.
    if (dbColl == null) {
        dbColl = getCollection(query.getEntityClass());
    }
    if (query.getSortObject() != null && query.getSortObject().keySet() != null && !query.getSortObject().keySet().isEmpty()) {
        throw new QueryException("sorting is not allowed for updates.");
    }
    if (query.getOffset() > 0) {
        throw new QueryException("a query offset is not allowed for updates.");
    }
    if (query.getLimit() > 0) {
        throw new QueryException("a query limit is not allowed for updates.");
    }
    DBObject queryObject = query.getQueryObject();
    final MappedClass mc = getMapper().getMappedClass(query.getEntityClass());
    final List<MappedField> fields = mc.getFieldsAnnotatedWith(Version.class);
    if (!fields.isEmpty()) {
        final MappedField versionMF = fields.get(0);
        if (update.get(versionMF.getNameToStore()) == null) {
            if (!update.containsField("$inc")) {
                update.put("$inc", new BasicDBObject(versionMF.getNameToStore(), 1));
            } else {
                ((Map<String, Object>) (update.get("$inc"))).put(versionMF.getNameToStore(), 1);
            }
        }
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace(format("Executing update(%s) for query: %s, ops: %s, multi: %s, upsert: %s", dbColl.getName(), queryObject, update, options.isMulti(), options.isUpsert()));
    }
    return new UpdateResults(dbColl.update(queryObject, update, enforceWriteConcern(options, query.getEntityClass()).getOptions()));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) QueryException(org.mongodb.morphia.query.QueryException) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) UpdateResults(org.mongodb.morphia.query.UpdateResults)

Example 5 with MappedField

use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.

the class IndexHelper method findField.

String findField(final MappedClass mc, final IndexOptions options, final List<String> path) {
    String segment = path.get(0);
    if (segment.equals("$**")) {
        return segment;
    }
    MappedField mf = mc.getMappedField(segment);
    if (mf == null) {
        mf = mc.getMappedFieldByJavaField(segment);
    }
    if (mf == null && mc.isInterface()) {
        for (final MappedClass mappedClass : mapper.getSubTypes(mc)) {
            try {
                return findField(mappedClass, options, new ArrayList<String>(path));
            } catch (MappingException e) {
            // try the next one
            }
        }
    }
    String namePath;
    if (mf != null) {
        namePath = mf.getNameToStore();
    } else {
        if (!options.disableValidation()) {
            throw pathFail(mc, path);
        } else {
            return join(path, '.');
        }
    }
    if (path.size() > 1) {
        try {
            Class concreteType = !mf.isSingleValue() ? mf.getSubClass() : mf.getConcreteType();
            namePath += "." + findField(mapper.getMappedClass(concreteType), options, path.subList(1, path.size()));
        } catch (MappingException e) {
            if (!options.disableValidation()) {
                throw pathFail(mc, path);
            } else {
                return join(path, '.');
            }
        }
    }
    return namePath;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) MappedClass(org.mongodb.morphia.mapping.MappedClass) MappedClass(org.mongodb.morphia.mapping.MappedClass) MappingException(org.mongodb.morphia.mapping.MappingException)

Aggregations

MappedField (org.mongodb.morphia.mapping.MappedField)68 MappedClass (org.mongodb.morphia.mapping.MappedClass)56 Test (org.junit.Test)50 ArrayList (java.util.ArrayList)47 Mapper (org.mongodb.morphia.mapping.Mapper)46 BasicDBObject (com.mongodb.BasicDBObject)13 DBObject (com.mongodb.DBObject)8 List (java.util.List)7 Key (org.mongodb.morphia.Key)6 SimpleEntity (org.mongodb.morphia.entities.SimpleEntity)5 ObjectId (org.bson.types.ObjectId)4 ValidationFailure (org.mongodb.morphia.query.validation.ValidationFailure)4 UpdateResults (org.mongodb.morphia.query.UpdateResults)3 DBCollection (com.mongodb.DBCollection)2 Index (org.mongodb.morphia.annotations.Index)2 NotSaved (org.mongodb.morphia.annotations.NotSaved)2 Version (org.mongodb.morphia.annotations.Version)2 MappingException (org.mongodb.morphia.mapping.MappingException)2 DBRef (com.mongodb.DBRef)1 WriteResult (com.mongodb.WriteResult)1