com.mongodb.gridfs.GridFS Java Examples

The following examples show how to use com.mongodb.gridfs.GridFS. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoTest.java    From pampas with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #2
Source File: ImportFilesRepositoryCustomImplTest.java    From osiris with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #3
Source File: ImportFilesRepositoryCustomImplTest.java    From osiris with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #4
Source File: ImportFilesRepositoryCustomImplTest.java    From osiris with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #5
Source File: ImportFilesRepositoryCustomImplTest.java    From osiris with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #6
Source File: ImportFilesRepositoryCustomImplTest.java    From osiris with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #7
Source File: ImportFilesRepositoryCustomImplTest.java    From osiris with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #8
Source File: MapFileRepositoryCustoImplTest.java    From osiris with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #9
Source File: Find.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #10
Source File: MapFileRepositoryCustoImplTest.java    From osiris with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #11
Source File: ImageDao.java    From XBDD with Apache License 2.0 6 votes vote down vote up
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;
	}
}
 
Example #12
Source File: MongoDbGridFSIO.java    From beam with Apache License 2.0 6 votes vote down vote up
@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();
  }
}
 
Example #13
Source File: MongoService.java    From BLELocalization with MIT License 5 votes vote down vote up
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();
	}
}
 
Example #14
Source File: MongoDbGridFSIO.java    From beam with Apache License 2.0 5 votes vote down vote up
private DBCursor createCursor(GridFS gridfs) {
  if (spec.filter() != null) {
    DBObject query = (DBObject) JSON.parse(spec.filter());
    return gridfs.getFileList(query);
  }
  return gridfs.getFileList();
}
 
Example #15
Source File: MongoDbGridFSIO.java    From beam with Apache License 2.0 5 votes vote down vote up
@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();
  }
}
 
Example #16
Source File: MongoDbGridFSIO.java    From beam with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #17
Source File: FindOne.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #18
Source File: ImportFilesRepositoryCustomImpl.java    From osiris with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #19
Source File: ImportFilesRepositoryCustomImpl.java    From osiris with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #20
Source File: ImportFilesRepositoryCustomImpl.java    From osiris with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #21
Source File: MapFileRepositoryCustoImplTest.java    From osiris with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #22
Source File: Delete.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #23
Source File: MongoGridFSSession.java    From ymate-platform-v2 with Apache License 2.0 5 votes vote down vote up
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"));
}
 
Example #24
Source File: Add.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example #25
Source File: GetMapFile.java    From osiris with Apache License 2.0 5 votes vote down vote up
@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);
	
}
 
Example #26
Source File: MapFileRepositoryCustoImplTest.java    From osiris with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #27
Source File: GetMapFile.java    From osiris with Apache License 2.0 5 votes vote down vote up
@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);				
}
 
Example #28
Source File: MapFileRepositoryCustomImpl.java    From osiris with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #29
Source File: BuguFS.java    From bugu-mongo with Apache License 2.0 5 votes vote down vote up
public BuguFS(String connectionName, String bucket, int chunkSize){
    this.bucket = bucket;
    this.chunkSize = chunkSize;
    DB db = BuguFramework.getInstance().getConnection(connectionName).getDB();
    fs = new GridFS(db, bucket);
    files = db.getCollection(bucket + ".files");
    //ensure the DBCursor can be cast to GridFSDBFile
    files.setObjectClass(GridFSDBFile.class);
}
 
Example #30
Source File: MapFileRepositoryCustomImpl.java    From osiris with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream getMapFileByAppId(String appIdentifier) throws MapFileNotExistsException{
	// TODO Auto-generated method stub
	GridFS gridFS = getGridFS(collectionNameMap);
	InputStream fileMap=getMapFileByAppId(gridFS,appIdentifier);
	return fileMap;
}