List of usage examples for com.mongodb.gridfs GridFS GridFS
public GridFS(final DB db, final String bucket)
From source file:UnitTest3.java
License:Open Source License
public static int testgridfs() { long time1;/*from w w w . j av a2 s . c om*/ long time2; long time3; long time4; try { MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("JFileDB"); // TODO JFileMetaDataTable should be in MetaDB database DBCollection collection = db.getCollection("JFileMetaDataTable"); String newFileName = "com.dilmus.scabi.testdata.in.App.class"; File jFile = new File("/home/anees/workspace/testdata/in/App.class"); // create a JFileTable namespace GridFS gfsj = new GridFS(db, "JFileTable"); // get file from local drive GridFSInputFile gfsFile = gfsj.createFile(jFile); // set a new filename for identify purpose gfsFile.setFilename(newFileName); gfsFile.setContentType("class"); // jar, zip, war // save the image file into mongoDB gfsFile.save(); // Let's create a new JSON document with some "metadata" information BasicDBObject info = new BasicDBObject(); info.put("DBHost", "localhost"); info.put("DBPort", "27017"); info.put("JFileName", newFileName); info.put("JFileID", gfsFile.getId()); info.put("JFileMD5", gfsFile.getMD5()); collection.insert(info, WriteConcern.ACKNOWLEDGED); // print the result DBCursor cursor = gfsj.getFileList(); while (cursor.hasNext()) { System.out.println(cursor.next()); } DBCursor cursor2 = collection.find(); while (cursor2.hasNext()) { System.out.println(cursor2.next()); } // get file by it's filename GridFSDBFile jForOutput = gfsj.findOne(newFileName); // save it into a new image file jForOutput.writeTo("/home/anees/workspace/testdata/out/AppOut.class"); // remove the file from mongoDB // gfsj.remove(gfsj.findOne(newFileName)); System.out.println("Done"); mongo.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return 0; }
From source file:calliope.db.MongoConnection.java
License:Open Source License
/** * List all the documents in a Mongo collection * @param collName the name of the collection * @return a String array of document keys * @throws AeseException // w w w . j a va 2 s . co m */ @Override public String[] listCollection(String collName) throws AeseException { if (!collName.equals(Database.CORPIX)) { try { connect(); } catch (Exception e) { throw new AeseException(e); } DBCollection coll = getCollectionFromName(collName); BasicDBObject keys = new BasicDBObject(); keys.put(JSONKeys.DOCID, 1); DBCursor cursor = coll.find(new BasicDBObject(), keys); System.out.println("Found " + cursor.count() + " documents"); cursor.count(); if (cursor.length() > 0) { String[] docs = new String[cursor.length()]; Iterator<DBObject> iter = cursor.iterator(); int i = 0; while (iter.hasNext()) docs[i++] = (String) iter.next().get(JSONKeys.DOCID); return docs; } else { return new String[0]; } } else { GridFS gfs = new GridFS(db, collName); DBCursor curs = gfs.getFileList(); int i = 0; List<DBObject> list = curs.toArray(); HashSet<String> set = new HashSet<String>(); Iterator<DBObject> iter = list.iterator(); while (iter.hasNext()) { String name = (String) iter.next().get("filename"); set.add(name); } String[] docs = new String[set.size()]; set.toArray(docs); return docs; } }
From source file:calliope.db.MongoConnection.java
License:Open Source License
/** * Get an image from the database//w ww . j a va 2s .co m * @param collName the collection name * @param docID the docid of the corpix * @return the image data */ @Override public byte[] getImageFromDb(String collName, String docID) { try { connect(); GridFS gfs = new GridFS(db, collName); GridFSDBFile file = gfs.findOne(docID); if (file != null) { InputStream ins = file.getInputStream(); long dataLen = file.getLength(); // this only happens if it is > 2 GB if (dataLen > Integer.MAX_VALUE) throw new AeseException("file too big (size=" + dataLen + ")"); byte[] data = new byte[(int) dataLen]; int offset = 0; while (offset < dataLen) { int len = ins.available(); offset += ins.read(data, offset, len); } return data; } else throw new FileNotFoundException(docID); } catch (Exception e) { e.printStackTrace(System.out); return null; } }
From source file:calliope.db.MongoConnection.java
License:Open Source License
/** * Store an image in the database//from w w w . j a v a2 s . c om * @param collName name of the image collection * @param docID the docid of the resource * @param data the image data to store * @throws AeseException */ @Override public void putImageToDb(String collName, String docID, byte[] data) throws AeseException { docIDCheck(collName, docID); GridFS gfs = new GridFS(db, collName); GridFSInputFile file = gfs.createFile(data); file.setFilename(docID); file.save(); }
From source file:calliope.db.MongoConnection.java
License:Open Source License
/** * Delete an image from the database//from w ww . j a v a 2 s . co m * @param collName the collection name e.g. "corpix" * @param docID the image's docid path * @throws AeseException */ @Override public void removeImageFromDb(String collName, String docID) throws AeseException { try { GridFS gfs = new GridFS(db, collName); GridFSDBFile file = gfs.findOne(docID); if (file == null) throw new FileNotFoundException("file " + collName + "/" + docID + " not found"); gfs.remove(file); } catch (Exception e) { throw new AeseException(e); } }
From source file:calliope.db.MongoConnection.java
License:Open Source License
/** * Test a Mongo connection by putting, deleting, getting a JSON * file and an image./*from w w w .j a va2s .c om*/ * @return a String indicating how many tests succeeded */ @Override public String test() { StringBuilder sb = new StringBuilder(); try { byte[] imageData = Base64.decodeBase64(testImage); connect(); String response = putToDb("test", "data/text", testJson); if (checkResponse(response)) { response = getFromDb("test", "data/text"); if (!checkResponse(response)) sb.append("failed put/get test for plain json\n"); else { response = removeFromDb("test", "data/text"); if (!checkResponse(response)) sb.append("failed to remove plain json\n"); } } putImageToDb("corpix", "data/image", imageData); byte[] data = getImageFromDb("corpix", "data/image"); if (data == null || data.length != imageData.length) sb.append("failed put/get test for image\n"); else removeImageFromDb("corpix", "data/image"); DBObject query = new BasicDBObject(JSONKeys.DOCID, "data/image"); GridFS gfs = new GridFS(db, "corpix"); GridFSDBFile file = gfs.findOne(query); if (file != null) sb.append("removed failed for image\n"); } catch (Exception e) { e.printStackTrace(System.out); } return sb.toString(); }
From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java
License:Apache License
protected GridFSDBFile readFile(String storageKey, String tableName) { DB db = options.getCollection(TABLE_TEMP_KEY).getDB(); DBObject query = new BasicDBObject(); query.put(FIELD_STORAGE_KEY, new ObjectId(storageKey)); GridFSDBFile gfsFile = new GridFS(db, tableName).findOne(query); return gfsFile; }
From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java
License:Apache License
protected GridFSDBFile readFile(int appid, int docid, String vid, String tableName) { DB db = options.getCollection(TABLE_TEMP_KEY).getDB(); db.requestStart();/* w ww .java2 s . c om*/ DBObject query = new BasicDBObject(); query.put(FIELD_DOC_ID, docid); query.put(FILED_VERSION_ID, vid); query.put(FIELD_APP_ID, appid); GridFSDBFile gfsFile = new GridFS(db, tableName).findOne(query); db.requestEnsureConnection(); db.requestDone(); return gfsFile; }
From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java
License:Apache License
protected GridFSDBFile readTrivialFile(String spaceName, String fileName, String tableName) { DB db = options.getCollection(TABLE_TEMP_KEY).getDB(); DBObject query = new BasicDBObject(); query.put(FIELD_SPACE_NAME, new ObjectId(spaceName)); query.put(FIELD_FILE_NAME, fileName); GridFSDBFile gfsFile = new GridFS(db, tableName).findOne(query); return gfsFile; }
From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java
License:Apache License
protected void writeFile(InputStream ins, MFile mf, String tableName) { DB db = options.getCollection(TABLE_TEMP_KEY).getDB(); db.requestStart();//from ww w . ja va 2s. c o m GridFSInputFile gfsInput; gfsInput = new GridFS(db, tableName).createFile(ins); DBCollection col = db.getCollection(tableName + ".files"); col.setWriteConcern(WriteConcern.SAFE); gfsInput.setFilename(mf.getFilename()); gfsInput.put(FIELD_STORAGE_KEY, mf.getStorageKey()); gfsInput.put(FIELD_DOC_ID, mf.getDocid()); gfsInput.put(FILED_VERSION_ID, mf.getVid()); gfsInput.put(FIELD_APP_ID, mf.getAppid()); gfsInput.put(FILED_IS_PUB, getIsPubStatus(mf.getIsPub())); gfsInput.setContentType(mf.getContentType()); gfsInput.save(); db.requestDone(); }