Example usage for com.mongodb BasicDBObjectBuilder start

List of usage examples for com.mongodb BasicDBObjectBuilder start

Introduction

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

Prototype

public static BasicDBObjectBuilder start() 

Source Link

Document

Creates a builder intialized with an empty document.

Usage

From source file:com.linuxbox.enkive.docstore.mongogrid.MongoGridDocStoreService.java

License:Open Source License

@Override
protected String nextUnindexedByShardKey(int shardKeyLow, int shardKeyHigh) {
    final DBObject query = createUnindexedQuery(shardKeyLow, shardKeyHigh);
    final DBObject updateSet = BasicDBObjectBuilder.start().add(INDEX_STATUS_QUERY, STATUS_INDEXING)
            .add(INDEX_TIMESTAMP_QUERY, new Date()).get();
    final BasicDBObject update = new BasicDBObject("$set", updateSet);

    // some constants to make the call to findAndModify more readable;
    // compiler please optimize them away!
    final boolean doNotRemove = false;
    final boolean returnNewVersion = true;
    final boolean doNotUpsert = false;

    DBObject result = filesCollection.findAndModify(query, RETRIEVE_OBJECT_ID_AND_FILENAME,
            SORT_BY_INDEX_TIMESTAMP, doNotRemove, update, returnNewVersion, doNotUpsert);

    if (result != null) {
        return (String) result.get(FILENAME_KEY);
    } else {// w  ww  .ja v a2  s .  c  o m
        return null;
    }
}

From source file:com.linuxbox.enkive.docstore.mongogrid.MongoGridDocStoreService.java

License:Open Source License

/**
 * Change the file name of GridFS file with a given id to newName.
 * /*www.  j  a  v  a 2  s . c  o m*/
 * @param id
 * @param newName
 * @return true if a file was modified, false otherwise
 */
boolean setFileNameAndShardKey(Object id, String newName, int shardKey) {
    DBObject query = new BasicDBObject(OBJECT_ID_KEY, id);
    DBObject updateItems = BasicDBObjectBuilder.start().add(Constants.FILENAME_KEY, newName)
            .add(Constants.INDEX_SHARD_QUERY, shardKey).get();
    DBObject update = new BasicDBObject("$set", updateItems);
    DBObject fields = new BasicDBObject(Constants.FILENAME_KEY, 1);
    final boolean returnOldVersion = false;
    final DBObject doNotSort = null;
    final boolean doNotRemove = false;
    final boolean doNotUpsert = false;
    DBObject result = filesCollection.findAndModify(query, fields, doNotSort, doNotRemove, update,
            returnOldVersion, doNotUpsert);
    return result != null;
}

From source file:com.linuxbox.enkive.docstore.mongogrid.MongoGridDocStoreService.java

License:Open Source License

void markAsIndexedHelper(String identifier, int status) throws DocStoreException {
    final DBObject identifierQuery = new QueryBuilder().and(FILENAME_KEY).is(identifier).get();

    final DBObject updateSet = BasicDBObjectBuilder.start().add(INDEX_STATUS_QUERY, status)
            .add(INDEX_TIMESTAMP_QUERY, new Date()).get();
    final BasicDBObject update = new BasicDBObject("$set", updateSet);

    final boolean doNotRemove = false;
    final boolean returnNewVersion = true;
    final boolean doNotUpsert = false;
    final DBObject doNotSort = null;

    DBObject result = filesCollection.findAndModify(identifierQuery, RETRIEVE_OBJECT_ID, doNotSort, doNotRemove,
            update, returnNewVersion, doNotUpsert);
    if (result == null) {
        throw new DocStoreException(
                "could not mark document '" + identifier + "' with indexing status of " + status + ".");
    } else {// ww  w.  java 2s .c o m
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(
                    "Marked document " + result.get(OBJECT_ID_KEY) + " with index status of " + status + ".");
        }
    }
}

From source file:com.linuxbox.enkive.docstore.mongogrid.MongoGridDocStoreService.java

License:Open Source License

@Override
public List<IndexDescription> getPreferredIndexes() {
    List<IndexDescription> result = new LinkedList<IndexDescription>();

    /*// ww  w.j  a  v  a  2s.  c o m
     * NOTE: we DO NOT NEED a filename index, because by default GridFS will
     * create an index on filename and upload date. You can efficiently
     * query on compound indexes if the key searched for come before those
     * that are not, which is true in this case.
     * 
     * DBObject filenameIndex = BasicDBObjectBuilder.start()
     * .add(FILENAME_KEY, 1).get();
     * filesCollection.ensureIndex(filenameIndex);
     */

    // be sure to put status before timestamp, because it's more likely
    // we'll search on just status rather than on just timestamp
    DBObject searchIndexingIndex = BasicDBObjectBuilder.start().add(INDEX_STATUS_KEY, 1)
            .add(INDEX_TIMESTAMP_KEY, 1).get();
    IndexDescription id1 = new IndexDescription("indexingStatusIndex", searchIndexingIndex, false);
    result.add(id1);

    return result;
}

From source file:com.linuxbox.enkive.tool.mongodb.MongoDbIndexManager.java

License:Open Source License

/**
 * Run the MongoDB ensure_index function on the service provided, creating
 * the index provided. Can be ensured in the foreground or background
 * depending on value of background./*from  w w w . j av a  2s  .co m*/
 */
void ensureIndex(MongoIndexable service, IndexDescription desiredIndex, boolean inBackground)
        throws MongoException {
    final DBObject options = BasicDBObjectBuilder.start().add("name", desiredIndex.getName())
            .add("unique", desiredIndex.isUnique()).add("background", inBackground).get();
    service.ensureIndex(desiredIndex.getDescription(), options);
}

From source file:com.linuxbox.util.lockservice.mongodb.MongoLockService.java

License:Open Source License

@Override
public List<IndexDescription> getPreferredIndexes() {
    List<IndexDescription> result = new LinkedList<IndexDescription>();

    /*//w w  w  . ja  v a2 s  .c  o m
     * We want the identifier index to be unique, as that's how we
     * atomically detect when someone tries to create an already-existing
     * lock record
     */
    DBObject lockIndex = BasicDBObjectBuilder.start().add(LOCK_IDENTIFIER_KEY, 1).get();
    IndexDescription id1 = new IndexDescription("lockIndex", lockIndex, true);
    result.add(id1);

    return result;
}

From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java

License:Apache License

@Override
public List<DBObject> findAll(String entityName, String projection, Pageable page) {
    if (page == null) {
        page = new PageRequest(0, Integer.MAX_VALUE);
    }//from w  ww . ja  v a 2  s  .  com

    DBCursor cursor = null;
    if (StringUtils.isEmpty(projection)) {
        cursor = getCollection(entityName).find(new BasicDBObject());
    } else {
        String[] properties = projection.split(",");
        BasicDBObjectBuilder projectionBuilder = BasicDBObjectBuilder.start();
        boolean idWanted = false;
        for (String property : properties) {
            property = property.trim();
            if (!StringUtils.isEmpty(property)) {
                if (property.equals(EntityUtils.ID)) {
                    idWanted = true;
                }

                projectionBuilder.add(property.trim(), true);
            }
        }

        if (idWanted == false) {
            projectionBuilder.append("_id", false);
        }

        cursor = getCollection(entityName).find(new BasicDBObject(), projectionBuilder.get())
                .sort(new BasicDBObject(projection, 1));
    }

    if (page.getSort() != null) {
        cursor = cursor.sort(createSort(page));
    } else if (projection != null) {
        cursor = cursor.sort(new BasicDBObject(projection, 1));
    }

    cursor = cursor.skip(page.getOffset()).limit(page.getPageSize());

    List<DBObject> result = cursor.toArray();
    return result;
}

From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java

License:Apache License

/**
 * @param page//from w  ww.  j a  v a  2 s  . c om
 * @return sort DBObject
 */
protected DBObject createSort(Pageable page) {
    if (page.getSort() == null) {
        return null;
    }

    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
    Iterator<Order> orderIter = page.getSort().iterator();
    while (orderIter.hasNext()) {
        Order order = orderIter.next();
        builder.add(order.getProperty(), order.getDirection().equals(Direction.ASC) ? 1 : -1);
    }

    return builder.get();
}

From source file:com.mobileman.kuravis.core.services.user.impl.UserServiceImpl.java

License:Apache License

/** 
 * {@inheritDoc}//  w  w  w.j a va  2  s.com
 * @see com.mobileman.kuravis.core.services.user.UserService#findUsersByDiseaseAndTreatment(com.mongodb.DBObject, Pageable)
 */
@Override
public List<DBObject> findUsersByDiseaseAndTreatment(DBObject query, Pageable page) {

    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();

    if (query != null) {
        if (query.containsField("diseaseId")) {
            builder.add("disease." + EntityUtils.ID, query.get("diseaseId"));
        }

        if (query.containsField("treatmentId")) {
            builder.add("treatment." + EntityUtils.ID, query.get("treatmentId"));
        }
    }

    AggregationOutput out = getCollection(TreatmentReview.ENTITY_NAME).aggregate(
            new BasicDBObject("$match", builder.get()),
            new BasicDBObject("$group", new BasicDBObject("_id", new BasicDBObject("_id", "$author._id"))),
            new BasicDBObject("$project", new BasicDBObject(EntityUtils.ID, "$_id._id")),
            new BasicDBObject("$skip", page.getOffset()), new BasicDBObject("$limit", page.getPageSize())

    );

    List<DBObject> result = new ArrayList<>();
    Set<String> userIds = new HashSet<String>();
    for (DBObject dbObject : out.results()) {
        String userId = EntityUtils.getEntityId(dbObject);
        userIds.add(userId);
        result.add(dbObject);
    }

    Map<String, DBObject> usersData = findUsersData(userIds, "name", "gender", "settings.profile");
    for (DBObject user : result) {
        DBObject data = usersData.get(EntityUtils.getEntityId(user));
        if (data != null) {
            user.put("name", data.get("name"));
            user.put("gender", data.get("gender"));
            user.put("settings", data.get("settings"));
        }
    }

    return result;
}

From source file:com.mobileman.kuravis.core.util.ErrorUtils.java

License:Apache License

/**
 * @param message error message//from  ww w.j  a v a2 s.  c o  m
 * @param code 
 * @return json error result with given message
 */
public static DBObject error(String message, ErrorCodes code) {
    return BasicDBObjectBuilder.start().add(EntityUtils.ATTR_RESULT, ErrorUtils.ERROR)
            .add(ATTR_MESSAGE, message).add(ATTR_CODE, code.getValue()).add(ATTR_STATUS, code.getCode()).get();
}