View on GitHub

FOnline Engine

Flexible cross-platform isometric game engine

Persistence

This document explains the server-side database abstraction, collection/key model, commit queue, recovery logs, and backend implementations.

Use it when changing Source/Server/DataBase.*, database settings, entity save/load code, or persistence tests.

Ownership model

The engine owns the reusable database abstraction and backend implementations. An embedding project owns deployment choices, connection strings, backup policy, data migration policy, and operational runbooks.

Do not put live credentials, production connection strings, or host-specific recovery steps in this engine document.

Public database facade

Source/Server/DataBase.h defines the public facade DataBase. It wraps a DataBaseImpl backend and exposes collection/document operations:

ConnectToDataBase() constructs the facade from settings, connection info, collection schemas, and a panic callback.

Collections and keys

The database layer stores AnyData::Document values in named collections.

Core types:

DataBaseImpl::ValidateCollectionKey() enforces that collection schemas and record IDs agree. When adding a new persistent collection, add the schema at the server/entity-manager layer and validate all backend implementations.

Backend interface

DataBaseImpl is the backend base class. Backends must implement:

Backends can override:

Factory functions declared in DataBase.h:

Implementation files:

Commit queue

Writes are represented as commit operations:

DataBaseImpl queues pending commit operations and processes them through commit-thread machinery:

The public DataBase facade forwards write calls into this machinery. Backend write implementations should remain focused on durable record operations, while shared logic handles scheduling, operation logs, panic/retry policy, and metrics.

Recovery logs and panic policy

DataBaseImpl::RecoveryLogHandle owns an operation-log file:

DataBaseImpl can keep pending and committed change logs:

Recovery/panic settings include:

Relevant methods:

When changing commit durability, validate failed-write recovery and pending-log restoration, not only successful writes.

Backend-specific notes

DocumentToBson() and BsonToDocument() convert between AnyData::Document and BSON for Mongo-backed storage. GetDbKeyType() reports whether a runtime key is integer- or string-backed.

Relationship to entity state

Persistence stores documents; entity state reaches those documents through property serialization and server entity-management code.

Relevant entity/property concepts from EntityModel.md:

Do not add database-specific assumptions to Entity or Properties unless all backends and tests can support the behavior.

Metrics and diagnostics

GetDbRequestsPerMinute() reports recent database request volume using per-second buckets. Backend failures and reconnect attempts are tracked in DataBaseImpl state.

DrawGui() is available at both facade and backend levels for debug/inspection UI.

Tests to inspect

Relevant tests include:

Change routing

Validation checklist

  1. Run Source/Tests/Test_DataBase.cpp or the embedding project’s equivalent database test target.
  2. Validate insert, update, delete, get, valid, and ID enumeration for every affected backend.
  3. Validate integer-key and string-key collections when changing key handling.
  4. Validate commit queue drain with StartCommitChanges() / WaitCommitChanges().
  5. Validate operation-log restore after a simulated failed commit when durability/recovery behavior changes.
  6. Validate entity save/load paths when persistent property semantics change.
  7. Never put production credentials or live connection strings into repository docs or tests.