Example usage for com.mongodb.gridfs GridFS remove

List of usage examples for com.mongodb.gridfs GridFS remove

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFS remove.

Prototype

public void remove(final DBObject query) 

Source Link

Document

Removes all files matching the given query.

Usage

From source file:calliope.db.MongoConnection.java

License:Open Source License

/**
 * Delete an image from the database//  w w w .j a  va2  s. c o m
 * @param collName the collection name e.g. "corpix"
 * @param docID the image's docid path
 * @throws AeseException 
 */
@Override
public void removeImageFromDb(String collName, String docID) throws AeseException {
    try {
        GridFS gfs = new GridFS(db, collName);
        GridFSDBFile file = gfs.findOne(docID);
        if (file == null)
            throw new FileNotFoundException("file " + collName + "/" + docID + " not found");
        gfs.remove(file);
    } catch (Exception e) {
        throw new AeseException(e);
    }
}

From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java

License:Apache License

@Override
public void removeTrivial(String spaceName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    DBObject query = new BasicDBObject();
    query.put(FIELD_SPACE_NAME, new ObjectId(spaceName));
    GridFS gfs = new GridFS(db, TABLE_TRIVIAL);
    gfs.remove(query);
}

From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java

License:Apache License

private void deleteMetaAndContent(String storageKey, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    DBObject query = new BasicDBObject();
    query.put(FIELD_STORAGE_KEY, new ObjectId(storageKey));
    GridFS gfs = new GridFS(db, tableName);
    gfs.remove(query);
}

From source file:com.andreig.jetty.GridfsServlet.java

License:GNU General Public License

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doDelete()");

    if (!can_write(req)) {
        res.sendError(SC_UNAUTHORIZED);/*from   ww w.j  ava 2s. c  o  m*/
        return;
    }

    String db_name = req.getParameter("dbname");
    String bucket_name = req.getParameter("bucketname");
    if (db_name == null || bucket_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];
            bucket_name = names[1];
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (bucket_name == null)
        bucket_name = "fs";

    DB db = mongo.getDB(db_name);

    String fs_cache_key = db_name + bucket_name;
    GridFS fs = fs_cache.get(fs_cache_key);
    if (fs == null) {
        fs = new GridFS(db, bucket_name);
        fs_cache.put(fs_cache_key, fs);
    }

    // mongo auth
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (user != null && passwd != null && (!db.isAuthenticated())) {
        boolean auth = db.authenticate(user, passwd.toCharArray());
        if (!auth) {
            res.sendError(SC_UNAUTHORIZED);
            return;
        }
    }

    String file_name = req.getParameter("filename");

    if (file_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }

    fs.remove(file_name);

    out_json(req, Status.OK);

}

From source file:com.bluedragon.mongo.gridfs.Delete.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {

    // Get the necessary Mongo references
    DB db = getDB(_session, argStruct);/*from   w w w. j a va  2s .  com*/
    GridFS gridfs = getGridFS(_session, argStruct, db);

    // Get the file information
    String filename = getNamedStringParam(argStruct, "filename", null);
    if (filename != null) {
        gridfs.remove(filename);
        return cfBooleanData.TRUE;
    }

    // Get the _id
    String _id = getNamedStringParam(argStruct, "_id", null);
    if (_id != null) {
        gridfs.remove(new ObjectId(_id));
        return cfBooleanData.TRUE;
    }

    // Get the Query
    cfData mTmp = getNamedParam(argStruct, "query", null);
    if (mTmp != null) {
        gridfs.remove(getDBObject(mTmp));
        return cfBooleanData.TRUE;
    }

    throwException(_session, "Please specify file, _id or a query");
    return null;
}

From source file:com.cognifide.aet.vs.artifacts.ArtifactsDAOMongoDBImpl.java

License:Apache License

@Override
public void removeArtifacts(DBKey dbKey, Set<String> artifactsToRemove) {
    GridFS gfs = getGridFS(dbKey);
    if (gfs != null) {
        for (String artifactId : artifactsToRemove) {
            LOGGER.debug("Removing artifact {} from {}", artifactId, dbKey);
            gfs.remove(new ObjectId(artifactId));
        }//from   w  w  w  . j  a  va  2  s  .  co m
    }
}

From source file:com.cyslab.craftvm.rest.mongo.GridfsServlet.java

License:GNU General Public License

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.trace("doDelete()");

    if (!can_write(req)) {
        res.sendError(SC_UNAUTHORIZED);/*from ww w .  j  a v  a 2s. c o  m*/
        return;
    }

    String db_name = req.getParameter("dbname");
    String bucket_name = req.getParameter("bucketname");
    if (db_name == null || bucket_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];
            bucket_name = names[1];
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (bucket_name == null)
        bucket_name = "fs";

    DB db = mongo.getDB(db_name);

    String fs_cache_key = db_name + bucket_name;
    GridFS fs = fs_cache.get(fs_cache_key);
    if (fs == null) {
        fs = new GridFS(db, bucket_name);
        fs_cache.put(fs_cache_key, fs);
    }

    // mongo auth
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (user != null && passwd != null && (!db.isAuthenticated())) {
        boolean auth = db.authenticate(user, passwd.toCharArray());
        if (!auth) {
            res.sendError(SC_UNAUTHORIZED);
            return;
        }
    }

    String file_name = req.getParameter("filename");

    if (file_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }

    fs.remove(file_name);

    out_json(req, Status.OK);

}

From source file:com.fileoperations.RenameFolder.java

public Boolean forSingleFile() {
    try {//from   www  .  j a  v a 2s.c o m
        if (oldName.contains(".")) {

            BasicDBObject query = new BasicDBObject();
            query.put("_id", parentPath + pathMerger + oldName);
            DBCursor cursor = collection.find(query);

            if (cursor.hasNext()) {
                DBObject renameFile = cursor.next();
                BasicDBObject checknewquery = new BasicDBObject();
                checknewquery.put("_id", parentPath + pathMerger + newName + renameFile.get("type").toString());
                DBCursor tempCursor = collection.find(checknewquery);
                if (tempCursor.hasNext()) {
                    return false;
                }

                GridFS file = new GridFS(mymongo.getDB(), userCollectionName);
                InputStream data = file.findOne(query).getInputStream();

                BasicDBObject document = new BasicDBObject();
                document.append("_id", parentPath + pathMerger + newName + renameFile.get("type").toString());
                document.append("folder", "0");
                document.append("parent", parentPath);
                document.append("name", newName + renameFile.get("type").toString());
                document.append("type", renameFile.get("type").toString());
                collection.insert(document);
                GridFSInputFile inputFile = file.createFile(data);
                inputFile.setId(parentPath + pathMerger + newName + renameFile.get("type").toString());
                inputFile.put("path", parentPath);
                inputFile.setFilename(newName + renameFile.get("type").toString());
                inputFile.save();
                file.remove(file.findOne(query));
                collection.remove(renameFile);
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } finally {
        mymongo.closeConnection();
    }
}

From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java

License:Open Source License

/**
 * delte gridfs//from  w  w w  .ja va2  s .com
 * 
 * @param userDB
 * @param _id
 * @throws Exception
 */
public static void dleteGridFs(UserDBDAO userDB, String strBucket, String _id) throws Exception {
    DB mongoDb = findDB(userDB);
    GridFS gridFs = null;

    if ("".equals(strBucket))
        gridFs = new GridFS(mongoDb);
    else
        gridFs = new GridFS(mongoDb, strBucket);

    gridFs.remove(gridFs.findOne(new ObjectId(_id)));

}

From source file:com.imaginea.mongodb.services.GridFSServiceImpl.java

License:Apache License

/**
 * Service implementation for dropping a file from GridFS.
 *
 * @param dbName     Name of Database//w  ww.  java  2  s . c  o m
 * @param bucketName Name of GridFS Bucket
 * @param id         Object id of file to be deleted
 * @returns Status message.
 */
public String deleteFile(String dbName, String bucketName, ObjectId id)
        throws DatabaseException, DocumentException, ValidationException {
    mongoInstance = mongoInstanceProvider.getMongoInstance();
    if (dbName == null) {
        throw new EmptyDatabaseNameException("Database name is null");

    }
    if (dbName.equals("")) {
        throw new EmptyDatabaseNameException("Database Name Empty");
    }

    if (bucketName == null) {
        throw new EmptyCollectionNameException("Bucket name is null");
    }
    if (bucketName.equals("")) {
        throw new EmptyCollectionNameException("Bucket Name Empty");
    }

    String result = null;
    GridFSDBFile gridFSDBFile = null;
    try {
        if (!mongoInstance.getDatabaseNames().contains(dbName)) {
            throw new UndefinedDatabaseException("DB [" + dbName + "] DOES NOT EXIST");
        }
        if (id == null) {
            throw new EmptyDocumentDataException("File is empty");
        }

        GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName);

        gridFSDBFile = gridFS.findOne(id);

        if (gridFSDBFile == null) {
            throw new UndefinedDocumentException("DOCUMENT_DOES_NOT_EXIST");
        }

        gridFS.remove(id);

    } catch (MongoException e) {
        throw new DeleteDocumentException("FILE_DELETION_EXCEPTION");
    }
    result = "Deleted File : [" + gridFSDBFile.getFilename() + "]";
    return result;
}