Search in sources :

Example 21 with DBCursor

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;
}
Also used : DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor) Resource(org.apache.sling.api.resource.Resource) Iterator(java.util.Iterator) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 22 with DBCursor

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;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 23 with DBCursor

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;
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 24 with DBCursor

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));
}
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 25 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)

Aggregations

DBCursor (com.mongodb.DBCursor)54 BasicDBObject (com.mongodb.BasicDBObject)46 DBObject (com.mongodb.DBObject)45 DBCollection (com.mongodb.DBCollection)23 MongoException (com.mongodb.MongoException)8 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)7 Function (com.google.common.base.Function)6 BasicDBList (com.mongodb.BasicDBList)6 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)6 JSONObject (org.json.JSONObject)6 Test (org.junit.Test)6 QueryBuilder (com.mongodb.QueryBuilder)5 ObjectId (org.locationtech.geogig.api.ObjectId)4 DB (com.mongodb.DB)3 Map (java.util.Map)3 Nonnull (javax.annotation.Nonnull)3 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)3 GridFSDBFile (com.mongodb.gridfs.GridFSDBFile)2 VCollection (de.fhg.igd.mongomvcc.VCollection)2