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());
}
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;
}
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();
}
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");
}
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;
}
Aggregations