Skip to content

The readings in the database

tilttek/reading-quests-bundle

The repositories work on a Doctrine DBAL connection, in tables that ReadingTables describes for each platform and the schema listener adds to the schema the ORM generates.

The tables

tilttek_reading_progress holds one row per reading:

ColumnHolds
reader, book, readingThe reader's resource reference, the book's identifier, the reading number from 1: the primary key.
progressThe progress in the form Progress::toString() writes, at most 1 024 bytes.
versionThe version the reading is kept at, 1 once first kept.

The latest reading of a book is read on the primary key, the highest number first; every reading of a reader too.

tilttek_reading_goal holds one row per reader and year, member and year its primary key, with the target and the unit, null for a goal of books.

tilttek_reading_book, with the shelf in the database only, holds one row per book: its id, its pages, null when it has none, and its facts as a JSON object.

On MySQL and MariaDB, the identifiers compare byte by byte, as they do in PHP.

Writers at once

A new reading, at version 0, is inserted; a second one of the same number meets the primary key. A known one is updated where it is still at its version, compared in the UPDATE itself. A writer that finds another version, a reading taken, a deadlock or a lock held too long writes nothing and throws ReadingConflict.

A goal or a book is replaced in place: an UPDATE, and an INSERT when no row matched. A driver that counts the rows it changed rather than those it matched, or a row inserted by another request in between, makes the INSERT meet the primary key; the row is then updated.

The shelf in the database

DbalBookShelf is public with shelf.source: doctrine, for the pages that edit the catalogue:

$shelf->save(new Book('core-rules', 320, ['system' => 'dnd5e']));
$shelf->remove('core-rules');

Each book read is checked again as Book checks it: a row whose facts are not a JSON object of one level, or break the rules of the facts, throws InvalidReadingQuest rather than count a book out of shape.

What a position costs

On SQLite in memory, with 10 000 readings of other readers and a book of 2 000 blocks, a position recorded — the reading read, its activities recorded in a journal in memory, the reading written — takes about 35 µs. The library's own benchmark gives the step alone at about 9 µs.

The repositories keep nothing between two calls: one instance serves every request of a worker.

Beyond the contracts

Four methods serve the privacy module, and an application may call them too: DbalReadingRepository::readingsOf() and eraseReader(), DbalGoalRepository::goalsOf() and eraseMember().