Search in sources :

Example 76 with DBCursor

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

the class MongoDBVMaintenanceTest method pruneUnreferencedDocuments.

/**
 * Tests if unreferenced documents can be deleted
 * @throws Exception if something goes wrong
 */
@Test
public void pruneUnreferencedDocuments() throws Exception {
    Object[] ud = makeUnreferencedDocuments();
    long[] unreferenced = (long[]) ud[0];
    long count = _db.getMaintenance().pruneUnreferencedDocuments("persons", 0, TimeUnit.MILLISECONDS);
    assertEquals(3, count);
    Mongo mongo = new Mongo();
    DB db = mongo.getDB("mvcctest");
    DBCollection personsColl = db.getCollection("persons");
    for (int i = 0; i < 3; ++i) {
        DBCursor cursor = personsColl.find(new BasicDBObject(MongoDBConstants.ID, unreferenced[i]));
        assertEquals(0, cursor.size());
    }
    assertEquals(3, personsColl.find().size());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) Mongo(com.mongodb.Mongo) BasicDBObject(com.mongodb.BasicDBObject) DB(com.mongodb.DB) Test(org.junit.Test)

Example 77 with DBCursor

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

the class MongoDBVCollection method findOne.

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> findOne(Map<String, Object> example) {
    DBObject o = new BasicDBObject();
    o.putAll(_branch.getQueryObject());
    o.putAll(example);
    OIDInIndexFilter filter = new OIDInIndexFilter();
    DBCursor c = _delegate.find(o);
    for (DBObject obj : c) {
        if (filter.filter(obj)) {
            if (obj instanceof Map) {
                return (Map<String, Object>) obj;
            }
            return obj.toMap();
        }
    }
    return null;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) IdMap(de.fhg.igd.mongomvcc.helper.IdMap) Map(java.util.Map)

Example 78 with DBCursor

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

the class MongoDBVMaintenance method doFindUnreferencedDocuments.

private long[] doFindUnreferencedDocuments(String collection, long expiry, TimeUnit unit) {
    long maxTime = getMaxTime(expiry, unit);
    // fetch the OIDs of all documents older than the expiry time
    DBCollection collDocs = _db.getDB().getCollection(collection);
    DBCursor docs = collDocs.find(new BasicDBObject(MongoDBConstants.TIMESTAMP, // also include docs without a timestamp
    new BasicDBObject("$not", new BasicDBObject("$gte", maxTime))), new BasicDBObject(MongoDBConstants.ID, 1));
    IdSet oids = new IdHashSet(docs.count());
    for (DBObject o : docs) {
        oids.add((Long) o.get(MongoDBConstants.ID));
    }
    // iterate through all commits and eliminate referenced documents
    DBCollection collCommits = _db.getDB().getCollection(MongoDBConstants.COLLECTION_COMMITS);
    for (DBObject o : collCommits.find()) {
        Commit c = Tree.deserializeCommit(o);
        Map<String, IdMap> allObjs = c.getObjects();
        IdMap objs = allObjs.get(collection);
        if (objs != null) {
            // eliminate OIDs referenced by this commit
            IdMapIterator mi = objs.iterator();
            while (mi.hasNext()) {
                mi.advance();
                oids.remove(mi.value());
            }
        }
    }
    // the remaining OIDs must be the unreferenced ones
    return oids.toArray();
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) IdMap(de.fhg.igd.mongomvcc.helper.IdMap) Commit(de.fhg.igd.mongomvcc.impl.internal.Commit) IdSet(de.fhg.igd.mongomvcc.helper.IdSet) IdMapIterator(de.fhg.igd.mongomvcc.helper.IdMapIterator) IdHashSet(de.fhg.igd.mongomvcc.helper.IdHashSet) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 79 with DBCursor

use of com.mongodb.DBCursor in project tutorials by eugenp.

the class AppIntegrationTest method testAddressPersistance.

@Test
public void testAddressPersistance() {
    BasicDBObject contact = new BasicDBObject();
    contact.put("name", "John");
    contact.put("company", "Baeldung");
    // Inserting document
    collection.insert(contact);
    DBCursor cursorDoc = collection.find();
    BasicDBObject contact1 = new BasicDBObject();
    while (cursorDoc.hasNext()) {
        contact1 = (BasicDBObject) cursorDoc.next();
        System.out.println(contact1);
    }
    assertEquals(contact1.get("name"), "John");
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) Test(org.junit.Test)

Example 80 with DBCursor

use of com.mongodb.DBCursor in project records-management by Alfresco.

the class RecordService method getRecordsInPaths.

/**
 * Helper to return a list or records with specified execution state that are in specified parent paths. If listOfParentPaths is null or empty then all records with
 * specified execution state will be returned. Supports pagination.
 *
 * @param state - record execution state to search for
 * @param listOfParentPaths - list of parent paths to search in
 * @param skip - the number of entries to skip
 * @param limit - the number of entries to return
 * @return to return a list of records with specified execution state that are in specified parent paths. If listOfParentPaths is null or empty then all records with
 * specified execution state will be returned.
 */
public List<RecordData> getRecordsInPaths(String state, List<String> listOfParentPaths, int skip, int limit) {
    if (state == null) {
        throw new IllegalArgumentException();
    }
    QueryBuilder queryObjBuilder = QueryBuilder.start();
    queryObjBuilder.and(FIELD_STATE).is(state);
    if (listOfParentPaths != null && listOfParentPaths.size() > 0) {
        queryObjBuilder.and(FIELD_PARENT_PATH).in(listOfParentPaths);
    }
    DBObject queryObj = queryObjBuilder.get();
    DBCursor cursor = collection.find(queryObj).skip(skip).limit(limit);
    List<RecordData> results = fromDBCursor(cursor);
    return results;
}
Also used : DBCursor(com.mongodb.DBCursor) QueryBuilder(com.mongodb.QueryBuilder) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Aggregations

DBCursor (com.mongodb.DBCursor)87 BasicDBObject (com.mongodb.BasicDBObject)70 DBObject (com.mongodb.DBObject)54 DBCollection (com.mongodb.DBCollection)42 ArrayList (java.util.ArrayList)20 JSONObject (org.json.JSONObject)15 MongoException (com.mongodb.MongoException)11 DB (com.mongodb.DB)10 Test (org.junit.Test)9 BasicDBList (com.mongodb.BasicDBList)8 HashMap (java.util.HashMap)8 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)6 JSONArray (org.json.JSONArray)5 MongoClient (com.mongodb.MongoClient)4 ObjectId (org.locationtech.geogig.api.ObjectId)4 Function (com.google.common.base.Function)3 Date (java.util.Date)3 Iterator (java.util.Iterator)3 ObjectId (org.bson.types.ObjectId)3 WorkItem (com.example.entities.WorkItem)2