下面列出了怎么用com.mongodb.gridfs.GridFS的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
@Ignore
public void storeFile() throws IOException {
File file = new File("/home/darrenfu/IdeaProjects/pampas/pampas-grpc/df/open/grpc/hello/grpc-test-229014610914606914.jar");
GridFS gridFS = new GridFS(datastore.getDB());
GridFSInputFile gridFSInputFile = gridFS.createFile(file);
if (gridFS.findOne(file.getName()) == null) {
gridFSInputFile.setId(file.getName());
gridFSInputFile.setMetaData(new BasicDBObject("version", "1.1.2"));
gridFSInputFile.save();
}
GridFSDBFile fsdbFile = gridFS.findOne(file.getName());
File newfile = new File("/home/darrenfu/IdeaProjects/pampas/pampas-grpc/df/open/grpc/hello/grpc-test-229014610914606914.new.jar");
if (newfile.exists()) {
newfile.delete();
}
newfile.createNewFile();
newfile.setWritable(true);
fsdbFile.writeTo(newfile);
System.out.println("done : " + fsdbFile.getFilename());
}
@Override
public long getEstimatedSizeBytes(PipelineOptions options) throws Exception {
Mongo mongo = spec.connectionConfiguration().setupMongo();
try {
GridFS gridfs = spec.connectionConfiguration().setupGridFS(mongo);
DBCursor cursor = createCursor(gridfs);
long size = 0;
while (cursor.hasNext()) {
GridFSDBFile file = (GridFSDBFile) cursor.next();
size += file.getLength();
}
return size;
} finally {
mongo.close();
}
}
public String saveImageAndReturnFilename(final JUnitEmbedding embedding, final Coordinates coordinates, final String featureId,
final String scenarioId) {
final GridFS gridFS = getGridFS();
try {
final GridFSInputFile image = gridFS
.createFile(Base64.decodeBase64((embedding.getData()).getBytes()));
image.setFilename(UUID.randomUUID().toString());
final BasicDBObject metadata = new BasicDBObject().append("product", coordinates.getProduct())
.append("major", coordinates.getMajor()).append("minor", coordinates.getMinor())
.append("servicePack", coordinates.getServicePack()).append("build", coordinates.getBuild())
.append("feature", featureId)
.append("scenario", scenarioId);
image.setMetaData(metadata);
image.setContentType(embedding.getMime_type());
image.save();
return image.getFilename();
} catch (final ClassCastException e) {
LOGGER.warn("Embedding was malformed and will be skipped");
return null;
}
}
@Test
public void getFileMapByAppId() throws Exception{
//Fixture
Mockito.when(mongoTemplate.getDb()).thenReturn(db);
PowerMockito.whenNew(GridFS.class).withArguments(db,collectionNameMap).thenReturn(gridFS);
Mockito.when(gridFS.findOne(idApp)).thenReturn(gridFSFile);
Mockito.when(gridFSFile.getInputStream()).thenReturn(inputStream);
// Experimentations
InputStream response=mapFileRepositoryCustomImpl.getMapFileByAppId(idApp);
// Expectations
Mockito.verify(gridFS).findOne(idApp);
Mockito.verify(gridFSFile).getInputStream();
Assert.assertEquals("File .map must be the same",inputStream,response);
}
@Test
public void getFileMapByAppId() throws Exception{
//Fixture
Mockito.when(mongoTemplate.getDb()).thenReturn(db);
PowerMockito.whenNew(GridFS.class).withArguments(db,collectionNameMap).thenReturn(gridFS);
Mockito.when(gridFS.findOne(idApp)).thenReturn(gridFSFile);
Mockito.when(gridFSFile.getInputStream()).thenReturn(inputStream);
// Experimentations
InputStream response=mapFileRepositoryCustomImpl.getMapFileByAppId(idApp);
// Expectations
Mockito.verify(gridFS).findOne(idApp);
Mockito.verify(gridFSFile).getInputStream();
Assert.assertEquals("File .map must be the same",inputStream,response);
}
@Test
public void saveFileMap() throws Exception{
//Fixture
Mockito.when(mongoTemplate.getDb()).thenReturn(db);
PowerMockito.whenNew(GridFS.class).withArguments(db, collectionNameMap).thenReturn(gridFS);
Mockito.when(gridFS.findOne(idApp)).thenReturn(gridFSFile);
Mockito.when(gridFS.createFile(file)).thenReturn(gridFSInputFile);
// Experimentations
importFilesRepositoryCustomImpl.saveFileMap(idApp,file);
// Expectations
Mockito.verify(gridFS).remove(gridFSFile);
Mockito.verify(gridFS).createFile(file);
Mockito.verify(gridFSInputFile).setFilename(idApp);
Mockito.verify(gridFSInputFile).save();
}
@Test
public void saveFileMapWithoutRemoving() throws Exception{
//Fixture
Mockito.when(mongoTemplate.getDb()).thenReturn(db);
PowerMockito.whenNew(GridFS.class).withArguments(db, collectionNameMap).thenReturn(gridFS);
Mockito.when(gridFS.findOne(idApp)).thenReturn(null);
Mockito.when(gridFS.createFile(file)).thenReturn(gridFSInputFile);
// Experimentations
importFilesRepositoryCustomImpl.saveFileMap(idApp,file);
// Expectations
Mockito.verify(gridFS).createFile(file);
Mockito.verify(gridFSInputFile).setFilename(idApp);
Mockito.verify(gridFSInputFile).save();
}
@Test
public void saveFileOSM() throws Exception{
//Fixture
Mockito.when(mongoTemplate.getDb()).thenReturn(db);
PowerMockito.whenNew(GridFS.class).withArguments(db, collectionNameOSM).thenReturn(gridFS);
Mockito.when(gridFS.findOne(idApp)).thenReturn(gridFSFile);
Mockito.when(gridFS.createFile(file)).thenReturn(gridFSInputFile);
// Experimentations
importFilesRepositoryCustomImpl.saveFileOSM(idApp,file);
// Expectations
Mockito.verify(gridFS).remove(gridFSFile);
Mockito.verify(gridFS).createFile(file);
Mockito.verify(gridFSInputFile).setFilename(idApp);
Mockito.verify(gridFSInputFile).save();
}
@Test
public void saveFileOSMWithoutRemoving() throws Exception{
//Fixture
Mockito.when(mongoTemplate.getDb()).thenReturn(db);
PowerMockito.whenNew(GridFS.class).withArguments(db, collectionNameOSM).thenReturn(gridFS);
Mockito.when(gridFS.findOne(idApp)).thenReturn(null);
Mockito.when(gridFS.createFile(file)).thenReturn(gridFSInputFile);
// Experimentations
importFilesRepositoryCustomImpl.saveFileOSM(idApp,file);
// Expectations
Mockito.verify(gridFS).createFile(file);
Mockito.verify(gridFSInputFile).setFilename(idApp);
Mockito.verify(gridFSInputFile).save();
}
@Test
public void saveFileObj() throws Exception{
//Fixture
Mockito.when(mongoTemplate.getDb()).thenReturn(db);
PowerMockito.whenNew(GridFS.class).withArguments(db, collectionNameObj).thenReturn(gridFS);
Mockito.when(gridFS.findOne(idApp)).thenReturn(gridFSFile);
Mockito.when(gridFS.createFile(file)).thenReturn(gridFSInputFile);
// Experimentations
importFilesRepositoryCustomImpl.saveFileObj(idApp,file);
// Expectations
Mockito.verify(gridFS).remove(gridFSFile);
Mockito.verify(gridFS).createFile(file);
Mockito.verify(gridFSInputFile).setFilename(idApp);
Mockito.verify(gridFSInputFile).save();
}
@Test
public void saveFileObjWithoutRemoving() throws Exception{
//Fixture
Mockito.when(mongoTemplate.getDb()).thenReturn(db);
PowerMockito.whenNew(GridFS.class).withArguments(db, collectionNameObj).thenReturn(gridFS);
Mockito.when(gridFS.findOne(idApp)).thenReturn(null);
Mockito.when(gridFS.createFile(file)).thenReturn(gridFSInputFile);
// Experimentations
importFilesRepositoryCustomImpl.saveFileObj(idApp,file);
// Expectations
Mockito.verify(gridFS).createFile(file);
Mockito.verify(gridFSInputFile).setFilename(idApp);
Mockito.verify(gridFSInputFile).save();
}
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
// Get the necessary Mongo references
DB db = getDB(_session, argStruct);
GridFS gridfs = getGridFS(_session, argStruct, db);
// Get the file information
String filename = getNamedStringParam(argStruct, "filename", null);
if ( filename != null ){
return toArray( gridfs.find(filename) );
} else {
cfData mTmp = getNamedParam(argStruct, "query", null);
if ( mTmp != null )
return toArray( gridfs.find(getDBObject(mTmp)) );
}
throwException(_session, "Please specify file or a query");
return null;
}
private DBCursor createCursor(GridFS gridfs) {
if (spec.filter() != null) {
DBObject query = (DBObject) JSON.parse(spec.filter());
return gridfs.getFileList(query);
}
return gridfs.getFileList();
}
@Override
public List<? extends BoundedSource<ObjectId>> split(
long desiredBundleSizeBytes, PipelineOptions options) throws Exception {
Mongo mongo = spec.connectionConfiguration().setupMongo();
try {
GridFS gridfs = spec.connectionConfiguration().setupGridFS(mongo);
DBCursor cursor = createCursor(gridfs);
long size = 0;
List<BoundedGridFSSource> list = new ArrayList<>();
List<ObjectId> objects = new ArrayList<>();
while (cursor.hasNext()) {
GridFSDBFile file = (GridFSDBFile) cursor.next();
long len = file.getLength();
if ((size + len) > desiredBundleSizeBytes && !objects.isEmpty()) {
list.add(new BoundedGridFSSource(spec, objects));
size = 0;
objects = new ArrayList<>();
}
objects.add((ObjectId) file.getId());
size += len;
}
if (!objects.isEmpty() || list.isEmpty()) {
list.add(new BoundedGridFSSource(spec, objects));
}
return list;
} finally {
mongo.close();
}
}
@Override
public boolean start() throws IOException {
if (objects == null) {
mongo = source.spec.connectionConfiguration().setupMongo();
GridFS gridfs = source.spec.connectionConfiguration().setupGridFS(mongo);
cursor = source.createCursor(gridfs);
} else {
iterator = objects.iterator();
}
return advance();
}
public MongoGridFSSession(IMongoDataSourceAdapter dataSourceAdapter, String bucketName) throws Exception {
this.__id = UUIDUtils.UUID();
this.__dataSourceHolder = dataSourceAdapter;
this.__bucketName = StringUtils.defaultIfBlank(bucketName, GridFS.DEFAULT_BUCKET);
//
__gridFS = new GridFS(new DB(dataSourceAdapter.getMongoClient(), dataSourceAdapter.getDataSourceCfgMeta().getDatabaseName()), __bucketName);
__dbCollection = __gridFS.getDB().getCollection(__bucketName.concat(".files"));
}
public MongoService(String host, String dbName) {
try {
mFS = new GridFS(mDB = new MongoClient(host).getDB(dbName));
System.out.println(JSON.serialize(getCollectionNames()));
System.out.println(System.getProperty("user.dir"));
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
@GET
@Path("/{id}")
public Response getAttachment(@PathParam("id") final String id) throws IOException {
final GridFS gridFS = new GridFS(this.mongoLegacyGrid);
final GridFSDBFile file = gridFS.findOne(id);
// log.info(file);
if (file == null) {
throw new WebApplicationException(404);
}
return Response.ok(org.apache.commons.io.IOUtils.toByteArray(file.getInputStream()), file.getContentType()).build();
}
@Override
public InputStream getMapFileByAppId(String appIdentifier) throws MapFileNotExistsException{
// TODO Auto-generated method stub
GridFS gridFS = getGridFS(collectionNameMap);
InputStream fileMap=getMapFileByAppId(gridFS,appIdentifier);
return fileMap;
}
private InputStream getMapFileByAppId(GridFS gridFS,String appIdentifier) throws MapFileNotExistsException{
InputStream fileMap=null;
GridFSDBFile gridFSFileMap = gridFS.findOne(appIdentifier);
if(gridFSFileMap==null){
throw new MapFileNotExistsException();
}
fileMap=gridFSFileMap.getInputStream();
return fileMap;
}
@Given("^I have a map file with appId \"([^\"]*)\"$")
public void I_a_map_with_appId(String appId) throws IOException{
// Express the Regexp above with the code you wish you had
File mapFile = new File("src/acceptance-test/resources/maps/background_" + appId +".map");
GridFS gridFS = getGridFS(collectionNameMap);
removeFile(appId,gridFS);
saveFile(appId,mapFile,gridFS);
}
@Test(expected=MapFileNotExistsException.class)
public void getFileMapByAppIdWithoutMapFile() throws Exception{
//Fixture
Mockito.when(mongoTemplate.getDb()).thenReturn(db);
PowerMockito.whenNew(GridFS.class).withArguments(db,collectionNameMap).thenReturn(gridFS);
Mockito.when(gridFS.findOne(idApp)).thenReturn(null);
// Experimentations
mapFileRepositoryCustomImpl.getMapFileByAppId(idApp);
// Expectations
Mockito.verify(gridFS).findOne(idApp);
}
@Given("^I have a map file with APPID \"([^\"]*)\"$")
public void I_a_map_with_APPID(String appId) throws IOException{
// Express the Regexp above with the code you wish you had
File mapFile = new File("src/acceptance-test/resources/maps/background_" + appId +".map");
GridFS gridFS = getGridFS(collectionNameMap);
removeFile(appId,gridFS);
saveFile(appId,mapFile,gridFS);
}
@Test(expected=MapFileNotExistsException.class)
public void getFileMapByAppIdWithoutMapFile() throws Exception{
//Fixture
Mockito.when(mongoTemplate.getDb()).thenReturn(db);
PowerMockito.whenNew(GridFS.class).withArguments(db,collectionNameMap).thenReturn(gridFS);
Mockito.when(gridFS.findOne(idApp)).thenReturn(null);
// Experimentations
mapFileRepositoryCustomImpl.getMapFileByAppId(idApp);
// Expectations
Mockito.verify(gridFS).findOne(idApp);
}
@Override
public void saveFileMap(String appIdentifier, File map) throws IOException{
// TODO Auto-generated method stub
GridFS gridFS = getGridFS(collectionNameMap);
removeFile(appIdentifier,gridFS);
saveFile(appIdentifier,map,gridFS);
}
@Override
public void saveFileOSM(String appIdentifier, File osm) throws IOException{
// TODO Auto-generated method stub
GridFS gridFS = getGridFS(collectionNameOSM);
removeFile(appIdentifier,gridFS);
saveFile(appIdentifier,osm,gridFS);
}
@Override
public void saveFileObj(String appIdentifier, File obj) throws IOException{
// TODO Auto-generated method stub
GridFS gridFS = getGridFS(collectionNameObj);
removeFile(appIdentifier,gridFS);
saveFile(appIdentifier,obj,gridFS);
}
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
// Get the necessary Mongo references
DB db = getDB(_session, argStruct);
GridFS gridfs = getGridFS(_session, argStruct, db);
// Get the file information
String filename = getNamedStringParam(argStruct, "filename", null);
if ( filename != null ){
return toStruct( gridfs.findOne(filename) );
} else {
String _id = getNamedStringParam(argStruct, "_id", null);
if ( _id != null ){
return toStruct( gridfs.findOne( new ObjectId(_id) ) );
} else {
cfData mTmp = getNamedParam(argStruct, "query", null);
if ( mTmp != null )
return toStruct( gridfs.findOne(getDBObject(mTmp)) );
}
}
throwException(_session, "Please specify file, _id or a query");
return null;
}
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
// Get the necessary Mongo references
DB db = getDB(_session,argStruct);
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;
}
protected GridFS getGridFS(cfSession _session, cfArgStructData argStruct, DB db ) throws cfmRunTimeException {
String bucket = getNamedStringParam(argStruct, "bucket", null);
if ( bucket == null )
throwException(_session, "please specify a bucket");
return new GridFS(db, bucket);
}