如何在LiteDB数据库中合理存储元数据?求更优实现方案
LiteDB数据库元数据存储优化方案
现有方案概述
我正在使用独立LiteDB数据库,需要为每个数据库添加额外元数据。当前采用仅含单个对象的专用集合存储,代码如下:
定义DBInfo类
internal class DBInfo { public int Id { get; set; } public Guid UId { get; set; } = Guid.NewGuid(); public string Source { get; set; } = string.Empty; public DateTime DTCreated { get; set; } = DateTime.UtcNow; }
创建数据库时插入元数据
_db = new LiteDatabase(dbPath); var dbInfoCollection = _db.GetCollection<DBInfo>(); var dbInfo = new DBInfo(); dbInfo.Source = "company 1"; dbInfoCollection.Insert(dbInfo); dbInfoCollection.Update(dbInfo); _db.Commit();
读取元数据
_db = new LiteDatabase(dbPath); var dbInfo = _db.GetCollection<DBInfo>().FindAll().Single(); Console.WriteLine($"Source of current database: {dbInfo.Source}");
该方案可正常运行,但不够理想,希望找到更合理的实现方式。
优化实现方案
1. 使用内置UserVersion属性
LiteDB提供数据库级别的UserVersion整数字段,适合存储简单版本标识类信息:
设置UserVersion
_db = new LiteDatabase(dbPath); _db.Pragma.UserVersion = 1; // 用整数存储版本或简单编码信息 _db.Commit();
读取UserVersion
_db = new LiteDatabase(dbPath); var version = _db.Pragma.UserVersion; Console.WriteLine($"Database version: {version}");
2. 简化专用集合方案(固定单文档)
对现有方案优化,通过固定ID确保集合仅存一个文档,避免Single()查询的潜在问题:
调整DBInfo类(固定ID)
internal class DBInfo { public int Id { get; set; } = 1; // 固定ID,保证唯一文档 public Guid UId { get; set; } = Guid.NewGuid(); public string Source { get; set; } = string.Empty; public DateTime DTCreated { get; set; } = DateTime.UtcNow; }
插入/更新元数据
_db = new LiteDatabase(dbPath); var dbInfoCollection = _db.GetCollection<DBInfo>("DBInfo"); var dbInfo = dbInfoCollection.FindById(1); if (dbInfo == null) { dbInfo = new DBInfo { Source = "company 1" }; dbInfoCollection.Insert(dbInfo); } else { dbInfo.Source = "updated company"; dbInfoCollection.Update(dbInfo); } _db.Commit();
读取元数据
_db = new LiteDatabase(dbPath); var dbInfo = _db.GetCollection<DBInfo>("DBInfo").FindById(1); if (dbInfo != null) { Console.WriteLine($"Source: {dbInfo.Source}"); }
3. 用Pragma.Custom存储序列化元数据
利用Pragma.Custom字段存储任意字符串,可序列化复杂元数据结构:
设置自定义元数据
_db = new LiteDatabase(dbPath); var dbInfo = new DBInfo { Source = "company 1" }; var json = System.Text.Json.JsonSerializer.Serialize(dbInfo); _db.Pragma.Custom = json; _db.Commit();
读取自定义元数据
_db = new LiteDatabase(dbPath); var json = _db.Pragma.Custom; if (!string.IsNullOrEmpty(json)) { var dbInfo = System.Text.Json.JsonSerializer.Deserialize<DBInfo>(json); Console.WriteLine($"Source: {dbInfo.Source}"); }
4. 利用FileStorage存储元数据文件
若元数据需和其他资源关联,可使用LiteDB的文件存储功能:
存储元数据文件
_db = new LiteDatabase(dbPath); var dbInfo = new DBInfo { Source = "company 1" }; var json = System.Text.Json.JsonSerializer.Serialize(dbInfo); using var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)); _db.FileStorage.Upload("metadata/dbinfo.json", "DB Metadata", ms); _db.Commit();
读取元数据文件
_db = new LiteDatabase(dbPath); var file = _db.FileStorage.FindById("metadata/dbinfo.json"); if (file != null) { using var ms = new MemoryStream(); file.CopyTo(ms); ms.Position = 0; var json = new StreamReader(ms).ReadToEnd(); var dbInfo = System.Text.Json.JsonSerializer.Deserialize<DBInfo>(json); Console.WriteLine($"Source: {dbInfo.Source}"); }
内容的提问来源于stack exchange,提问作者Spark13
相关产品推荐
相关产品推荐

