Search in sources :

Example 56 with DBCursor

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

the class MongoDBResourceProvider method delete.

/**
     * TODO - we should handle delete different and not put all child resources into the
     * deleted set.
     * Instead when getting resources, the parents of the resource should be checked
     * first.
     * This minimizes concurrency issues.
     * @see org.apache.sling.api.resource.ModifyingResourceProvider#delete(org.apache.sling.api.resource.ResourceResolver, java.lang.String)
     */
public void delete(final ResourceResolver resolver, final String path) throws PersistenceException {
    final String[] info = this.extractResourceInfo(path);
    if (info != null) {
        boolean deletedResource = false;
        if (!deletedResources.contains(path)) {
            final Resource rsrc = this.getResource(resolver, path, info);
            if (rsrc instanceof MongoDBResource) {
                this.deletedResources.add(path);
                this.changedResources.remove(path);
                final DBCollection col = this.getCollection(info[0]);
                final String pattern = "^" + Pattern.quote(info[1]) + "/";
                final DBObject query = QueryBuilder.start(getPROP_PATH()).regex(Pattern.compile(pattern)).get();
                final DBCursor cur = col.find(query);
                while (cur.hasNext()) {
                    final DBObject dbObj = cur.next();
                    final String childPath = info[0] + '/' + dbObj.get(getPROP_PATH());
                    this.deletedResources.add(childPath);
                    this.changedResources.remove(childPath);
                }
                deletedResource = true;
            }
        } else {
            deletedResource = true;
        }
        if (deletedResource) {
            final String prefix = path + "/";
            final Iterator<Map.Entry<String, MongoDBResource>> i = this.changedResources.entrySet().iterator();
            while (i.hasNext()) {
                final Map.Entry<String, MongoDBResource> entry = i.next();
                if (entry.getKey().startsWith(prefix)) {
                    i.remove();
                }
            }
            return;
        }
    }
    throw new PersistenceException("Unable to delete resource at {}" + path, null, path, null);
}
Also used : Resource(org.apache.sling.api.resource.Resource) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor) PersistenceException(org.apache.sling.api.resource.PersistenceException) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 57 with DBCursor

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

the class MongoDBStore method getRole.

@Override
public Role getRole(String name) {
    DBCollection coll = getCollection();
    DBCursor cursor = coll.find(getTemplateObject(name));
    try {
        if (cursor.hasNext()) {
            return m_helper.deserialize(cursor.next());
        }
    } finally {
        cursor.close();
    }
    return null;
}
Also used : DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor)

Example 58 with DBCursor

use of com.mongodb.DBCursor in project incubator-rya by apache.

the class SimpleMongoDBNamespaceManager method iterateNamespace.

@Override
public CloseableIteration<? extends Namespace, RyaDAOException> iterateNamespace() throws RyaDAOException {
    final DBObject query = new BasicDBObject();
    final DBCursor cursor = nsColl.find(query);
    return new MongoCursorIteration(cursor);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 59 with DBCursor

use of com.mongodb.DBCursor in project Twister by NicolasBizzozzero.

the class MessagesTools method getTousLesMessages.

/**
 * Permet d'obtenir tous les messages contenus dans la collection "Messages"
 * Utilisee seulement a des fins de debugage
 * @return Un JSONObject contenant tous les messages
 * @throws UnknownHostException
 */
public static JSONObject getTousLesMessages() throws UnknownHostException {
    // On se connecte a la BDD puis on recupere les messages
    DBCollection messages = getCollectionMessages();
    // On itere sur les resultats
    DBCursor curseur = messages.find();
    JSONObject reponse = new JSONObject();
    while (curseur.hasNext()) {
        reponse.accumulate("Messages", curseur.next());
    }
    return reponse;
}
Also used : DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor) JSONObject(org.json.JSONObject)

Example 60 with DBCursor

use of com.mongodb.DBCursor in project Twister by NicolasBizzozzero.

the class CommentairesTools method commentaireExistant.

/**
 * Verifie si un commentaire existe dans un message
 * @param id_message : L'ID du message contenant le commentaire
 * @param id_commentaire : L'ID du commentaire a supprimer
 * @return -1 si le message n'existe pas, son index dans la liste sinon.
 * @throws UnknownHostException
 */
public static boolean commentaireExistant(String id_message, String id_commentaire) throws UnknownHostException {
    // On se connecte a la BDD puis on recupere les messages
    DBCollection messages = bd.tools.MessagesTools.getCollectionMessages();
    // Creation du message
    BasicDBObject message = new BasicDBObject();
    message.put(Noms.CHAMP_ID_MESSAGE, Integer.parseInt(id_message));
    // On verifie si le message existe
    DBCursor curseur = messages.find(message);
    if (!curseur.hasNext()) {
        // Le message n'existe pas, donc le commentaire non plus
        return false;
    }
    // On recupere la liste des commentaires
    JSONObject reponse_message = new JSONObject(JSON.serialize(curseur.next()));
    JSONArray commentaires = reponse_message.getJSONArray(Noms.CHAMP_COMMENTAIRES);
    // On itere dessus jusqu'au bout ou jusqu'a trouver le commentaire
    for (int i = 0; i < commentaires.length(); i++) {
        JSONObject commentaire = commentaires.getJSONObject(i);
        if (commentaire.getInt(Noms.CHAMP_ID_COMMENTAIRE) == (Integer.parseInt(id_commentaire))) {
            return true;
        }
    }
    return false;
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

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