LiteDB是.NET平台上的无需服务端(serverless)的文档数据库。 LiteDB.Studio是相应的数据库管理工具。

https://www.litedb.org/docs/

Getting Started

dotnet new console -o p1
cd p1
dotnet add package LiteDB 

为了能够运行实例代码,

  • 开头加上using LiteDB;
  • csproj中把nullable设为disable
  • 把top-level代码移到类定义之前

Data Structure

存储的标的为文档,文档结构为键值对,有点像JSON的对象。 文档按合集组织。合集是一组关联的文档,有一个共享的索引集合,此点类似于关系数据库。

实际存储的结构是BSON,即Binary JSON,再辅之以额外的信息。 仅部分BSON的数据结构受到支持:

  • MinValue
  • Null
  • Int32/64 (System.Int32/64)
  • Double (System.Double)
  • Decimal (System.Decimal)
  • String (System.String)
  • Document (System.Collection.Generic.Dictionary<string, BsonValue>)
  • Array (System.Collection.Generic.List<BsonValue>)
  • Binary (System.Byte[])
  • ObjectID (LiteDB.ObjectId)
  • Guid (System.Guid)
  • Boolean (System.Boolean)
  • DateTime (System.DateTime)
  • MaxValue

从BSON到JSON,需要对JSON进行扩展,例如:

  • ObjectId { "$oid": "507f1f55bcf96cd799438110" }

LiteDB implements JSON in its JsonSerializer static class. If you want to convert your object type to a BsonValue, you must use a BsonMapper. JsonSerialize also supports TextReader and TextWriter to read/write directly from a file or Stream

ObjectId

  • 12字节BSON类型,其中包括
    • 4字节Timestamp(seconds since the Unix epoch)
    • 3字节Machine(Machine Identifier)
    • 2字节Pid(Process Id)

Object Mapping

BsonMapper 将POCO类们映射成BsonDocument。

LiteDatabase.GetCollection<T>可以获取到ILiteCollection<T> ,例如:

var typedCustomerCollection = db.GetCollection<Customer>("customer");

var schemelessCollection = db.GetCollection("customer"); // <T> is BsonDocument

……

Starting with version 5 of LiteDB you can use BsonCtorAttribute to indicate which constructor the mapper must use.

You can register your own map function, using the RegisterType instance method.

……

Collections

LiteCollection用于处理文档合集。每个合集有特定的名字,按一定规则来命名。

系统合集包括:

  • $cols,列出所有合集
  • $database,数据库常见信息
  • $indexes,列出所有索引
  • $sequences,列出所有序列
  • $transactions,列出所有开启的事务
  • $snapshots
  • $open_cursors
  • $dump(pageID)
  • $page_list(pageID)
  • $query(subquery),玩票的
  • $file(path),读写外部文件

BsonDocument

BsonDocument类标实现了文档的概念,把文档中的键值对保存到Dictionary<string, BsonValue>

LiteDB supports documents up to 16MB after BSON serialization.

……

Expressions

Expressions are path or formulas to access and modify the data inside a document. Based on the concept of JSON path (http://goessner.net/articles/JsonPath/), LiteDB supports a similar syntax to navigate inside a document.

……

DbRef

LiteDB is a document database, so there is no JOIN between collections. You can use embedded documents (sub-documents) or create a reference between collections. To create a reference you can use [BsonRef] attribute or use theDbRef method from the fluent API mapper.

Connection String

LiteDatabase can be initialized using a string connection, with key1=value1; key2=value2; … syntax. If there is no = in your connection string, LiteDB assume that your connection string contains only the Filename. Values can be quoted (" or ‘) if they contain special characters (like ; or =). Keys and values are case-insensitive.

FileStorage

文档的大小限制在1MB。这可能太小,于是乎可以使用FileStorage,一个自定义的合集,用来存储文件和数据流。

Indexes

通过对文档字段或表达式进行索引来提高检索性能。索引采用skip list实现,也就是32级别的双向链表。因为合集是没有廓图(schema)的,同一个查询对不同文档会返回不同的数据类型。

Encryption

使用盐化的AES(RFC2898)做加密。按Rfc2898DeriveBytes 类标实现。

Pragmas

pragmas是一些变量,可以更改数据文件的行为。

Collation

特定标码的合集,让用户可以为数据文件指定culture和string compare选项。

特别之处在于,collation是只读的。想修改的话,必须重建数据文件。

https://www.litedb.org/api/

  • SELECT
  • INSERT
  • DELETE
  • MISC
  • Functions
    • Aggregate Functions
    • DataType Functions
    • Date Functions
    • Math Functions
    • String Functions
    • Misc Functions
  • Interfaces and Classes
    • LiteDatabase

(终止线)