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:org.alfresco.bm.file.FileDataServiceImpl.java

License:Open Source License

@Override
public FileData getRandomFile(String fileset, String extension) {
    long count = fileCount(fileset, extension);
    if (count == 0L) {
        // There is nothing to choose from
        return null;
    }/*from   w  w w  . j av  a  2  s .  c o  m*/
    // 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).add(FIELD_EXTENSION, extension)
            .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);
    }
}

From source file:org.alfresco.bm.log.MongoLogService.java

License:Open Source License

/**
 * Construct an instance providing the DB and collection name to use
 * /*from  ww  w .  j  a va  2 s .co  m*/
 * @param db            the database to use
 * @param size          the size (bytes) to cap the log size at or 0 to ignore.
 *                      This must be zero if the TTL is set.
 * @param max           the maximum number of log entries or 0 to ignore.
 *                      This must be zero if the TTL is set.
 * @param ttl           the time to live (seconds) of a log message or 0 to ignore.
 *                      This must be zero or less if the logs are capped by size or max entries.
 */
public MongoLogService(DB db, int size, int max, int ttl) {
    try {
        BasicDBObjectBuilder optionsBuilder = BasicDBObjectBuilder.start();
        if (size > 0L) {
            optionsBuilder.add("capped", true);
            optionsBuilder.add("size", size);
            if (max > 0L) {
                optionsBuilder.add("max", max);
            }
            if (ttl > 0) {
                throw new IllegalArgumentException(
                        "The log collection can only be capped by size, max entries or time to live.");
            }
        } else if (max > 0L) {
            throw new IllegalArgumentException(
                    "The logs must always be capped by size before capping by number.");
        }
        DBObject options = optionsBuilder.get();
        this.collection = db.createCollection(COLLECTION_LOGS, options);
    } catch (MongoException ex) {
        if (!db.getCollectionNames().contains(COLLECTION_LOGS)) {
            throw ex;
        }
        this.collection = db.getCollection(COLLECTION_LOGS);
    }
    this.ttl = ttl;
}

From source file:org.alfresco.bm.log.MongoLogService.java

License:Open Source License

/**
 * Ensure that the MongoDB collection has the required indexes
 */// w w  w.ja  va  2 s  .  c o m
private void checkIndexes() {
    // Ensure ordering and TTL
    DBObject idxTime = BasicDBObjectBuilder.start().add(FIELD_TIME, -1).get();
    DBObject optTime = BasicDBObjectBuilder.start().add("unique", Boolean.FALSE).get();
    if (ttl > 0) {
        optTime.put("expireAfterSeconds", ttl);
    }
    collection.createIndex(idxTime, optTime);

    // Select by driver, order by time
    DBObject idxDriverTime = BasicDBObjectBuilder.start().add(FIELD_DRIVER_ID, 1).add(FIELD_TIME, -1).get();
    DBObject optDriverTime = BasicDBObjectBuilder.start().add("unique", Boolean.FALSE).get();
    collection.createIndex(idxDriverTime, optDriverTime);

    // Select by test, order by time
    DBObject idxTestTime = BasicDBObjectBuilder.start().add(FIELD_TEST, 1).add(FIELD_TIME, -1).get();
    DBObject optTestTime = BasicDBObjectBuilder.start().add("unique", Boolean.FALSE).get();
    collection.createIndex(idxTestTime, optTestTime);

    // Select by test run, order by time
    DBObject idxTestRunTime = BasicDBObjectBuilder.start().add(FIELD_TEST_RUN, 1).add(FIELD_TIME, -1).get();
    DBObject optTestRunTime = BasicDBObjectBuilder.start().add("unique", Boolean.FALSE).get();
    collection.createIndex(idxTestRunTime, optTestRunTime);
}

From source file:org.alfresco.bm.log.MongoLogService.java

License:Open Source License

@Override
public void log(String driverId, String test, String testRun, LogLevel level, String msg) {
    BasicDBObjectBuilder insertObjBuilder = BasicDBObjectBuilder.start().add(FIELD_TIME, new Date())
            .add(FIELD_LEVEL, level.getLevel()).add(FIELD_MSG, msg);
    if (driverId != null) {
        insertObjBuilder.add(FIELD_DRIVER_ID, driverId);
    }/*from w  ww.j  a va 2s. co m*/
    if (test != null) {
        insertObjBuilder.add(FIELD_TEST, test);
    }
    if (testRun != null) {
        insertObjBuilder.add(FIELD_TEST_RUN, testRun);
    }
    DBObject insertObj = insertObjBuilder.get();

    collection.insert(insertObj);
}

From source file:org.alfresco.bm.log.MongoLogService.java

License:Open Source License

@Override
public DBCursor getLogs(String driverId, String test, String testRun, LogLevel level, Long minTime,
        Long maxTime, int skip, int limit) {
    BasicDBObjectBuilder queryObjBuilder = BasicDBObjectBuilder.start();
    if (level != null) {
        queryObjBuilder.push(FIELD_LEVEL).add("$gte", level.getLevel()).pop();
    }//  w  w w .jav a2s  .  co  m
    if (driverId != null) {
        queryObjBuilder.add(FIELD_DRIVER_ID, driverId);
    }
    if (test != null) {
        queryObjBuilder.add(FIELD_TEST, test);
    }
    if (testRun != null) {
        queryObjBuilder.add(FIELD_TEST_RUN, testRun);
    }
    if (minTime != null || maxTime != null) {
        queryObjBuilder.push(FIELD_TIME);
        if (minTime != null) {
            queryObjBuilder.add("$gte", new Date(minTime));
        }
        if (maxTime != null) {
            queryObjBuilder.add("$lt", new Date(maxTime));
        }
        queryObjBuilder.pop();
    }
    DBObject queryObj = queryObjBuilder.get();
    DBObject sortObj = new BasicDBObject(FIELD_TIME, -1);
    DBObject fieldsObj = BasicDBObjectBuilder.start().add(FIELD_ID, false).add(FIELD_TIME, true)
            .add(FIELD_DRIVER_ID, true).add(FIELD_TEST, true).add(FIELD_TEST_RUN, true).add(FIELD_LEVEL, true)
            .add(FIELD_MSG, true).get();
    return collection.find(queryObj, fieldsObj).sort(sortObj).skip(skip).limit(limit);
}

From source file:org.alfresco.bm.session.MongoSessionService.java

License:Open Source License

@Override
protected String newSession(SessionData sessionData) {
    ObjectId id = new ObjectId();
    DBObject insertObj = BasicDBObjectBuilder.start().add(FIELD_ID, id)
            .add(FIELD_START_TIME, sessionData.getStartTime()).add(FIELD_END_TIME, sessionData.getEndTime())
            .add(FIELD_DATA, sessionData.getData()).get();
    try {/*w w  w  .j a  v  a 2  s .  co m*/
        collection.insert(insertObj);
        return id.toString();
    } catch (MongoException e) {
        throw new RuntimeException("Failed to write new session data: \n" + "   Session: " + sessionData, e);
    }
}

From source file:org.alfresco.bm.session.MongoSessionService.java

License:Open Source License

@Override
protected SessionData findSessionData(String sessionId) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_ID, new ObjectId(sessionId)).get();
    DBObject result = collection.findOne(queryObj);
    if (result == null) {
        return null;
    } else {//from ww w.j  av  a  2  s  . c  o m
        return fromDBObject(result);
    }
}

From source file:org.alfresco.bm.session.MongoSessionService.java

License:Open Source License

@Override
protected void updateSessionEndTime(String sessionId, long endTime) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_ID, new ObjectId(sessionId)).get();
    DBObject updateObj = BasicDBObjectBuilder.start().push("$set").add(FIELD_END_TIME, endTime).pop().get();
    try {//from ww w .  j  a va 2  s  .c  o  m
        collection.update(queryObj, updateObj);
    } catch (MongoException e) {
        throw new RuntimeException("Failed to update session end time: \n" + "   Session:      " + sessionId
                + "\n" + "   End Time:     " + endTime, e);
    }
}

From source file:org.alfresco.bm.session.MongoSessionService.java

License:Open Source License

@Override
protected boolean updateSessionData(String sessionId, DBObject data) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_ID, new ObjectId(sessionId)).get();
    DBObject updateObj = BasicDBObjectBuilder.start().push("$set").add(FIELD_DATA, data).pop().get();
    try {//from   ww  w .  j  a  va 2 s  .  co m
        WriteResult wr = collection.update(queryObj, updateObj);
        return wr.getN() > 0;
    } catch (MongoException e) {
        throw new RuntimeException("Failed to update session data: \n" + "   Session:      " + sessionId + "\n"
                + "   Data:         " + data, e);
    }
}

From source file:org.alfresco.bm.session.MongoSessionService.java

License:Open Source License

@Override
public long getActiveSessionsCount() {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_END_TIME, -1L).get();
    return collection.count(queryObj);
}