@Override public ByteArrayFileCache getBlob(final MD5 md5, final ByteArrayFileCacheManager bafcMan) throws NoSuchBlobException, BlobStoreCommunicationException, FileCacheIOException, FileCacheLimitExceededException { final GridFSDBFile out; try { out = getFile(md5); if (out == null) { throw new NoSuchBlobException( "Attempt to retrieve non-existant blob with chksum " + md5.getMD5()); } final boolean sorted; if (!out.containsField(Fields.GFS_SORTED)) { sorted = false; } else { sorted = (Boolean) out.get(Fields.GFS_SORTED); } final InputStream file = out.getInputStream(); try { return bafcMan.createBAFC(file, true, sorted); } finally { try { file.close(); } catch (IOException ioe) { throw new RuntimeException("Something is broken", ioe); } } } catch (MongoException me) { throw new BlobStoreCommunicationException("Could not read from the mongo database", me); } }
private GridFSDBFile getFile(final MD5 md5) { final GridFSDBFile out; final DBObject query = new BasicDBObject(); query.put(Fields.MONGO_ID, md5.getMD5()); out = gfs.findOne(query); return out; }
@Override public void removeBlob(MD5 md5) throws BlobStoreCommunicationException { final DBObject query = new BasicDBObject(); query.put(Fields.MONGO_ID, md5.getMD5()); try { gfs.remove(query); } catch (MongoException me) { throw new BlobStoreCommunicationException("Could not write to the mongo database", me); } }
@Override public void saveBlob(final MD5 md5, final InputStream data, final boolean sorted) throws BlobStoreCommunicationException { if (data == null || md5 == null) { throw new NullPointerException("Arguments cannot be null"); } if (getFile(md5) != null) { return; // already exists } final GridFSInputFile gif = gfs.createFile(data, true); gif.setId(md5.getMD5()); gif.setFilename(md5.getMD5()); gif.put(Fields.GFS_SORTED, sorted); try { gif.save(); } catch (DuplicateKeyException dk) { // already here, done } catch (MongoException me) { throw new BlobStoreCommunicationException("Could not write to the mongo database", me); } }