Example usage for com.mongodb BasicDBObject putAll

List of usage examples for com.mongodb BasicDBObject putAll

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject putAll.

Prototype

@SuppressWarnings("unchecked")
    @Override
    public void putAll(final Map m) 

Source Link

Usage

From source file:com.stratio.connector.mongodb.core.engine.metadata.UpdateDBObjectBuilder.java

License:Apache License

/**
 * Adds an update relation./* www. ja va 2  s.c  o m*/
 *
 * @param left
 *            the left selector
 * @param operator
 *            the operator
 * @param right
 *            the right selector
 * @throws ExecutionException
 *             the execution exception
 */
public void addUpdateRelation(Selector left, Operator operator, Selector right) throws ExecutionException {
    BasicDBObject basicDBObject;

    switch (operator) {

    case EQ:
    case ASSIGN:
        if (containsAnInnerRelation(left, right)) {
            Relation innerRelation = ((RelationSelector) right).getRelation();
            addUpdateRelation(innerRelation.getLeftTerm(), innerRelation.getOperator(),
                    innerRelation.getRightTerm());
        } else {
            if (relations.containsField(SET_COMMAND)) {
                basicDBObject = (BasicDBObject) relations.get(SET_COMMAND);
                basicDBObject.putAll(getBasicRelation(left, right));
            } else {
                basicDBObject = new BasicDBObject();
                basicDBObject.putAll(getBasicRelation(left, right));
                relations.put(SET_COMMAND, basicDBObject);
            }
        }
        break;

    case ADD:
        if (relations.containsField(INCREMENT_COMMAND)) {
            basicDBObject = (BasicDBObject) relations.get(INCREMENT_COMMAND);
            basicDBObject.putAll(getIncrementalRelation(left, right, false));
        } else {
            basicDBObject = new BasicDBObject();
            basicDBObject.putAll(getIncrementalRelation(left, right, false));
            relations.put(INCREMENT_COMMAND, basicDBObject);
        }
        break;

    case SUBTRACT:
        if (relations.containsField(INCREMENT_COMMAND)) {
            basicDBObject = (BasicDBObject) relations.get(INCREMENT_COMMAND);
            basicDBObject.putAll(getIncrementalRelation(left, right, true));
        } else {
            basicDBObject = new BasicDBObject();
            basicDBObject.putAll(getIncrementalRelation(left, right, true));
            relations.put(INCREMENT_COMMAND, basicDBObject);
        }
        break;

    case MULTIPLICATION:
        if (relations.containsField(MUTLTIPLICATION_COMMAND)) {
            basicDBObject = (BasicDBObject) relations.get(MUTLTIPLICATION_COMMAND);
            basicDBObject.putAll(getNumberRelation(left, right));
        } else {
            basicDBObject = new BasicDBObject();
            basicDBObject.putAll(getNumberRelation(left, right));
            relations.put(MUTLTIPLICATION_COMMAND, basicDBObject);
        }
        break;

    default:
        String msg = "Operator: " + operator + " is not supported in update queries";
        LOGGER.error(msg);
        throw new MongoValidationException(msg);
    }

}

From source file:com.tomtom.speedtools.mongodb.MongoDBQuery.java

License:Apache License

@Nonnull
public <T> MongoDBQuery elemMatch(@Nonnull final EntityMapper<?>.Field<T> f0,
        @Nonnull final MongoDBQuery... q) {
    assert f0 != null;
    assert q != null;

    final String fieldName = mongoPath(f0);
    append(fieldName, new FieldSelection(OPERATOR_ELEM_MATCH, fieldName) {
        @Override//ww  w .j  ava  2s .c o  m
        @Nonnull
        Object toDBValue() throws MapperException {
            final BasicDBObject elemMatch = new BasicDBObject();

            for (final MongoDBQuery query : q) {
                elemMatch.putAll(query.toDBObject());
            }
            return elemMatch;
        }
    });
    return this;
}

From source file:com.zjy.mongo.splitter.MongoCollectionSplitter.java

License:Apache License

/**
 * Create an instance of MongoInputSplit that represents a view of this splitter's input URI between the given lower/upper bounds. If
 * this splitter has range queries enabled, it will attempt to use $gt/$lte clauses in the query construct to create the split,
 * otherwise it will use min/max index boundaries (default behavior)
 *
 * @param lowerBound the lower bound of the collection
 * @param upperBound the upper bound of the collection
 * @return a MongoInputSplit in the given bounds
 * @throws SplitFailedException if the split could not be created
 *///from w  ww  .ja v a2s. com
public MongoInputSplit createSplitFromBounds(final BasicDBObject lowerBound, final BasicDBObject upperBound)
        throws SplitFailedException {
    LOG.info("Created split: min=" + (lowerBound != null ? lowerBound.toString() : "null") + ", max= "
            + (upperBound != null ? upperBound.toString() : "null"));
    //Objects to contain upper/lower bounds for each split
    DBObject splitMin = new BasicDBObject();
    DBObject splitMax = new BasicDBObject();
    if (lowerBound != null) {
        for (Entry<String, Object> entry : lowerBound.entrySet()) {
            String key = entry.getKey();
            Object val = entry.getValue();

            if (!val.equals(MIN_KEY_TYPE)) {
                splitMin.put(key, val);
            }
        }
    }

    if (upperBound != null) {
        for (Entry<String, Object> entry : upperBound.entrySet()) {
            String key = entry.getKey();
            Object val = entry.getValue();

            if (!val.equals(MAX_KEY_TYPE)) {
                splitMax.put(key, val);
            }
        }
    }

    MongoInputSplit split = null;

    //If enabled, attempt to build the split using $gt/$lte
    DBObject query = MongoConfigUtil.getQuery(getConfiguration());
    if (MongoConfigUtil.isRangeQueryEnabled(getConfiguration())) {
        try {
            query = MongoConfigUtil.getQuery(getConfiguration());
            split = createRangeQuerySplit(lowerBound, upperBound, query);
        } catch (Exception e) {
            throw new SplitFailedException("Couldn't use range query to create split: " + e.getMessage());
        }
    }
    if (split == null) {
        split = new MongoInputSplit();
        BasicDBObject splitQuery = new BasicDBObject();
        if (query != null) {
            splitQuery.putAll(query);
        }
        split.setQuery(splitQuery);
        split.setMin(splitMin);
        split.setMax(splitMax);
    }
    split.setInputURI(MongoConfigUtil.getInputURI(getConfiguration()));
    split.setAuthURI(MongoConfigUtil.getAuthURI(getConfiguration()));
    split.setNoTimeout(MongoConfigUtil.isNoTimeout(getConfiguration()));
    split.setFields(MongoConfigUtil.getFields(getConfiguration()));
    split.setSort(MongoConfigUtil.getSort(getConfiguration()));
    split.setKeyField(MongoConfigUtil.getInputKey(getConfiguration()));
    return split;
}

From source file:de.uni_koeln.spinfo.maalr.mongo.core.Converter.java

License:Apache License

private static BasicDBObject convertLemmaVersion(LemmaVersion lemmaVersion) {
    BasicDBObject obj = new BasicDBObject();
    Map<String, String> toStore = new HashMap<String, String>(lemmaVersion.getEntryValues());
    toStore.keySet().removeAll(LemmaVersion.MAALR_KEYS);
    toStore.putAll(lemmaVersion.getMaalrValues());
    obj.putAll(toStore);
    obj.put(LemmaVersion.TIMESTAMP, lemmaVersion.getTimestamp());
    return obj;//  w  w w .java 2  s.  c om
}

From source file:edu.sjsu.carbonated.mongodbaccessors.MongoDBAlbum.java

License:Apache License

/**
 * Uses the getMap method in AlbumResource to insert into the database.
 * <p>//www  .  j  av a  2  s  .  c  o  m
 * Make sure no fields are null and that album_id has already been set
 * before reaching here.
 * 
 * @param albumRes
 */
public void addAlbum(AlbumResource albumRes) {

    BasicDBObject info = new BasicDBObject();
    info.putAll(albumRes.getMap());
    albumColl.insert(info);

}

From source file:edu.sjsu.carbonated.mongodbaccessors.MongoDBAlbum.java

License:Apache License

public void addPhotoToAlbum(AlbumResource albumRes) {

    // check to see if the user has the rights to the album
    DBCursor cur = albumColl.find(new BasicDBObject("$and", JSON.parse("[{\"album_id\":\""
            + albumRes.getAlbum_id() + "\"},{\"user_id\":\"" + albumRes.getUser_id() + "\"}]")));

    // if the is no match betwen an album id and user id, throw error
    if (!cur.hasNext()) {
        throw new WebApplicationException(Response.status(400).entity("Album not found").build());

    }//w w  w . ja  va  2  s .c  o  m

    BasicDBObject info = new BasicDBObject();
    info.putAll(albumRes.getMap());
    info.put("photo_url", photo_url_location + albumRes.getAlbum_id() + "/" + albumRes.getPhoto_id() + ".png");
    photoColl.insert(info);

}

From source file:edu.sjsu.carbonated.mongodbaccessors.MongoDBBlog.java

License:Apache License

public void addEntry(BlogResource blogRes) {

    BasicDBObject info = new BasicDBObject();

    info.putAll(blogRes.getMap());
    collection.insert(info);//from   w  w  w .  j a va  2 s .  c  o  m

}

From source file:edu.sjsu.carbonated.mongodbaccessors.MongoDBGroup.java

License:Apache License

public void addEntry(GroupResource groupRes) {

    BasicDBObject info = new BasicDBObject();

    info.putAll(groupRes.getMap());
    collection.insert(info);/*from  w w  w . j  a  va2s.c o  m*/

}

From source file:fr.gouv.vitam.cases.DbRequest.java

License:Open Source License

private final ResultInterface getRequest1LevelMaipFromMD(final TypeRequest request,
        final ResultInterface previous, final boolean useStart)
        throws InvalidExecOperationException, InstantiationException, IllegalAccessException {
    BasicDBObject query = null;
    if (request.requestModel == null) {
        throw new InvalidExecOperationException(
                "Expression is not valid for Daip Level 1 with MD only since no MD request is available");
    }//from   w w  w  .  jav a  2s .c o m
    if (useStart) {
        query = getInClauseForField(DAip.ID, previous.getCurrentDaip());
    } else {
        if (previous.getMinLevel() == 1) {
            query = getInClauseForField(CassandraAccess.VitamLinks.Domain2DAip.field2to1,
                    previous.getCurrentDaip());
        } else {
            query = getInClauseForField(CassandraAccess.VitamLinks.DAip2DAip.field2to1,
                    previous.getCurrentDaip());
        }
    }
    final String srequest = request.requestModel.toString();
    final BasicDBObject condition = (BasicDBObject) JSON.parse(srequest);
    query.putAll((BSONObject) condition);
    final ResultInterface subresult = CassandraAccess.createOneResult();
    if (simulate) {
        LOGGER.info("Req1LevelMD: {}", query);
        return createFalseResult(previous, 1);
    }
    LOGGER.debug("Req1LevelMD: {}", query);
    if (GlobalDatas.PRINT_REQUEST) {
        LOGGER.warn("Req1LevelMD: {}", query);
    }
    final DBCursor cursor = mdAccess.find(mdAccess.daips, query, ID_NBCHILD);
    long tempCount = 0;
    while (cursor.hasNext()) {
        final DAip maip = (DAip) cursor.next();
        final String mid = maip.getId();
        if (useStart) {
            if (previous.getCurrentDaip().contains(mid)) {
                subresult.getCurrentDaip().add(mid);
                tempCount += maip.getLong(Domain.NBCHILD);
            }
        } else {
            subresult.getCurrentDaip().add(mid);
            tempCount += maip.getLong(Domain.NBCHILD);
        }
    }
    cursor.close();
    subresult.setNbSubNodes(tempCount);
    // filter on Ancestor
    if (!useStart && !previous.checkAncestor(mdAccess, subresult)) {
        LOGGER.error("No ancestor for " + query + "\n" + previous.getCurrentDaip() + " not in "
                + subresult.getCurrentDaip());
        return null;
    }
    // Not updateMinMax since result is not "valid" path but node UUID and not needed
    subresult.setMinLevel(previous.getMinLevel() + 1);
    subresult.setMaxLevel(previous.getMaxLevel() + 1);
    if (GlobalDatas.PRINT_REQUEST) {
        subresult.putBeforeSave();
        LOGGER.warn("MetaAip2: {}", subresult);
    }
    return subresult;
}

From source file:fr.gouv.vitam.cases.DbRequest.java

License:Open Source License

private final ResultInterface getRequestNegativeRelativeDepthFromMD(final TypeRequest request,
        final ResultInterface previous, final boolean useStart)
        throws InvalidExecOperationException, InstantiationException, IllegalAccessException {
    BasicDBObject query = null;
    if (request.requestModel == null) {
        throw new InvalidExecOperationException("Expression is not valid for Daip Level "
                + request.relativedepth + " with MD only since no MD request is available");
    }/*  www.  ja v  a  2s  .com*/
    if (useStart) {
        throw new InvalidExecOperationException("Cannot make a negative path when starting up");
    }
    if (simulate) {
        LOGGER.info("Req-xLevelMD");
        return createFalseResult(previous, 1);
    }
    int distance = -request.relativedepth;
    Set<String> subset = new HashSet<String>();
    for (String prev : previous.getCurrentDaip()) {
        DAip dprev = DAip.findOne(mdAccess, prev);
        Map<String, Integer> parents = dprev.getDomDepth();
        for (Entry<String, Integer> elt : parents.entrySet()) {
            if (elt.getValue() == distance) {
                subset.add(elt.getKey());
            }
        }
    }
    // Use ID and not graph dependencies
    query = getInClauseForField(DAip.ID, subset);
    final String srequest = request.requestModel.toString();
    final BasicDBObject condition = (BasicDBObject) JSON.parse(srequest);
    query.putAll((BSONObject) condition);
    final ResultInterface subresult = CassandraAccess.createOneResult();
    LOGGER.debug("Req-xLevelMD: {}", query);
    if (GlobalDatas.PRINT_REQUEST) {
        LOGGER.warn("Req-xLevelMD: {}", query);
    }
    final DBCursor cursor = mdAccess.find(mdAccess.daips, query, ID_NBCHILD);
    long tempCount = 0;
    subresult.setMinLevel(previous.getMaxLevel());
    subresult.setMaxLevel(0);
    while (cursor.hasNext()) {
        final DAip maip = (DAip) cursor.next();
        final String mid = maip.getId();
        subresult.getCurrentDaip().add(mid);
        maip.load(mdAccess);
        tempCount += maip.getLong(DAip.NBCHILD);
        // Not updateMinMax since result is not "valid" path but node UUID and not needed
        int max = maip.getMaxDepth();
        if (subresult.getMaxLevel() < max) {
            subresult.setMaxLevel(max);
        }
        if (subresult.getMinLevel() > max) {
            subresult.setMinLevel(max);
        }
    }
    cursor.close();
    subresult.setNbSubNodes(tempCount);
    if (GlobalDatas.PRINT_REQUEST) {
        subresult.putBeforeSave();
        LOGGER.warn("MetaAip2: {}", subresult);
    }
    return subresult;
}