Example usage for com.mongodb BasicDBObjectBuilder add

List of usage examples for com.mongodb BasicDBObjectBuilder add

Introduction

In this page you can find the example usage for com.mongodb BasicDBObjectBuilder add.

Prototype

public BasicDBObjectBuilder add(final String key, final Object val) 

Source Link

Document

Same as append

Usage

From source file:org.apache.rya.mongodb.instance.MongoDetailsAdapter.java

License:Apache License

/**
 * Serializes {@link RyaDetails} to mongo {@link DBObject}.
 * @param details - The details to be serialized.
 * @return The mongo {@link DBObject}.//w w w.  jav a 2s. c om
 */
public static BasicDBObject toDBObject(final RyaDetails details) {
    Preconditions.checkNotNull(details);
    final BasicDBObjectBuilder builder = BasicDBObjectBuilder.start()
            .add(INSTANCE_KEY, details.getRyaInstanceName()).add(VERSION_KEY, details.getRyaVersion())
            .add(ENTITY_DETAILS_KEY, details.getEntityCentricIndexDetails().isEnabled())
            //RYA-215            .add(GEO_DETAILS_KEY, details.getGeoIndexDetails().isEnabled())
            .add(PCJ_DETAILS_KEY, toDBObject(details.getPCJIndexDetails()))
            .add(TEMPORAL_DETAILS_KEY, details.getTemporalIndexDetails().isEnabled())
            .add(FREETEXT_DETAILS_KEY, details.getFreeTextIndexDetails().isEnabled());
    if (details.getProspectorDetails().getLastUpdated().isPresent()) {
        builder.add(PROSPECTOR_DETAILS_KEY, details.getProspectorDetails().getLastUpdated().get());
    }
    if (details.getJoinSelectivityDetails().getLastUpdated().isPresent()) {
        builder.add(JOIN_SELECTIVITY_DETAILS_KEY, details.getJoinSelectivityDetails().getLastUpdated().get());
    }
    return (BasicDBObject) builder.get();
}

From source file:org.apache.rya.mongodb.instance.MongoDetailsAdapter.java

License:Apache License

private static DBObject toDBObject(final PCJIndexDetails pcjIndexDetails) {
    requireNonNull(pcjIndexDetails);//ww  w .  j av a2 s .co  m

    final BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();

    // Is Enabled
    builder.add(PCJ_ENABLED_KEY, pcjIndexDetails.isEnabled());

    // Fluo Details if present.
    if (pcjIndexDetails.getFluoDetails().isPresent()) {
        builder.add(PCJ_FLUO_KEY, pcjIndexDetails.getFluoDetails().get().getUpdateAppName());
    }

    // Add the PCJDetail objects.
    final List<DBObject> pcjDetailsList = new ArrayList<>();
    for (final PCJDetails pcjDetails : pcjIndexDetails.getPCJDetails().values()) {
        pcjDetailsList.add(toDBObject(pcjDetails));
    }
    builder.add(PCJ_PCJS_KEY, pcjDetailsList.toArray());

    return builder.get();
}

From source file:org.apache.rya.mongodb.instance.MongoDetailsAdapter.java

License:Apache License

static DBObject toDBObject(final PCJDetails pcjDetails) {
    requireNonNull(pcjDetails);/* www . ja  v  a 2  s  . c  o m*/

    final BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();

    // PCJ ID
    builder.add(PCJ_ID_KEY, pcjDetails.getId());

    // PCJ Update Strategy if present.
    if (pcjDetails.getUpdateStrategy().isPresent()) {
        builder.add(PCJ_UPDATE_STRAT_KEY, pcjDetails.getUpdateStrategy().get().name());
    }

    // Last Update Time if present.
    if (pcjDetails.getLastUpdateTime().isPresent()) {
        builder.add(PCJ_LAST_UPDATE_KEY, pcjDetails.getLastUpdateTime().get());
    }

    return builder.get();
}

From source file:org.apache.whirr.service.mongodb.MongoDBReplSetMemberClusterActionHandler.java

License:Apache License

/** 
 * Returns a BasicDBObject containing a replica set config object, usable as param to rs.initiate().
 * /*from   www . j  a v a  2s. com*/
 * @param memberInstances   A set containing instances to be included in config object.
 * @throws IOException      Thrown if the private IP of any instance is unavailable
 *
 */
private BasicDBObject generateReplicaSetConfig(Set<Cluster.Instance> memberInstances) throws IOException {
    BasicDBObject returnVal = new BasicDBObject();

    if (this.replicaSetName != null) {
        returnVal.put("_id", this.replicaSetName);
    } else {
        returnVal.put("_id", "whirr");
    }
    int counter = 0;

    ArrayList replicaSetMembers = new ArrayList();
    for (Cluster.Instance member : memberInstances) {
        BasicDBObjectBuilder hostObj = BasicDBObjectBuilder.start().add("_id", counter);
        if (member.getRoles().contains(MongoDBArbiterClusterActionHandler.ROLE)) {
            //it's an arbiter, use port from config file
            hostObj.add("host", member.getPrivateAddress().getHostAddress() + ":" + this.arbiterPort);
            hostObj.add("arbiterOnly", true);
        } else {
            // it's a data member
            hostObj.add("host", member.getPrivateAddress().getHostAddress() + ":" + this.port); // throws an IOExc
        }

        replicaSetMembers.add(hostObj.get());
        counter++;
    }

    returnVal.put("members", replicaSetMembers);
    return returnVal;
}

From source file:org.envirocar.server.mongo.dao.MongoMeasurementDao.java

License:Open Source License

private Measurements getMongo(MeasurementFilter request) {
    BasicDBObjectBuilder q = new BasicDBObjectBuilder();
    if (request.hasGeometry()) {
        q.add(MongoMeasurement.GEOMETRY, withinGeometry(request.getGeometry()));
    }//from   w w  w  .ja v  a2  s  .c  o m
    if (request.hasTrack()) {
        q.add(MongoMeasurement.TRACK, ref(request.getTrack()));
    }
    if (request.hasUser()) {
        q.add(MongoMeasurement.USER, ref(request.getUser()));
    }
    if (request.hasTemporalFilter()) {
        q.add(MongoMeasurement.TIME, MongoUtils.temporalFilter(request.getTemporalFilter()));
    }
    return query(q.get(), request.getPagination());
}

From source file:org.envirocar.server.mongo.dao.MongoStatisticsDao.java

License:Open Source License

private DBObject matches(StatisticsFilter request) {
    BasicDBObjectBuilder b = new BasicDBObjectBuilder();
    BasicDBObjectBuilder match = b.push(Ops.MATCH);
    if (request.hasTrack()) {
        DBRef track = mongoDB.ref(request.getTrack());
        match.add(MongoMeasurement.TRACK, track);
    }/*from  w  w  w.j a v  a  2s .c om*/
    if (request.hasUser()) {
        DBRef user = mongoDB.ref(request.getUser());
        match.add(MongoMeasurement.USER, user);
    }
    if (request.hasSensor()) {
        MongoSensor sensor = (MongoSensor) request.getSensor();
        match.add(SENSOR_ID_PATH, sensor.getId());
    }
    return b.get();
}

From source file:org.mongoj.samples.service.persistence.CarPersistenceImpl.java

License:Open Source License

protected Car updateImpl(org.mongoj.samples.model.Car car) throws UpdateException, SystemException {
    DBCollection collection = getDB().getCollection(CarImpl.COLLECTION_NAME);

    if (car.isNew()) {
        car.setNew(false);/*from   ww w.ja v a2s.  co m*/

        CarImpl carImpl = (CarImpl) car;

        carImpl.addMap.clear();
        carImpl.appendMap.clear();
        carImpl.removeMap.clear();
        carImpl.setMap.clear();

        WriteResult writeResult = collection.insert(getDBObject(car));

        String err = writeResult.getError();

        if (err != null) {
            throw new UpdateException(err);
        }
    } else {
        DBObject criteria = new QueryBuilder().put("_id").is(new ObjectId(car.getId())).get();

        CarImpl carImpl = (CarImpl) car;

        BasicDBObjectBuilder updateBuilder = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder setUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder pushUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder pushAllUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder addUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder removeUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder removeAllUpdates = BasicDBObjectBuilder.start();

        for (String field : carImpl.setMap.keySet()) {
            setUpdates = setUpdates.add(field, carImpl.setMap.get(field));
        }

        if (!setUpdates.isEmpty()) {
            updateBuilder.add(SET_OPERATOR, setUpdates.get());
        }

        for (String field : carImpl.appendMap.keySet()) {
            List<Object> list = (List<Object>) carImpl.appendMap.get(field);

            if (!list.isEmpty()) {
                if (list.size() == 1) {
                    pushUpdates = pushUpdates.add(field, ((List) carImpl.appendMap.get(field)).get(0));
                } else {
                    pushAllUpdates = pushAllUpdates.add(field, carImpl.appendMap.get(field));
                }
            }
        }

        if (!pushUpdates.isEmpty()) {
            updateBuilder.add(PUSH_OPERATOR, pushUpdates.get());
        }

        if (!pushAllUpdates.isEmpty()) {
            updateBuilder.add(PUSH_ALL_OPERATOR, pushAllUpdates.get());
        }

        for (String field : carImpl.addMap.keySet()) {
            List<Object> list = (List<Object>) carImpl.addMap.get(field);

            if (!list.isEmpty()) {
                if (list.size() == 1) {
                    addUpdates = addUpdates.add(field, ((List) carImpl.addMap.get(field)).get(0));
                } else {
                    DBObject each = BasicDBObjectBuilder.start()
                            .add(EACH_OPERATOR, ((List) carImpl.addMap.get(field)).toArray()).get();

                    addUpdates = addUpdates.add(field, each);
                }
            }
        }

        if (!addUpdates.isEmpty()) {
            updateBuilder.add(ADD_TO_SET_OPERATOR, addUpdates.get());
        }

        for (String field : carImpl.removeMap.keySet()) {
            List<Object> list = (List<Object>) carImpl.removeMap.get(field);

            if (!list.isEmpty()) {
                if (list.size() == 1) {
                    removeUpdates = removeUpdates.add(field, ((List) carImpl.removeMap.get(field)).get(0));
                } else {
                    removeAllUpdates = removeAllUpdates.add(field, carImpl.removeMap.get(field));
                }
            }
        }

        if (!removeUpdates.isEmpty()) {
            updateBuilder.add(PULL_OPERATOR, removeUpdates.get());
        }

        if (!removeAllUpdates.isEmpty()) {
            updateBuilder.add(PULL_ALL_OPERATOR, removeAllUpdates.get());
        }

        if (!updateBuilder.isEmpty()) {
            DBObject update = updateBuilder.get();

            _log.debug("Update query = {}", update);

            WriteResult writeResult = collection.update(criteria, update);

            String err = writeResult.getError();

            if (err != null) {
                throw new UpdateException(err);
            }
        }
    }

    return car;
}

From source file:org.mongoj.samples.service.persistence.UserPersistenceImpl.java

License:Open Source License

protected User updateImpl(org.mongoj.samples.model.User user) throws UpdateException, SystemException {
    DBCollection collection = getDB().getCollection(UserImpl.COLLECTION_NAME);

    if (user.isNew()) {
        user.setNew(false);//from ww w.j  av  a  2  s.c  o m

        UserImpl userImpl = (UserImpl) user;

        userImpl.addMap.clear();
        userImpl.appendMap.clear();
        userImpl.removeMap.clear();
        userImpl.setMap.clear();

        WriteResult writeResult = collection.insert(getDBObject(user));

        String err = writeResult.getError();

        if (err != null) {
            throw new UpdateException(err);
        }
    } else {
        DBObject criteria = new QueryBuilder().put("_id").is(new ObjectId(user.getId())).get();

        UserImpl userImpl = (UserImpl) user;

        BasicDBObjectBuilder updateBuilder = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder setUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder pushUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder pushAllUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder addUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder removeUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder removeAllUpdates = BasicDBObjectBuilder.start();

        for (String field : userImpl.setMap.keySet()) {
            setUpdates = setUpdates.add(field, userImpl.setMap.get(field));
        }

        if (!setUpdates.isEmpty()) {
            updateBuilder.add(SET_OPERATOR, setUpdates.get());
        }

        for (String field : userImpl.appendMap.keySet()) {
            List<Object> list = (List<Object>) userImpl.appendMap.get(field);

            if (!list.isEmpty()) {
                if (list.size() == 1) {
                    pushUpdates = pushUpdates.add(field, ((List) userImpl.appendMap.get(field)).get(0));
                } else {
                    pushAllUpdates = pushAllUpdates.add(field, userImpl.appendMap.get(field));
                }
            }
        }

        if (!pushUpdates.isEmpty()) {
            updateBuilder.add(PUSH_OPERATOR, pushUpdates.get());
        }

        if (!pushAllUpdates.isEmpty()) {
            updateBuilder.add(PUSH_ALL_OPERATOR, pushAllUpdates.get());
        }

        for (String field : userImpl.addMap.keySet()) {
            List<Object> list = (List<Object>) userImpl.addMap.get(field);

            if (!list.isEmpty()) {
                if (list.size() == 1) {
                    addUpdates = addUpdates.add(field, ((List) userImpl.addMap.get(field)).get(0));
                } else {
                    DBObject each = BasicDBObjectBuilder.start()
                            .add(EACH_OPERATOR, ((List) userImpl.addMap.get(field)).toArray()).get();

                    addUpdates = addUpdates.add(field, each);
                }
            }
        }

        if (!addUpdates.isEmpty()) {
            updateBuilder.add(ADD_TO_SET_OPERATOR, addUpdates.get());
        }

        for (String field : userImpl.removeMap.keySet()) {
            List<Object> list = (List<Object>) userImpl.removeMap.get(field);

            if (!list.isEmpty()) {
                if (list.size() == 1) {
                    removeUpdates = removeUpdates.add(field, ((List) userImpl.removeMap.get(field)).get(0));
                } else {
                    removeAllUpdates = removeAllUpdates.add(field, userImpl.removeMap.get(field));
                }
            }
        }

        if (!removeUpdates.isEmpty()) {
            updateBuilder.add(PULL_OPERATOR, removeUpdates.get());
        }

        if (!removeAllUpdates.isEmpty()) {
            updateBuilder.add(PULL_ALL_OPERATOR, removeAllUpdates.get());
        }

        if (!updateBuilder.isEmpty()) {
            DBObject update = updateBuilder.get();

            _log.debug("Update query = {}", update);

            WriteResult writeResult = collection.update(criteria, update);

            String err = writeResult.getError();

            if (err != null) {
                throw new UpdateException(err);
            }
        }
    }

    return user;
}

From source file:org.obiba.magma.datasource.mongodb.converter.VariableConverter.java

License:Open Source License

public static DBObject marshall(Variable variable) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start() //
            .add("name", variable.getName()) //
            .add("valueType", variable.getValueType().getName()) //
            .add("entityType", variable.getEntityType()) //
            .add("mimeType", variable.getMimeType()) //
            .add("repeatable", variable.isRepeatable()) //
            .add("occurrenceGroup", variable.getOccurrenceGroup()) //
            .add("referencedEntityType", variable.getReferencedEntityType()) //
            .add("unit", variable.getUnit()).add("index", variable.getIndex());

    if (variable.hasCategories()) {
        Collection<Object> list = new BasicDBList();
        for (Category category : variable.getCategories()) {
            list.add(marshall(category));
        }/*  w ww .j a v  a 2 s.c om*/
        builder.add("categories", list);
    }

    if (variable.hasAttributes()) {
        Collection<Object> list = new BasicDBList();
        for (Attribute attribute : variable.getAttributes()) {
            list.add(marshall(attribute));
        }
        builder.add("attributes", list);
    }

    return builder.get();
}

From source file:org.obiba.magma.datasource.mongodb.converter.VariableConverter.java

License:Open Source License

private static DBObject marshall(Category category) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start() //
            .add("name", category.getName()).add("missing", category.isMissing());
    if (category.hasAttributes()) {
        Collection<Object> list = new BasicDBList();
        for (Attribute attribute : category.getAttributes()) {
            list.add(marshall(attribute));
        }//w ww. jav  a2  s . c o m
        builder.add("attributes", list);
    }
    return builder.get();
}