0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-08 19:11:08 +01:00
construct/include/ircd/db
Jason Volk 9f95c3233b ircd::db: Update defaults.
ircd:Ⓜ️:dbs: Add custom sizes for _event_json.
2019-08-26 22:40:13 -07:00
..
cache.h ircd::db: Use rvalue refs here. 2019-04-12 09:36:53 -07:00
cell.h ircd::db: Defriend row / cell related. 2019-01-24 10:53:07 -08:00
column.h ircd: Use bool return values through prefetching stack. 2019-08-19 21:18:26 -07:00
compactor.h ircd::db: Minor cleanup; comments. 2018-12-13 13:44:37 -08:00
comparator.h ircd::db: Support future CanKeysWithDifferentByteContentsBeEqual feature. 2018-10-31 11:25:07 -07:00
database.h ircd::db: Add write-ahead-log recovery callback surface. 2019-04-20 14:30:42 -07:00
db.h ircd::db: Move some internal headers out of the installed includedir. 2019-07-20 18:42:15 -07:00
delta.h ircd::db: minor cleanup: move this here. 2018-02-07 23:15:17 -08:00
descriptor.h ircd::db: Update defaults. 2019-08-26 22:40:13 -07:00
domain.h ircd::db: Rename db::index to db::domain; fix userspace. 2019-06-11 12:57:52 -07:00
error.h ircd::db: Simplify/Consolidate error hierarchy. 2018-12-24 13:32:22 -08:00
json.h ircd: Fix cast-conversion based reference-constructions. 2019-06-22 17:36:42 -06:00
merge.h Update Copyrastafaris. 2018-02-05 21:24:34 -08:00
opts.h ircd::db: Update / cleanup / comment various opts related. 2018-12-28 10:55:57 -08:00
pos.h ircd::db: Split out header for pos.h from main db.h 2018-12-03 12:20:55 -08:00
prefix_transform.h ircd::db: Rename prefix.h to prefix_transform.h 2019-06-11 12:47:43 -07:00
README.md ircd::db: Add organization section to README. 2019-02-04 13:59:01 -08:00
rocksdb.h ircd::db: Move some internal headers out of the installed includedir. 2019-07-20 18:42:15 -07:00
row.h ircd::db: Minor move attribute. 2019-05-08 05:19:04 -07:00
snapshot.h ircd::db: Move some internal headers out of the installed includedir. 2019-07-20 18:42:15 -07:00
sst.h ircd::db: Move some internal headers out of the installed includedir. 2019-07-20 18:42:15 -07:00
stats.h ircd::db: Enable histogram interface; partial data tally. 2018-09-25 22:18:37 -07:00
txn.h ircd::db::txn: Add template value-returning getters. 2019-05-09 21:45:17 -07:00
wal.h ircd::db: Move some internal headers out of the installed includedir. 2019-07-20 18:42:15 -07:00

Database

Organization

The database subsystem is split into three interfaces.

  • IRCd developer interface. These classes are for developers to simply use the database. An overview of them is given below. They do not require RocksDB headers and expose no RocksDB internals. These interfaces are included in the standard include stack. The headers are readily found listed in <ircd/db.h>. The definitions are in ircd/db.cc.

  • Database developer interface. These classes implement various RocksDB virtual interfaces. They are all declared as nested classes ofircd::db::database Most of these are not used directly by general users of the database; though they are technically frontend interfaces to RocksDB. They help us create the IRCd developer interface in ircd/db.cc. Headers are in <ircd/db/database/*> but most are not included in the standard include stack.

  • Database environment interface. These classes implement additional RocksDB virtual interfaces for backend callbacks operations. We connect RocksDB to ircd::fs and ircd::ctx et al on this surface. Headers are in <ircd/db/database/env/*> and are never included in the standard include stack. Definitions are in a separate unit db_env.cc.

  • Database port interface (bonus). This hack helps us trick RocksDB into using ircd::ctx rather than posix threads. We achieve this by conjuring magics using shared library PLT indirection while praying that the class layout and semantics RocksDB's compiler has assumed does not thwart our plans. These definitions are in ircd/db_port.cc.

Interface

The database here is a strictly schematized key-value grid built from the primitives of column, cell and row. We use the database as both a flat-object store (matrix event db) using cells, columns and rows; as well as binary data block store (matrix media db) using just a single column.

Columns

A column is a key-value store. We specify columns in the database descriptors when opening the database and (though technically somewhat possible) don't change them during runtime. The database must be opened with the same descriptors in properly aligned positions every single time. All keys and values in a column can be iterated. Columns can also be split into "domains" of keys (see: db/index.h) based on the key's prefix, allowing a seek and iteration isolated to just one domain.

In practice we create a column to present a single property in a JSON object. There is no recursion and we also must know the name of the property in advance to specify a descriptor for it. Applied, we create a column for each property in the Matrix event object† and then optimize that column specifically for that property.

{
	"origin_server_ts": 15373977384823,
	"content": {"msgtype": "m.text", "body": "hello"}
}

For the above example consider two columns: first origin_server_ts is optimized for timestamps by having values which are fixed 8 byte signed integers; next the content object is stored in whole as JSON text in the content column. Recursion is not yet supported but theoretically we can create more columns to hold nested properties if we want further optimizations.

† Note that the application in the example has been further optimized; not every property in the Matrix event has a dedicated column, and an additional meta-column is used to store full JSON events (albeit compressed) which is may be selected to improve performance by making a single larger request rather than several smaller requests to compose the same data.

Rows

Since columns are technically independent key-value stores (they have their own index), when an index key is the same between columns we call this a row. In the Matrix event example, each property of the same event is sought together in a row. A row seek is optimized and the individual cells are queried concurrently and iterated in lock-step together.

There is a caveat to rows even though the queries to individual cells are made concurrently: each cell still requires an individual request to the storage device. The device has a fixed number of total requests it can service concurrently.

Cells

A cell is a gratuitious interface representing of a single value in a column with a common key that should be able to form a row between columns. A row is comprised of cells.

Important notes

!!! The database system is plugged into the userspace context system to facilitate IO. This means that an expensive database call (mostly on the read side) that has to do disk IO will suspend your userspace context. Remember that when your userspace context resumes on the other side of the call, the state of IRCd and even the database itself may have changed. We have a suite of tools to mitigate this. !!!

  • While the database schema is modifiable at runtime (we can add and remove columns on the fly) the database is very picky about opening the exact same way it last closed. This means, for now, we have the full object schema explicitly specified when the DB is first opened. All columns exist for the lifetime of the DB, whether or not you have a handle to them.