Example usage for com.mongodb DBCursor size

List of usage examples for com.mongodb DBCursor size

Introduction

In this page you can find the example usage for com.mongodb DBCursor size.

Prototype

public int size() 

Source Link

Document

Counts the number of objects matching the query this does take limit/skip into consideration

Usage

From source file:nl.vu.psy.rite.persistence.mongo.MongoInfoStore.java

License:Open Source License

@Override
public String[] getClientInfoForRecipe(String recipeId) {
    BasicDBObject query = new BasicDBObject();
    query.put("clientinfo.recipeid", recipeId);
    DBCursor find = infoCollection.find(query);
    String[] result = new String[find.size()];
    int i = 0;/*  w  w w  . j  a  v a  2s  . co  m*/
    while (find.hasNext()) {
        result[i] = MongoInfoMapper.DBObjectToString(find.next());
        i++;
    }
    return result;
}

From source file:nl.vu.psy.rite.persistence.mongo.MongoRecipeStore.java

License:Open Source License

@Override
public Recipe getRecipe(String recipeId) {
    BasicDBObject bdo = new BasicDBObject();
    bdo.put("recipe", recipeId);
    DBCursor find = recipeCollection.find(bdo);
    if (find.size() == 1) {
        DBObject dbo = find.next();/*from w  w w. j a v a  2s  . c om*/
        return MongoRecipeMapper.DBObjectToRecipe(dbo);
    } else if (find.size() > 1) {
        throw new RiteRuntimeException(
                "The rite recipe collection is possibly corrupt, more than one entry was found for the identifier ["
                        + recipeId + "].");
    } else {
        return null;
    }
}

From source file:nl.vu.psy.rite.persistence.mongo.MongoRecipeStore.java

License:Open Source License

@Override
public Recipe[] getAllRecipes() {
    Recipe[] result = null;/*from  w w  w  .  j ava  2s .c o m*/
    DBCursor find = recipeCollection.find();
    result = new Recipe[find.size()];
    int i = 0;
    while (find.hasNext()) {
        DBObject dbo = find.next();
        result[i] = MongoRecipeMapper.DBObjectToRecipe(dbo);
        i++;
    }
    return result;
}

From source file:nl.vu.psy.rite.persistence.mongo.MongoRecipeStore.java

License:Open Source License

@Override
public Recipe[] getRecipesWithoutClients(int limit) {
    Recipe[] result = null;/*from www  .  j a  v  a  2 s. co  m*/
    DBObject query = new BasicDBObject();
    query.put("clientid", new BasicDBObject("$type", 10));
    DBCursor find = recipeCollection.find(query).limit(limit);
    ArrayList<Recipe> recipes = new ArrayList<Recipe>();
    result = new Recipe[find.size()];
    // int i = 0;
    while (find.hasNext()) {
        DBObject dbo = find.next();
        // result[i] = MongoRecipeMapper.DBObjectToRecipe(dbo);
        recipes.add(MongoRecipeMapper.DBObjectToRecipe(dbo));
        // i++;
    }
    return recipes.toArray(result);
}

From source file:nl.vu.psy.rite.persistence.mongo.MongoRecipeStore.java

License:Open Source License

@Override
public Recipe[] getUncompleteRecipesWithClients() {
    Recipe[] result = null;//  ww  w.  j  av a2 s. co m
    DBObject query = new BasicDBObject();
    query.put("clientid", new BasicDBObject("$type", 2));
    query.put("completed", false);
    DBCursor find = recipeCollection.find(query);
    result = new Recipe[find.size()];
    int i = 0;
    while (find.hasNext()) {
        DBObject dbo = find.next();
        result[i] = MongoRecipeMapper.DBObjectToRecipe(dbo);
        i++;
    }
    return result;
}

From source file:nl.vu.psy.rite.persistence.mongo.MongoRecipeStore.java

License:Open Source License

@Override
public Recipe[] getFailedRecipes() {
    Recipe[] result = null;/*w  w  w  .jav a2 s .c o  m*/
    DBObject query = new BasicDBObject();
    query.put("clientid", new BasicDBObject("$type", 2));
    query.put("completed", true);
    query.put("failed", true);
    DBCursor find = recipeCollection.find(query);
    result = new Recipe[find.size()];
    int i = 0;
    while (find.hasNext()) {
        DBObject dbo = find.next();
        result[i] = MongoRecipeMapper.DBObjectToRecipe(dbo);
        i++;
    }
    return result;
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

@Override
public List<EventRecord> getResults(String eventName, int skip, int limit) {
    DBObject queryObj = QueryBuilder.start().get();
    if (eventName != null) {
        queryObj.put(EventRecord.FIELD_EVENT_NAME, eventName);
    }/*from   w w  w  . ja  va 2 s.  c  om*/
    DBObject sortObj = BasicDBObjectBuilder.start().add(EventRecord.FIELD_START_TIME, Integer.valueOf(1)).get();

    DBCursor cursor = collection.find(queryObj);
    cursor.sort(sortObj);
    cursor.skip(skip);
    cursor.limit(limit);

    // Get all the results and convert them
    int size = cursor.size();
    List<EventRecord> results = new ArrayList<EventRecord>(size);
    try {
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            EventRecord eventRecord = convertToEventRecord(obj);
            results.add(eventRecord);
        }
    } finally {
        cursor.close();
    }
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug(
                "\n" + "Found results: \n" + "   Query:          " + queryObj + "\n" + "   Skip:           "
                        + skip + "\n" + "   Limit:          " + limit + "\n" + "   Results:        " + size);
    }
    return results;
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

@Override
public List<EventRecord> getResults(long startTime, long endTime, boolean chartOnly, int skip, int limit) {
    QueryBuilder queryBuilder = QueryBuilder.start().and(EventRecord.FIELD_START_TIME)
            .greaterThanEquals(new Date(startTime)).and(EventRecord.FIELD_START_TIME)
            .lessThan(new Date(endTime));
    if (chartOnly) {
        queryBuilder.and(EventRecord.FIELD_CHART).is(true);
    }/*from www .  j a v a 2s  . co  m*/
    DBObject queryObj = queryBuilder.get();
    DBObject sortObj = BasicDBObjectBuilder.start().add(EventRecord.FIELD_START_TIME, Integer.valueOf(1)).get();

    DBCursor cursor = collection.find(queryObj);
    cursor.sort(sortObj);
    cursor.skip(skip);
    cursor.limit(limit);

    // Get all the results and convert them
    int size = cursor.size();
    List<EventRecord> results = new ArrayList<EventRecord>(size);
    try {
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            EventRecord eventRecord = convertToEventRecord(obj);
            results.add(eventRecord);
        }
    } finally {
        cursor.close();
    }
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug(
                "\n" + "Found results: \n" + "   Query:          " + queryObj + "\n" + "   Skip:           "
                        + skip + "\n" + "   Limit:          " + limit + "\n" + "   Results:        " + size);
    }
    return results;
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

@Override
public List<EventDetails> getEventDetails(EventResultFilter filter, String filterEventName, int skip,
        int limit) {
    QueryBuilder queryBuilder = QueryBuilder.start();

    // apply filter
    switch (filter) {
    case Failed:/*  w w w.j  ava2 s .co m*/
        queryBuilder.and(EventRecord.FIELD_SUCCESS).is(false);
        break;

    case Success:
        queryBuilder.and(EventRecord.FIELD_SUCCESS).is(true);
        break;
    default:
        break;
    }

    //apply event name filter
    if (null != filterEventName && !filterEventName.isEmpty()) {
        queryBuilder.and(EventRecord.FIELD_EVENT_NAME).is(filterEventName);
    }

    DBObject queryObj = queryBuilder.get();
    // sort descending to get the newest values first
    DBObject sortObj = BasicDBObjectBuilder.start().add(EventRecord.FIELD_START_TIME, Integer.valueOf(-1))
            .get();
    DBCursor cursor = collection.find(queryObj);
    cursor.sort(sortObj);
    cursor.skip(skip);
    cursor.limit(limit);

    // Get all the results and convert them
    int size = cursor.size();
    List<EventDetails> results = new ArrayList<EventDetails>(size);
    try {
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            EventDetails eventDetails = convertToEventDetails(obj);
            results.add(eventDetails);
        }
    } finally {
        cursor.close();
    }

    return results;
}

From source file:org.alfresco.bm.file.FileDataServiceImpl.java

License:Open Source License

@Override
public FileData getRandomFile(String fileset) {
    long count = fileCount(fileset);
    if (count == 0L) {
        // There is nothing to choose from
        return null;
    }//from w w  w  . j  a v a  2 s. c  om
    // Use a random number from 0 (inclusive) to 'count' (exclusive)
    int skip = (int) (Math.random() * (double) count);

    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_FILESET, fileset).get();
    DBCursor results = collection.find(queryObj).skip(skip).limit(1);
    if (results.size() == 0) {
        // No results
        return null;
    } else {
        DBObject fileDataObj = results.next();
        return fromDBObject(fileDataObj);
    }
}