Search in sources :

Example 51 with DBCursor

use of com.mongodb.DBCursor in project camel by apache.

the class GridFsProducer method process.

public void process(Exchange exchange) throws Exception {
    String operation = endpoint.getOperation();
    if (operation == null) {
        operation = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_OPERATION, String.class);
    }
    if (operation == null || "create".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        Long chunkSize = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_CHUNKSIZE, Long.class);
        InputStream ins = exchange.getIn().getMandatoryBody(InputStream.class);
        GridFSInputFile gfsFile = endpoint.getGridFs().createFile(ins, filename, true);
        if (chunkSize != null && chunkSize > 0) {
            gfsFile.setChunkSize(chunkSize);
        }
        final String ct = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
        if (ct != null) {
            gfsFile.setContentType(ct);
        }
        String metaData = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_METADATA, String.class);
        DBObject dbObject = (DBObject) JSON.parse(metaData);
        gfsFile.setMetaData(dbObject);
        gfsFile.save();
        //add headers with the id and file name produced by the driver.
        exchange.getIn().setHeader(Exchange.FILE_NAME_PRODUCED, gfsFile.getFilename());
        exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_FILE_ID_PRODUCED, gfsFile.getId());
    } else if ("remove".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        endpoint.getGridFs().remove(filename);
    } else if ("findOne".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        GridFSDBFile file = endpoint.getGridFs().findOne(filename);
        if (file != null) {
            exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData()));
            exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
            exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
            exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
            exchange.getIn().setBody(file.getInputStream(), InputStream.class);
        } else {
            throw new FileNotFoundException("No GridFS file for " + filename);
        }
    } else if ("listAll".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        DBCursor cursor;
        if (filename == null) {
            cursor = endpoint.getGridFs().getFileList();
        } else {
            cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename));
        }
        exchange.getIn().setBody(new DBCursorFilenameReader(cursor), Reader.class);
    } else if ("count".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        DBCursor cursor;
        if (filename == null) {
            cursor = endpoint.getGridFs().getFileList();
        } else {
            cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename));
        }
        exchange.getIn().setBody(cursor.count(), Integer.class);
    }
}
Also used : GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) InputStream(java.io.InputStream) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 52 with DBCursor

use of com.mongodb.DBCursor in project GeoGig by boundlessgeo.

the class MongoGraphDatabase method getDepth.

@Override
public int getDepth(ObjectId id) {
    int depth = 0;
    Set<ObjectId> front = new HashSet<ObjectId>();
    front.add(id);
    Function<ObjectId, String> idMapper = new Function<ObjectId, String>() {

        @Override
        public String apply(ObjectId id) {
            return id.toString();
        }
    };
    while (front.size() > 0) {
        DBObject query = new BasicDBObject();
        query.put("_label", Relationship.PARENT.name());
        query.put("_in", new BasicDBObject("$in", Lists.transform(new ArrayList<ObjectId>(front), idMapper)));
        DBCursor result = collection.find(query);
        Set<ObjectId> nextFront = new HashSet<ObjectId>();
        for (DBObject o : result) {
            front.remove(ObjectId.valueOf((String) o.get("_in")));
            nextFront.add(ObjectId.valueOf((String) o.get("_out")));
        }
        if (front.size() > 0) {
            break;
        } else {
            front = nextFront;
            depth += 1;
        }
    }
    return depth;
}
Also used : Function(com.google.common.base.Function) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) ObjectId(org.locationtech.geogig.api.ObjectId) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) HashSet(java.util.HashSet)

Example 53 with DBCursor

use of com.mongodb.DBCursor in project GeoGig by boundlessgeo.

the class MongoGraphDatabase method getParents.

@Override
public ImmutableList<ObjectId> getParents(ObjectId id) {
    DBObject query = new BasicDBObject();
    query.put("_label", Relationship.PARENT.name());
    query.put("_in", id.toString());
    DBCursor cursor = collection.find(query);
    Function<DBObject, ObjectId> idMapper = new Function<DBObject, ObjectId>() {

        @Override
        public ObjectId apply(DBObject o) {
            return ObjectId.valueOf((String) o.get("_out"));
        }
    };
    return ImmutableList.copyOf(Iterators.transform(cursor.iterator(), idMapper));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Function(com.google.common.base.Function) DBCursor(com.mongodb.DBCursor) ObjectId(org.locationtech.geogig.api.ObjectId) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 54 with DBCursor

use of com.mongodb.DBCursor in project GeoGig by boundlessgeo.

the class MongoObjectDatabase method lookUp.

@Override
public List<ObjectId> lookUp(final String partialId) {
    if (partialId.matches("[a-fA-F0-9]+")) {
        DBObject regex = new BasicDBObject();
        regex.put("$regex", "^" + partialId);
        DBObject query = new BasicDBObject();
        query.put("oid", regex);
        DBCursor cursor = collection.find(query);
        List<ObjectId> ids = new ArrayList<ObjectId>();
        while (cursor.hasNext()) {
            DBObject elem = cursor.next();
            String oid = (String) elem.get("oid");
            ids.add(ObjectId.valueOf(oid));
        }
        return ids;
    } else {
        throw new IllegalArgumentException("Prefix query must be done with hexadecimal values only");
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) ObjectId(org.locationtech.geogig.api.ObjectId) ArrayList(java.util.ArrayList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 55 with DBCursor

use of com.mongodb.DBCursor in project GeoGig by boundlessgeo.

the class MongoStagingDatabase method getConflicts.

@Override
public List<Conflict> getConflicts(@Nullable String namespace, @Nullable String pathFilter) {
    DBObject query = new BasicDBObject();
    if (namespace == null) {
        query.put("namespace", 0);
    } else {
        query.put("namespace", namespace);
    }
    if (pathFilter != null) {
        DBObject regex = new BasicDBObject();
        regex.put("$regex", "^" + pathFilter);
        query.put("path", regex);
    }
    DBCursor cursor = conflicts.find(query);
    List<Conflict> results = new ArrayList<Conflict>();
    while (cursor.hasNext()) {
        DBObject element = cursor.next();
        String path = (String) element.get("path");
        ObjectId ancestor = ObjectId.valueOf((String) element.get("ancestor"));
        ObjectId ours = ObjectId.valueOf((String) element.get("ours"));
        ObjectId theirs = ObjectId.valueOf((String) element.get("theirs"));
        results.add(new Conflict(path, ancestor, ours, theirs));
    }
    return results;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) Conflict(org.locationtech.geogig.api.plumbing.merge.Conflict) ObjectId(org.locationtech.geogig.api.ObjectId) ArrayList(java.util.ArrayList) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

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