如果是測試 Database 我也很好奇,LiteDB 是怎麼處理大型檔案格式的,看了一下
你將大檔案寫入該資料庫中,他會規畫一區讓你放檔案,有一張表負責記錄你放入啥檔案,另外一個 他稱之為 chunk 的
主要是會把檔案切割,每 255KB 切一段

今天來測試一下,寫入一個 16.1 MB 的大圖進去,他會怎麼規劃
首先我先用預設的寫法,非常簡單
using (var db = new LiteDatabase(AppDomain.CurrentDomain.BaseDirectory + "sample.db"))
{
var fileStorage = db.FileStorage;
//讀取根目錄的檔案 16.1M.png 存檔到資料庫中為 $/photos/2024_01/sample.png
fileStorage.Upload("$/photos/2024_01/sample1.png", AppDomain.CurrentDomain.BaseDirectory + "16.1M.png");
//讀取出來
var newFile = fileStorage.FindById("$/photos/2024_01/sample1.png");
然後輸出成 output.png
newFile.SaveAs(AppDomain.CurrentDomain.BaseDirectory + "output.png");
}
這時候我們用 GUI 看,他會開兩張表一張是 _files 這邊就是紀錄檔案的名冊,再來有一個是 _chunks 這邊就是
該大檔被切割後的資料


所以其實我們可以透過程式的部分自己去規劃那兩張表的名稱
using (var db = new LiteDatabase(AppDomain.CurrentDomain.BaseDirectory + "sample.db"))
{
//_files=> SAMPLE_FS
//_chunks=> SAMPLE_CHUNK
var fileStorage = db.GetStorage("SAMPLE_FS", "SAMPLE_CHUNK");
fileStorage.Upload("$/photos/2024_01/sample1.png", AppDomain.CurrentDomain.BaseDirectory + "16.1M.png");
var newFile = fileStorage.FindById("$/photos/2024_01/sample1.png");
newFile.SaveAs(AppDomain.CurrentDomain.BaseDirectory + "output2.png");
}

畢竟在 _files 中有造冊,所以也可以透過程式的部分將檔案資訊讀取出來
using (var db = new LiteDatabase(AppDomain.CurrentDomain.BaseDirectory + "sample.db"))
{
var coll = db.GetCollection("SAMPLE_FS");
var imageData = coll.FindById("$/photos/2024_01/sample1.png");
Console.WriteLine(JsonConvert.SerializeObject(BsonMapper.Global.Deserialize>(imageData)));
}
//Result:
/*
{"Id":"$/photos/2024_01/sample1.png","Filename":"16.1M.png","MimeType":"image/png","Length":16920156,"Chunks":129,
"UploadDate":"2024-01-30T15:23:42.755+08:00","Metadata":{}}
*/
先筆記到這,其實在現實生活中,除非你有大量破碎檔案可以透過這方式一次管理
其實大檔案圖片,基本上也會讓他是獨立存在,或是使用 Azure
Blob Storage
reference:
https://www.litedb.org/docs/filestorage/