Search in sources :

Example 36 with DBCollection

use of com.mongodb.DBCollection in project morphia by mongodb.

the class TestQuery method testCommentsShowUpInLogs.

@Test
public void testCommentsShowUpInLogs() {
    getDs().save(asList(new Pic("pic1"), new Pic("pic2"), new Pic("pic3"), new Pic("pic4")));
    getDb().command(new BasicDBObject("profile", 2));
    String expectedComment = "test comment";
    getDs().find(Pic.class).asList(new FindOptions().modifier("$comment", expectedComment));
    DBCollection profileCollection = getDb().getCollection("system.profile");
    assertNotEquals(0, profileCollection.count());
    DBObject profileRecord = profileCollection.findOne(new BasicDBObject("op", "query").append("ns", getDs().getCollection(Pic.class).getFullName()));
    final Object commentPre32 = ((DBObject) profileRecord.get("query")).get("$comment");
    final Object commentPost32 = ((DBObject) profileRecord.get("query")).get("comment");
    assertTrue(profileRecord.toString(), expectedComment.equals(commentPre32) || expectedComment.equals(commentPost32));
    turnOffProfilingAndDropProfileCollection();
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UsesCustomIdObject(org.mongodb.morphia.TestMapper.UsesCustomIdObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 37 with DBCollection

use of com.mongodb.DBCollection in project mongomvcc by igd-geo.

the class MongoDBVDatabaseBenchmark method plainOldInsertDelete.

/**
	 * Inserts a lot of documents using plain old MongoDB and then deletes them
	 */
@Test
@BenchmarkOptions(benchmarkRounds = 2, warmupRounds = 1)
public void plainOldInsertDelete() {
    plainOldInsert();
    DBCollection coll = _db.getCollection("persons");
    for (long i = 0; i < DOCUMENTS; ++i) {
        coll.remove(new BasicDBObject("_id", 1 + i));
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test) BenchmarkOptions(com.carrotsearch.junitbenchmarks.BenchmarkOptions)

Example 38 with DBCollection

use of com.mongodb.DBCollection in project mongomvcc by igd-geo.

the class MongoDBVDatabaseBenchmark method plainOldInsertDeleteInsertQuery.

/**
	 * Queries a lot of objects after inserting, deleting and inserting again
	 */
@Test
@BenchmarkOptions(benchmarkRounds = 2, warmupRounds = 1)
public void plainOldInsertDeleteInsertQuery() {
    plainOldInsertDeleteInsert();
    _master.commit();
    DBCollection coll = _db.getCollection("persons");
    for (DBObject o : coll.find()) {
        assertTrue(((Long) o.get("age")).longValue() >= DOCUMENTS);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Test(org.junit.Test) BenchmarkOptions(com.carrotsearch.junitbenchmarks.BenchmarkOptions)

Example 39 with DBCollection

use of com.mongodb.DBCollection in project mongomvcc by igd-geo.

the class MongoDBVMaintenance method doFindDanglingCommits.

private long[] doFindDanglingCommits(long expiry, TimeUnit unit) {
    long maxTime = getMaxTime(expiry, unit);
    //load all commits which are older than the expiry time. mark them as dangling
    DBCollection collCommits = _db.getDB().getCollection(MongoDBConstants.COLLECTION_COMMITS);
    DBCursor commits = collCommits.find(new BasicDBObject(MongoDBConstants.TIMESTAMP, //also include commits without a timestamp
    new BasicDBObject("$not", new BasicDBObject("$gte", maxTime))), new BasicDBObject(MongoDBConstants.ID, 1));
    IdSet danglingCommits = new IdHashSet();
    for (DBObject o : commits) {
        long cid = (Long) o.get(MongoDBConstants.ID);
        danglingCommits.add(cid);
    }
    //walk through all branches and eliminate commits which are not dangling
    DBCollection collBranches = _db.getDB().getCollection(MongoDBConstants.COLLECTION_BRANCHES);
    DBCursor branches = collBranches.find(new BasicDBObject(), new BasicDBObject(MongoDBConstants.CID, 1));
    VHistory history = _db.getHistory();
    IdSet alreadyCheckedCommits = new IdHashSet();
    for (DBObject o : branches) {
        long cid = (Long) o.get(MongoDBConstants.CID);
        while (cid != 0) {
            if (alreadyCheckedCommits.contains(cid)) {
                break;
            }
            alreadyCheckedCommits.add(cid);
            danglingCommits.remove(cid);
            cid = history.getParent(cid);
        }
    }
    //all remaining commits must be dangling
    return danglingCommits.toArray();
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) VHistory(de.fhg.igd.mongomvcc.VHistory) IdSet(de.fhg.igd.mongomvcc.helper.IdSet) IdHashSet(de.fhg.igd.mongomvcc.helper.IdHashSet) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 40 with DBCollection

use of com.mongodb.DBCollection in project mongomvcc by igd-geo.

the class MongoDBVMaintenance method pruneUnreferencedDocuments.

@Override
public long pruneUnreferencedDocuments(String collection, long expiry, TimeUnit unit) {
    long[] oids = findUnreferencedDocuments(collection, expiry, unit);
    DBCollection coll = _db.getDB().getCollection(collection);
    //delete documents in chunks, so we avoid sending an array that is
    //larger than the maximum document size
    final int sliceCount = 1000;
    for (int i = 0; i < oids.length; i += sliceCount) {
        int maxSliceCount = Math.min(sliceCount, oids.length - i);
        long[] slice = new long[maxSliceCount];
        System.arraycopy(oids, i, slice, 0, maxSliceCount);
        coll.remove(new BasicDBObject(MongoDBConstants.ID, new BasicDBObject("$in", slice)));
    }
    return oids.length;
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject)

Aggregations

DBCollection (com.mongodb.DBCollection)174 DBObject (com.mongodb.DBObject)92 BasicDBObject (com.mongodb.BasicDBObject)91 Test (org.junit.Test)70 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)29 DBCursor (com.mongodb.DBCursor)25 DB (com.mongodb.DB)23 MongoException (com.mongodb.MongoException)22 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)17 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 JSONObject (org.json.JSONObject)12 MongoClientURI (com.mongodb.MongoClientURI)11 List (java.util.List)11 Map (java.util.Map)11 QueryBuilder (com.mongodb.QueryBuilder)10 HashMap (java.util.HashMap)10 Stopwatch (com.google.common.base.Stopwatch)9 WriteResult (com.mongodb.WriteResult)9 MongoClient (com.mongodb.MongoClient)8 IOException (java.io.IOException)8