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