List of usage examples for com.mongodb.gridfs GridFS getBucketName
public String getBucketName()
From source file:com.ikanow.utility.GridFSRandomAccessFile.java
License:Open Source License
/** Returns a random access file accessor from a GridFS and fileId * @param gridFS - com.mongodb.gridfs.GridFS - the MongoDB gridFS "collection" * @param fileId - org.bson.ObjectId, the _id of the file * @throws IOException// w w w.j a v a2 s .co m */ public GridFSRandomAccessFile(GridFS gridFS, ObjectId fileId) throws IOException { this(gridFS.getDB(), gridFS.getBucketName(), fileId); }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Saves a file into the current database using the specified <tt>namespace</tt> and <tt>filename</tt>. All files sharing the same * <tt>namespace</tt> and <tt>filename</tt> are considered versions of the same file. So, inserting a new file with an existing * <tt>namespace</tt> and <tt>filename</tt> will create a new entry in the database. The method {@link #readFile(String, String)} will * retrieve the latest version of the file and the method {@link #readFile(String, String)} will remove all the versions of the file. * Other possible options could be to define a unique index in the <tt>files</tt> collection to avoid duplicates (versions) to be * created: <code>createIndex("filename", namespace + ".files");</code> * @param namespace - (optional) name space under the file is saved. When nothing specified, the default bucket is used * @param filename - filename to be assigned to the file in the database * @param file - file to be saved to the database * @param metadata - optional file metadata * @return the id associated to the file in the collection */// w w w . j ava 2 s . c om public String saveFile(final @Nullable String namespace, final String filename, final File file, final @Nullable DBObject metadata) { checkArgument(isNotBlank(filename), "Uninitialized or invalid filename"); checkArgument(file != null && file.canRead() && file.isFile(), "Uninitialized or invalid file"); String objectId = null; final String namespace2 = trimToEmpty(namespace); final String filename2 = filename.trim(); if (metadata != null) { metadata.removeField(IS_LATEST_VERSION_ATTR); } final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace2) ? new GridFS(db, namespace2) : new GridFS(db); // enforce isolation property: each namespace has its own bucket (encompassing 2 collections: files and chunks) and indexes in the database createSparseIndexWithUniqueConstraint(FILE_VERSION_PROP, gfsNs.getBucketName() + "." + GRIDFS_FILES_COLLECTION, false); // index open access links createNonUniqueIndex(FILE_OPEN_ACCESS_LINK_PROP, gfsNs.getBucketName() + "." + GRIDFS_FILES_COLLECTION, false); try { // insert new file/version in the database final GridFSInputFile gfsFile = gfsNs.createFile(file); gfsFile.setFilename(filename2); gfsFile.setContentType(mimeType(file)); gfsFile.setMetaData(metadata); gfsFile.save(); objectId = ObjectId.class.cast(gfsFile.getId()).toString(); // unset the latest version in the database final GridFSDBFile latestVersion = getLatestVersion(gfsNs, filename2); if (latestVersion != null && latestVersion.getMetaData() != null) { latestVersion.getMetaData().removeField(IS_LATEST_VERSION_ATTR); latestVersion.save(); } } catch (DuplicateKeyException dke) { throw new MongoDBDuplicateKeyException(dke.getMessage()); } catch (IOException ioe) { throw new IllegalStateException("Failed to save file", ioe); } finally { // enforce versioning property by always restoring the latest version in the database restoreLatestVersion(gfsNs, filename2); } return objectId; }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Restores the latest version of a file in the database. * @param gridfs - GridFS client//from w ww .j ava2s . c om * @param filename - filename to be searched for in the database */ private void restoreLatestVersion(final GridFS gridfs, final String filename) { try { final GridFSDBFile latestUploadedVersion = getLatestUploadedFile(gridfs, filename); if (latestUploadedVersion != null) { if (latestUploadedVersion.getMetaData() == null) { latestUploadedVersion.setMetaData(new BasicDBObject()); } latestUploadedVersion.getMetaData().put(IS_LATEST_VERSION_ATTR, filename); latestUploadedVersion.save(); } } catch (Exception ignore) { LOGGER.error("Failed to restore latest version namespace=" + gridfs.getBucketName() + ", filename=" + filename); } }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
public void updateMetadata(final @Nullable String namespace, final String filename, final @Nullable DBObject metadata) { checkArgument(isNotBlank(filename), "Uninitialized or invalid filename"); final String namespace2 = trimToEmpty(namespace); final String filename2 = filename.trim(); final DBObject metadata2 = metadata != null ? metadata : new BasicDBObject(); metadata2.put(IS_LATEST_VERSION_ATTR, filename2); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace2) ? new GridFS(db, namespace2) : new GridFS(db); try {/*from w w w . j a v a 2 s . c om*/ final GridFSDBFile latestUploadedVersion = getLatestUploadedFile(gfsNs, filename2); checkState(latestUploadedVersion != null, "File not found"); latestUploadedVersion.setMetaData(metadata2); latestUploadedVersion.save(); } catch (IllegalStateException ise) { throw ise; } catch (Exception e) { LOGGER.error("Failed to update latest metadata version in namespace=" + gfsNs.getBucketName() + ", filename=" + filename, e); } }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
public String createOpenAccessLink(final @Nullable String namespace, final String filename) { checkArgument(isNotBlank(filename), "Uninitialized or invalid filename"); final String namespace2 = trimToEmpty(namespace); final String filename2 = filename.trim(); final String secret = random(32, "abcdefghijklmnopqrstuvwxyz0123456789"); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace2) ? new GridFS(db, namespace2) : new GridFS(db); try {//from ww w. jav a2s . c o m final GridFSDBFile latestUploadedVersion = getLatestUploadedFile(gfsNs, filename2); checkState(latestUploadedVersion != null, "File not found"); if (latestUploadedVersion.getMetaData() == null) { latestUploadedVersion.setMetaData(new BasicDBObject()); } latestUploadedVersion.getMetaData().put(OPEN_ACCESS_LINK_ATTR, secret); latestUploadedVersion.getMetaData().put(OPEN_ACCESS_DATE_ATTR, JSON.parse(JSON_MAPPER.writeValueAsString(new Date()))); latestUploadedVersion.save(); } catch (IllegalStateException ise) { throw ise; } catch (Exception e) { LOGGER.error("Failed to create open access link in latest file version in namespace=" + gfsNs.getBucketName() + ", filename=" + filename, e); } return secret; }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Counts the files stored in the specified name space. * @param namespace - (optional) name space whose files are counted * @return the number of objects stored in the collection *//*from ww w . j a v a 2s . c om*/ public long countFiles(final @Nullable String namespace) { String namespace2 = trimToNull(namespace); if (namespace2 == null) { final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = new GridFS(db); namespace2 = gfsNs.getBucketName(); } return count(namespace2 + "." + GRIDFS_FILES_COLLECTION); }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Writes statistics about a files name space to the specified output stream. * @param os - the output stream to write the statistics to * @param namespace - (optional) name space from which the statistics are collected * @throws IOException - If an I/O error occurred *//*from w w w . ja v a2 s . co m*/ public void statsFiles(final OutputStream os, final @Nullable String namespace) throws IOException { String namespace2 = trimToNull(namespace); if (namespace2 == null) { final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = new GridFS(db); namespace2 = gfsNs.getBucketName(); } stats(os, namespace2 + "." + GRIDFS_FILES_COLLECTION); }
From source file:org.aw20.mongoworkbench.command.GridFSCreateBucketCommand.java
License:Open Source License
@Override public void execute() throws Exception { MongoClient mdb = MongoFactory.getInst().getMongo(sName); if (mdb == null) throw new Exception("no server selected"); if (sDb == null) throw new Exception("no database selected"); MongoFactory.getInst().setActiveDB(sDb); DB db = mdb.getDB(sDb);/*w ww . ja v a 2 s. c om*/ GridFS gfs = new GridFS(db, sColl); gfs.getBucketName(); setMessage("bucketCreated=" + sColl); }
From source file:rapture.blob.mongodb.GridFSBlobHandler.java
License:Open Source License
private String createLockKey(GridFS gridFS, String docPath) { return gridFS.getDB().getName() + "." + gridFS.getBucketName() + "." + docPath; }