Search in sources :

Example 11 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 12 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)

Example 13 with DBCollection

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

the class MongoDBVMaintenance method pruneDanglingCommits.

@Override
public long pruneDanglingCommits(long expiry, TimeUnit unit) {
    long[] cids = findDanglingCommits(expiry, unit);
    DBCollection collCommits = _db.getDB().getCollection(MongoDBConstants.COLLECTION_COMMITS);
    //delete commits 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 < cids.length; i += sliceCount) {
        int maxSliceCount = Math.min(sliceCount, cids.length - i);
        long[] slice = new long[maxSliceCount];
        System.arraycopy(cids, i, slice, 0, maxSliceCount);
        collCommits.remove(new BasicDBObject(MongoDBConstants.ID, new BasicDBObject("$in", slice)));
    }
    return cids.length;
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject)

Example 14 with DBCollection

use of com.mongodb.DBCollection in project mongo-hadoop by mongodb.

the class GridFSInputFormat method getSplits.

@Override
public List<InputSplit> getSplits(final JobContext context) throws IOException, InterruptedException {
    Configuration conf = context.getConfiguration();
    DBCollection inputCollection = MongoConfigUtil.getInputCollection(conf);
    MongoClientURI inputURI = MongoConfigUtil.getInputURI(conf);
    GridFS gridFS = new GridFS(inputCollection.getDB(), inputCollection.getName());
    DBObject query = MongoConfigUtil.getQuery(conf);
    List<InputSplit> splits = new LinkedList<InputSplit>();
    for (GridFSDBFile file : gridFS.find(query)) {
        // One split per file.
        if (MongoConfigUtil.isGridFSWholeFileSplit(conf)) {
            splits.add(new GridFSSplit(inputURI, (ObjectId) file.getId(), (int) file.getChunkSize(), file.getLength()));
        } else // One split per file chunk.
        {
            for (int chunk = 0; chunk < file.numChunks(); ++chunk) {
                splits.add(new GridFSSplit(inputURI, (ObjectId) file.getId(), (int) file.getChunkSize(), file.getLength(), chunk));
            }
        }
    }
    LOG.debug("Found GridFS splits: " + splits);
    return splits;
}
Also used : DBCollection(com.mongodb.DBCollection) GridFSSplit(com.mongodb.hadoop.input.GridFSSplit) Configuration(org.apache.hadoop.conf.Configuration) ObjectId(org.bson.types.ObjectId) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) MongoClientURI(com.mongodb.MongoClientURI) GridFS(com.mongodb.gridfs.GridFS) DBObject(com.mongodb.DBObject) InputSplit(org.apache.hadoop.mapreduce.InputSplit) LinkedList(java.util.LinkedList)

Example 15 with DBCollection

use of com.mongodb.DBCollection in project mongo-hadoop by mongodb.

the class GridFSSplit method getGridFS.

private GridFS getGridFS() {
    if (null == gridFS) {
        DBCollection rootCollection = MongoConfigUtil.getCollection(inputURI);
        gridFS = new GridFS(rootCollection.getDB(), rootCollection.getName());
    }
    return gridFS;
}
Also used : DBCollection(com.mongodb.DBCollection) GridFS(com.mongodb.gridfs.GridFS)

Aggregations

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