use of com.mongodb.BasicDBObjectBuilder in project morphia by mongodb.
the class DatastoreImpl method ensureCaps.
@Override
public void ensureCaps() {
for (final MappedClass mc : mapper.getMappedClasses()) {
if (mc.getEntityAnnotation() != null && mc.getEntityAnnotation().cap().value() > 0) {
final CappedAt cap = mc.getEntityAnnotation().cap();
final String collName = mapper.getCollectionName(mc.getClazz());
final BasicDBObjectBuilder dbCapOpts = start("capped", true);
if (cap.value() > 0) {
dbCapOpts.add("size", cap.value());
}
if (cap.count() > 0) {
dbCapOpts.add("max", cap.count());
}
final DB database = getDB();
if (database.getCollectionNames().contains(collName)) {
final DBObject dbResult = database.command(start("collstats", collName).get());
if (dbResult.containsField("capped")) {
LOG.debug("DBCollection already exists and is capped already; doing nothing. " + dbResult);
} else {
LOG.warning("DBCollection already exists with same name(" + collName + ") and is not capped; not creating capped version!");
}
} else {
getDB().createCollection(collName, dbCapOpts.get());
LOG.debug("Created capped DBCollection (" + collName + ") with opts " + dbCapOpts);
}
}
}
}
use of com.mongodb.BasicDBObjectBuilder in project morphia by mongodb.
the class GeoFieldCriteria method addTo.
@Override
public void addTo(final DBObject obj) {
final BasicDBObjectBuilder query;
switch(getOperator()) {
case NEAR:
query = BasicDBObjectBuilder.start(FilterOperator.NEAR.val(), getValue());
break;
case NEAR_SPHERE:
query = BasicDBObjectBuilder.start(FilterOperator.NEAR_SPHERE.val(), getValue());
break;
case WITHIN_BOX:
query = BasicDBObjectBuilder.start().push(FilterOperator.GEO_WITHIN.val()).add(getOperator().val(), getValue());
break;
case WITHIN_CIRCLE:
query = BasicDBObjectBuilder.start().push(FilterOperator.GEO_WITHIN.val()).add(getOperator().val(), getValue());
break;
case WITHIN_CIRCLE_SPHERE:
query = BasicDBObjectBuilder.start().push(FilterOperator.GEO_WITHIN.val()).add(getOperator().val(), getValue());
break;
default:
throw new UnsupportedOperationException(getOperator() + " not supported for geo-query");
}
//add options...
if (opts != null) {
for (final Map.Entry<String, Object> e : opts.entrySet()) {
query.append(e.getKey(), e.getValue());
}
}
obj.put(getField(), query.get());
}
use of com.mongodb.BasicDBObjectBuilder in project incubator-rya by apache.
the class MongoDetailsAdapter method toDBObject.
static DBObject toDBObject(final PCJDetails pcjDetails) {
requireNonNull(pcjDetails);
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();
}
use of com.mongodb.BasicDBObjectBuilder in project incubator-rya by apache.
the class MongoDetailsAdapter method toDBObject.
/**
* Converts a {@link RyaDetails} object into its MongoDB {@link DBObject} equivalent.
*
* @param details - The details to convert. (not null)
* @return The MongoDB {@link DBObject} equivalent.
*/
public static BasicDBObject toDBObject(final RyaDetails details) {
requireNonNull(details);
final BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add(INSTANCE_KEY, details.getRyaInstanceName()).add(VERSION_KEY, details.getRyaVersion()).add(ENTITY_DETAILS_KEY, details.getEntityCentricIndexDetails().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());
}
// If the Rya Streams Details are present, then add them.
if (details.getRyaStreamsDetails().isPresent()) {
final RyaStreamsDetails ryaStreamsDetails = details.getRyaStreamsDetails().get();
// The embedded object that holds onto the fields.
final DBObject ryaStreamsFields = BasicDBObjectBuilder.start().add(RYA_STREAMS_HOSTNAME_KEY, ryaStreamsDetails.getHostname()).add(RYA_STREAMS_PORT_KEY, ryaStreamsDetails.getPort()).get();
// Add them to the main builder.
builder.add(RYA_STREAMS_DETAILS_KEY, ryaStreamsFields);
}
return (BasicDBObject) builder.get();
}
use of com.mongodb.BasicDBObjectBuilder in project incubator-rya by apache.
the class TemporalMongoDBStorageStrategy method getTimeValue.
public DBObject getTimeValue(final String timeData) {
final Matcher match = TemporalInstantRfc3339.PATTERN.matcher(timeData);
final BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
if (match.find()) {
final TemporalInterval date = TemporalInstantRfc3339.parseInterval(timeData);
builder.add(INTERVAL_START, date.getHasBeginning().getAsDateTime().toDate());
builder.add(INTERVAL_END, date.getHasEnd().getAsDateTime().toDate());
} else {
builder.add(INSTANT, TemporalInstantRfc3339.FORMATTER.parseDateTime(timeData).toDate());
}
return builder.get();
}
Aggregations