类com.mongodb.gridfs.GridFS源码实例Demo

下面列出了怎么用com.mongodb.gridfs.GridFS的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: pampas   文件: MongoTest.java
@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());
}
 
源代码2 项目: beam   文件: MongoDbGridFSIO.java
@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();
  }
}
 
源代码3 项目: XBDD   文件: ImageDao.java
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;
	}
}
 
源代码4 项目: osiris   文件: MapFileRepositoryCustoImplTest.java
@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);
}
 
源代码5 项目: osiris   文件: MapFileRepositoryCustoImplTest.java
@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();
}
 
源代码12 项目: openbd-core   文件: Find.java
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;
}
 
源代码13 项目: beam   文件: MongoDbGridFSIO.java
private DBCursor createCursor(GridFS gridfs) {
  if (spec.filter() != null) {
    DBObject query = (DBObject) JSON.parse(spec.filter());
    return gridfs.getFileList(query);
  }
  return gridfs.getFileList();
}
 
源代码14 项目: beam   文件: MongoDbGridFSIO.java
@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();
  }
}
 
源代码15 项目: beam   文件: MongoDbGridFSIO.java
@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();
}
 
源代码16 项目: ymate-platform-v2   文件: MongoGridFSSession.java
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"));
}
 
源代码17 项目: BLELocalization   文件: MongoService.java
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();
	}
}
 
源代码18 项目: XBDD   文件: Attachment.java
@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();

}
 
源代码19 项目: osiris   文件: MapFileRepositoryCustomImpl.java
@Override
public InputStream getMapFileByAppId(String appIdentifier) throws MapFileNotExistsException{
	// TODO Auto-generated method stub
	GridFS gridFS = getGridFS(collectionNameMap);
	InputStream fileMap=getMapFileByAppId(gridFS,appIdentifier);
	return fileMap;
}
 
源代码20 项目: osiris   文件: MapFileRepositoryCustomImpl.java
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;
}
 
源代码21 项目: osiris   文件: GetMapFile.java
@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);				
}
 
源代码22 项目: osiris   文件: MapFileRepositoryCustoImplTest.java
@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);
}
 
源代码23 项目: osiris   文件: GetMapFile.java
@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);
	
}
 
源代码24 项目: osiris   文件: MapFileRepositoryCustoImplTest.java
@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);
}
 
源代码25 项目: osiris   文件: ImportFilesRepositoryCustomImpl.java
@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);
}
 
源代码26 项目: osiris   文件: ImportFilesRepositoryCustomImpl.java
@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);
}
 
源代码27 项目: osiris   文件: ImportFilesRepositoryCustomImpl.java
@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);
}
 
源代码28 项目: openbd-core   文件: FindOne.java
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;
}
 
源代码29 项目: openbd-core   文件: Delete.java
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;
}
 
源代码30 项目: openbd-core   文件: Add.java
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);
}
 
 类所在包
 类方法
 同包方法