List of usage examples for com.mongodb.gridfs GridFS GridFS
public GridFS(final DB db, final String bucket)
From source file:de.fhg.igd.mongomvcc.impl.MongoDBVBranch.java
License:Open Source License
/** * Gets or creates a database collection that can handle large * objects (BLOBs) and uses a given access strategy. * @param name the collection's name/*from w w w. j ava 2 s .com*/ * @param accessStrategy the strategy used to access large objects * @return the collection (never null) */ public VLargeCollection getLargeCollection(String name, AccessStrategy accessStrategy) { DB db = _db.getDB(); return new MongoDBVLargeCollection(db.getCollection(name), new GridFS(db, name), this, _db.getCounter(), accessStrategy); }
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 ww . j av a2 s. c o m*/ 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
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 av 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 .ja va2 s . 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
public void removeOpenAccessLink(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 DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace2) ? new GridFS(db, namespace2) : new GridFS(db); final List<GridFSDBFile> files = gfsNs.find(new BasicDBObject("filename", filename2) .append(FILE_OPEN_ACCESS_LINK_PROP, new BasicDBObject("$exists", true)), new BasicDBObject("uploadDate", -1)); for (final GridFSDBFile file : files) { if (file.getMetaData() != null) { file.getMetaData().removeField(OPEN_ACCESS_LINK_ATTR); file.getMetaData().removeField(OPEN_ACCESS_DATE_ATTR); file.save();//from w w w. j a v a 2 s .c o m } } /* ideally we want to use a cursor here, but the save() operation fails final DBCursor cursor = gfsNs.getFileList(new BasicDBObject("filename", filename2).append(FILE_OPEN_ACCESS_LINK_PROP, new BasicDBObject("$exists", true))); try { while (cursor.hasNext()) { final GridFSDBFile file = (GridFSDBFile) cursor.next(); if (file.getMetaData() != null) { file.getMetaData().removeField(OPEN_ACCESS_LINK_ATTR); file.getMetaData().removeField(OPEN_ACCESS_DATE_ATTR); // this fails file.save(); } } } finally { cursor.close(); } */ }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Reads a file object from the current database. The file is identified by the original filename stored in the database and the * name space under the file was stored. When several versions exist of the same file, the latest version will be retrieved. * @param namespace - (optional) name space to be searched for in the database. When nothing specified, the default bucket is used * @param filename - filename to be searched for in the database * @param output - file where the output will be saved, this file must exists and must be writable *//*from w ww . ja v a2s . c om*/ public GridFSDBFile readFile(final @Nullable String namespace, final String filename) { checkArgument(isNotBlank(filename), "Uninitialized or invalid filename"); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db); return getLatestVersion(gfsNs, filename); }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
public GridFSDBFileWrapper readOpenAccessFile(final String secret) { checkArgument(isNotBlank(secret), "Uninitialized or invalid secret"); final String secret2 = secret.trim(); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); GridFSDBFileWrapper fileWrapper = null; final Set<String> collections = db.getCollectionNames(); final Iterator<String> it = collections.iterator(); while (it.hasNext() && fileWrapper == null) { final String collection = it.next(); if (collection.endsWith("." + GRIDFS_FILES_COLLECTION)) { final String[] tokens = Splitter.on('.').omitEmptyStrings().trimResults().splitToList(collection) .toArray(new String[2]); if (tokens.length >= 2) { final String namespace = tokens[tokens.length - 2]; final GridFS gfsNs = new GridFS(db, namespace); final GridFSDBFile file = gfsNs.findOne(new BasicDBObject(FILE_OPEN_ACCESS_LINK_PROP, secret2)); if (file != null) { fileWrapper = new GridFSDBFileWrapper(namespace, file); }/*w ww.j av a 2 s . co m*/ } } } return fileWrapper; }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Checks whether or not the specified file exists in the database, returning <tt>true</tt> only when the file exists in the * specified namespace./*from ww w . j av a 2 s .c o m*/ * @param namespace - (optional) name space to be searched for in the database. When nothing specified, the default bucket is used * @param filename - filename to be searched for in the database * @return */ public boolean fileExists(final @Nullable String namespace, final String filename) { checkArgument(isNotBlank(filename), "Uninitialized or invalid filename"); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db); return gfsNs.findOne(filename.trim()) != null; }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Lists all the files in the specified name space. Only latest versions are included in the list. * @param namespace - (optional) name space to be searched for files. When nothing specified, the default bucket is used * @param sortCriteria - objects in the collection are sorted with this criteria * @param start - starting index/* w ww . j a v a 2 s .c o m*/ * @param size - maximum number of objects returned * @param count - (optional) is updated with the number of objects in the database * @return a view of the files stored under the specified name space that contains the specified range. */ public List<GridFSDBFile> listFiles(final @Nullable String namespace, final DBObject sortCriteria, final int start, final int size, final @Nullable MutableLong count) { final List<GridFSDBFile> list = newArrayList(); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db); final DBCursor cursor = gfsNs.getFileList( new BasicDBObject(FILE_VERSION_PROP, new BasicDBObject("$exists", true)), sortCriteria); cursor.skip(start).limit(size); try { while (cursor.hasNext()) { list.add((GridFSDBFile) cursor.next()); } } finally { cursor.close(); } if (count != null) { count.setValue(cursor.count()); } return list; }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
public List<GridFSDBFile> listFileOpenAccess(final @Nullable String namespace, final DBObject sortCriteria, final int start, final int size, final @Nullable MutableLong count) { final List<GridFSDBFile> list = newArrayList(); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db); final DBCursor cursor = gfsNs .getFileList(new BasicDBObject(FILE_VERSION_PROP, new BasicDBObject("$exists", true)) .append(FILE_OPEN_ACCESS_LINK_PROP, new BasicDBObject("$exists", true)), sortCriteria); cursor.skip(start).limit(size);// www . ja v a 2 s . com try { while (cursor.hasNext()) { list.add((GridFSDBFile) cursor.next()); } } finally { cursor.close(); } if (count != null) { count.setValue(cursor.count()); } return list; }