MongoDB - GridFS源碼分析

來源:互聯網
上載者:User

The database supports native storage of binary data within BSON objects.  However, BSON objects in MongoDB are limited in size (4MB older versions, 16MB in v1.7/1.8, higher limits in the future). The GridFS spec provides a mechanism for transparently dividing a large file among multiple documents. This allows us to efficiently store large objects, and in the case of especially large files, such as videos, permits range operations (e.g., fetching only the first N bytes of a file).

這是官網的一段GridFS的介紹. GridFS是提供了把一個大檔案切分成多個document來儲存的. 向GridFS中插入一個檔案, 預設使用fs.files和fs.chunks兩個collection來儲存此檔案的資訊的, 其中fs.files存放了檔案的資訊, fs.chunks存放了檔案的資料. 

  1. > db.fs.files.findOne()  
  2. {  
  3. "_id" : ObjectId("4fbfae0ad417a4f2bc5bc6d1"),  
  4. "filename" : "./Makefile",      //檔案名稱   
  5. "chunkSize" : 262144,           //chunk的大小, 是固定的, 預設為256*1024   
  6. "uploadDate" : ISODate("2012-05-25T16:06:34.794Z"), //上傳日期   
  7. "md5" : "f9eae9d5987644a537862ca3707ff59d", //檔案的md5值   
  8. "length" : 130  //檔案的長度   
  9. }  
  10.   
  11. > db.fs.chunks.findOne()  
  12. {  
  13. "_id" : ObjectId("4fbfae0a4d460742f1aa76dc"),  
  14. "files_id" : ObjectId("4fbfae0ad417a4f2bc5bc6d1"), //對應fs.files中的_id   
  15. "n" : 0, //檔案的第幾個chunk,這裡要注意 如果檔案大於fs.files中的chunkSize則進行分塊, 從0計數   
  16. "data" : BinData(0,"QWxsOgoJZysrIG1haW4uY3BwIC1ML3Vzci9sb2NhbC9saWIvIC1JL3Vzci9sb2NhbC9pbmNsdWRlIC1sbW9uZ29jbGllbnQg  
  17. LWxib29zdF90aHJlYWQgLWxib29zdF9maWxlc3lzdGVtIC1sYm9vc3RfcHJvZ3JhbV9vcHRpb25zCg==") //檔案的二進位流   
  18. }  
下面看具體測試代碼:
  1. int main(int argc, const char** argv)   
  2. {  
  3.     DBClientConnection pConn;  
  4.     pConn.connect("10.15.107.154:20000");  
  5.   
  6.   
  7.     GridFS* pGridFs = new GridFS(pConn, "TestGF");   
  8.   
  9. #if 1    
  10.     ///< 隱藏檔   
  11.     pGridFs->storeFile("./Makefile");  
  12. #endif   
  13.   
  14.   
  15. #if 0   
  16.     ///< 遍曆檔案   
  17.     auto_ptr<DBClientCursor> cursor = pGridFs->list();  
  18.     if (cursor->more()) {  
  19.         BSONObj obj = cursor->next();  
  20.         cout<<obj.toString().data()<<endl;;  
  21.     }  
  22. #endif   
  23.   
  24.   
  25. #if 0   
  26.     ///< 讀取檔案   
  27.     GridFile file = pGridFs->findFile("./Makefile");  
  28.     file.write("./hello");  
  29. #endif    
  30.   
  31.   
  32. #if 0   
  33.     ///< 刪除檔案   
  34.     pGridFs->removeFile("./main.cpp");  
  35. #endif   
  36.   
  37.   
  38.     return 0;  
  39. }  
  • 1
  • 2
  • 下一頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.