Example usage for com.mongodb DBObject putAll

List of usage examples for com.mongodb DBObject putAll

Introduction

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

Prototype

void putAll(BSONObject o);

Source Link

Document

Sets all key/value pairs from an object into this object

Usage

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

License:Apache License

/**
 * Creates an instance of {@link MongoInputSplit} whose upper and lower bounds are restricted by adding $gt/$lte clauses to the query
 * filter. This requires that the boundaries are not compound keys, and that the query does not contain any keys used in the split key.
 *
 * @param chunkLowerBound the lower bound of the chunk (min)
 * @param chunkUpperBound the upper bound of the chunk (max)
 * @param query a query filtering the documents within the split
 * @return a MongoInputSplit from a range query
 * @throws IllegalArgumentException if the query conflicts with the chunk bounds, or the either of the bounds are compound keys.
 *//*  w w  w  .  j  av  a 2 s  . c o m*/
public MongoInputSplit createRangeQuerySplit(final BasicDBObject chunkLowerBound,
        final BasicDBObject chunkUpperBound, final BSONObject query) {

    //If the boundaries are actually empty, just return
    //a split without boundaries.
    if (chunkLowerBound == null && chunkUpperBound == null) {
        DBObject splitQuery = new BasicDBObject();
        splitQuery.putAll(query);
        MongoInputSplit split = new MongoInputSplit();
        split.setQuery(splitQuery);
        return split;
    }

    //The boundaries are not empty, so try to build
    //a split using $gt/$lte.

    //First check that the split contains no compound keys.
    // e.g. this is valid: { _id : "foo" }
    // but this is not {_id : "foo", name : "bar"}
    Entry<String, Object> minKey = chunkLowerBound != null && chunkLowerBound.keySet().size() == 1
            ? chunkLowerBound.entrySet().iterator().next()
            : null;
    Entry<String, Object> maxKey = chunkUpperBound != null && chunkUpperBound.keySet().size() == 1
            ? chunkUpperBound.entrySet().iterator().next()
            : null;
    if (minKey == null && maxKey == null) {
        throw new IllegalArgumentException(
                "Range query is enabled but one or more split boundaries contains a compound key:\n" + "min:  "
                        + chunkLowerBound + "\nmax:  " + chunkUpperBound);
    }

    //Now, check that the lower and upper bounds don't have any keys
    //which overlap with the query.
    if (minKey != null && query.containsField(minKey.getKey())
            || maxKey != null && query.containsField(maxKey.getKey())) {
        throw new IllegalArgumentException("Range query is enabled but split key conflicts with query filter:\n"
                + "min:  " + chunkLowerBound + "\nmax:  " + chunkUpperBound + "\nquery:  " + query);
    }

    String key = null;
    BasicDBObject rangeObj = new BasicDBObject();
    if (minKey != null) {
        key = minKey.getKey();
        rangeObj.put("$gte", minKey.getValue());
    }

    if (maxKey != null) {
        key = maxKey.getKey();
        rangeObj.put("$lt", maxKey.getValue());
    }

    DBObject splitQuery = new BasicDBObject();
    splitQuery.putAll(query);
    splitQuery.put(key, rangeObj);
    MongoInputSplit split = new MongoInputSplit();
    split.setQuery(splitQuery);
    split.setKeyField(MongoConfigUtil.getInputKey(getConfiguration()));
    return split;
}

From source file:de.fhg.igd.mongomvcc.impl.MongoDBVCollection.java

License:Open Source License

@Override
public VCursor find() {
    //ask index for OIDs
    IdMap objs = _branch.getIndex().find(_name);
    if (objs.size() == 0) {
        return MongoDBVCursor.EMPTY;
    }//from   w ww .  ja  va  2 s.  com

    //ask MongoDB for objects with the given OIDs
    if (objs.size() == 1) {
        //shortcut for one object
        return createCursor(_delegate.find(new BasicDBObject(OID, objs.values()[0]), EXCLUDEHIDDENATTRS), null);
    } else {
        DBObject qo = new BasicDBObject();
        qo.putAll(_branch.getQueryObject());
        return createCursor(_delegate.find(qo, EXCLUDEHIDDENATTRS), new OIDInIndexFilter());
    }
}

From source file:de.fhg.igd.mongomvcc.impl.MongoDBVCollection.java

License:Open Source License

@Override
public VCursor find(Map<String, Object> example) {
    DBObject o = new BasicDBObject();
    o.putAll(_branch.getQueryObject());
    o.putAll(example);//from  w  w  w .jav a2  s. c  o m
    return createCursor(_delegate.find(o, EXCLUDEHIDDENATTRS), new OIDInIndexFilter());
}

From source file:de.fhg.igd.mongomvcc.impl.MongoDBVCollection.java

License:Open Source License

@Override
public VCursor find(Map<String, Object> example, String... fields) {
    DBObject fo = new BasicDBObject();
    for (String f : fields) {
        fo.put(f, 1);/*from   w w w .j  a v  a2 s  . c  o m*/
    }

    //make sure UID and OID are also returned
    fo.put(UID, 1);
    fo.put(OID, 1);

    //exclude lifetime
    //FIXME MongoDB cannot currently mix including and excluding fields
    //FIXME if this is an issue for you, vote for https://jira.mongodb.org/browse/SERVER-391
    //fo.putAll(EXCLUDELIFETIME);

    DBObject o = new BasicDBObject();
    o.putAll(_branch.getQueryObject());
    o.putAll(example);
    return createCursor(_delegate.find(o, fo), new OIDInIndexFilter());
}

From source file:de.fhg.igd.mongomvcc.impl.MongoDBVCollection.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from   www  .ja  va  2s  .co m
public Map<String, Object> findOne(Map<String, Object> example) {
    DBObject o = new BasicDBObject();
    o.putAll(_branch.getQueryObject());
    o.putAll(example);
    OIDInIndexFilter filter = new OIDInIndexFilter();
    DBCursor c = _delegate.find(o, EXCLUDEHIDDENATTRS);
    for (DBObject obj : c) {
        if (filter.filter(obj)) {
            if (obj instanceof Map) {
                return (Map<String, Object>) obj;
            }
            return obj.toMap();
        }
    }
    return null;
}

From source file:jc.mongodb.command.DefaultMapPutCommand.java

License:Apache License

public V put(MongoMapContext<K, V> ctx, K key, V value, boolean insertIfAbsent, boolean returnNew) {
    DBObject dbObject;
    DBObject queryObject;// w  w w .  j a  v a2s.  c  om
    V old = null;

    queryObject = ctx.getKeySerializer().toDBObject(key, true, false);
    dbObject = ctx.getKeySerializer().toDBObject(key, false, false);
    dbObject.putAll(ctx.getValueSerializer().toDBObject(value, false, false));

    dbObject = ctx.getCollection().findAndModify(queryObject, null, null, false, dbObject, returnNew,
            insertIfAbsent);
    if (dbObject != null) {
        old = ctx.getValueSerializer().toElement(dbObject);
    }

    return old;
}

From source file:jc.mongodb.command.DefaultMapRemoveCommand.java

License:Apache License

@Override
public boolean remove(MongoMapContext<K, V> ctx, K key, V value) {
    DBObject queryObject = ctx.getKeySerializer().toDBObject(key, true, false);
    queryObject.putAll(ctx.getValueSerializer().toDBObject(value, true, false));

    return ctx.getCollection().remove(queryObject).getN() > 0;
}

From source file:jc.mongodb.command.DefaultMapReplaceCommand.java

License:Apache License

@Override
public boolean replace(MongoMapContext<K, V> ctx, K key, V oldValue, V newValue) {
    DBObject queryObject = ctx.getKeySerializer().toDBObject(key, true, false);
    DBObject dbObject = ctx.getKeySerializer().toDBObject(key, false, false);

    queryObject.putAll(ctx.getValueSerializer().toDBObject(oldValue, true, false));
    dbObject.putAll(ctx.getValueSerializer().toDBObject(newValue, false, false));

    return ctx.getCollection().update(queryObject, dbObject).getN() > 0;
}

From source file:jc.mongodb.command.DefaultMapReplaceCommand.java

License:Apache License

@Override
public V replace(MongoMapContext<K, V> ctx, K key, V value) {
    DBObject dbObject;
    DBObject queryObject;//from   w w  w  . ja  v  a2  s.  com
    V old = null;

    queryObject = ctx.getKeySerializer().toDBObject(key, true, false);
    dbObject = ctx.getKeySerializer().toDBObject(key, false, false);
    dbObject.putAll(ctx.getValueSerializer().toDBObject(value, false, false));

    dbObject = ctx.getCollection().findAndModify(queryObject, null, null, false, dbObject, false, false);
    if (dbObject != null) {
        old = ctx.getValueSerializer().toElement(dbObject);
    }

    return old;
}

From source file:jc.mongodb.MongoMapEntryDBObjectSerializer.java

License:Apache License

@Override
public DBObject toDBObject(Entry<K, V> element, boolean equalFunctions, boolean negate) {
    DBObject obj = keySerializer.toDBObject(element.getKey(), equalFunctions, negate);
    obj.putAll(valueSerializer.toDBObject(element.getValue(), equalFunctions, negate));

    return obj;/*www . j  a v a2  s.  com*/
}