use of com.mongodb.DBCursor in project sling by apache.
the class MongoDBResourceProvider method findResources.
public Iterator<Resource> findResources(final ResourceResolver resolver, String query, String language) {
if (!language.equals("mongodb") || query == null || query.length() == 0 || query.indexOf(".find(") <= 0) {
return null;
}
Iterator<Resource> returnValue = null;
final String collectionName = query.substring(0, query.indexOf(".find("));
DBCollection col = this.getCollection(collectionName);
if (col != null) {
String criteria = query.trim().substring(query.indexOf(".find(") + 6, query.length() - 1);
DBObject dbObject = (DBObject) JSON.parse(criteria);
final DBCursor cur = col.find(dbObject);
final String rootPath = context.getRootWithSlash();
return new Iterator<Resource>() {
public boolean hasNext() {
return cur.hasNext();
}
public Resource next() {
final DBObject obj = cur.next();
final String objPath = obj.get(getPROP_PATH()).toString();
final int lastSlash = objPath.lastIndexOf('/');
final String name;
if (lastSlash == -1) {
name = objPath;
} else {
name = objPath.substring(lastSlash + 1);
}
return new MongoDBResource(resolver, rootPath + collectionName + "/" + name, collectionName, obj, MongoDBResourceProvider.this);
}
public void remove() {
throw new UnsupportedOperationException("remove");
}
};
}
return returnValue;
}
use of com.mongodb.DBCursor in project sling by apache.
the class MongoDBResourceProvider method listChildren.
/**
* TODO - we have to check for deleted and added resources
* @see org.apache.sling.api.resource.ResourceProvider#listChildren(org.apache.sling.api.resource.Resource)
*/
public Iterator<Resource> listChildren(final Resource parent) {
final String[] info = this.extractResourceInfo(parent.getPath());
if (info != null) {
if (info.length == 0) {
// all collections
final Set<String> names = new HashSet<String>(context.getDatabase().getCollectionNames());
names.removeAll(this.context.getFilterCollectionNames());
final Iterator<String> i = names.iterator();
return new Iterator<Resource>() {
public boolean hasNext() {
return i.hasNext();
}
public Resource next() {
final String name = i.next();
return new MongoDBCollectionResource(parent.getResourceResolver(), parent.getPath() + '/' + name);
}
public void remove() {
throw new UnsupportedOperationException("remove");
}
};
}
final DBCollection col = this.getCollection(info[0]);
if (col != null) {
final String pattern;
if (info.length == 1) {
pattern = "^([^/])*$";
} else {
pattern = "^" + Pattern.quote(info[1]) + "/([^/])*$";
}
final DBObject query = QueryBuilder.start(getPROP_PATH()).regex(Pattern.compile(pattern)).get();
final DBCursor cur = col.find(query).sort(BasicDBObjectBuilder.start(getPROP_PATH(), 1).get());
return new Iterator<Resource>() {
public boolean hasNext() {
return cur.hasNext();
}
public Resource next() {
final DBObject obj = cur.next();
final String objPath = obj.get(getPROP_PATH()).toString();
final int lastSlash = objPath.lastIndexOf('/');
final String name;
if (lastSlash == -1) {
name = objPath;
} else {
name = objPath.substring(lastSlash + 1);
}
return new MongoDBResource(parent.getResourceResolver(), parent.getPath() + '/' + name, info[0], obj, MongoDBResourceProvider.this);
}
public void remove() {
throw new UnsupportedOperationException("remove");
}
};
}
}
return null;
}
use of com.mongodb.DBCursor in project GeoGig by boundlessgeo.
the class MongoObjectDatabase method getIfPresent.
@Override
public RevObject getIfPresent(ObjectId id) {
DBObject query = new BasicDBObject();
query.put("oid", id.toString());
DBCursor results = collection.find(query);
if (results.hasNext()) {
DBObject result = results.next();
return fromBytes(id, (byte[]) result.get("serialized_object"));
} else {
return null;
}
}
use of com.mongodb.DBCursor in project GeoGig by boundlessgeo.
the class MongoGraphDatabase method getChildren.
@Override
public ImmutableList<ObjectId> getChildren(ObjectId id) {
DBObject query = new BasicDBObject();
query.put("_label", Relationship.PARENT.name());
query.put("_out", 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("_in"));
}
};
return ImmutableList.copyOf(Iterators.transform(cursor.iterator(), idMapper));
}
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());
}
Aggregations